mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
Remove duplicate code and allow filename overwrite for DomFile (#6817)
This commit is contained in:
parent
628c10b552
commit
903d28f872
1 changed files with 19 additions and 43 deletions
|
@ -8,26 +8,27 @@
|
||||||
|
|
||||||
const dataSymbol = Symbol("data");
|
const dataSymbol = Symbol("data");
|
||||||
|
|
||||||
|
function parseFormDataValue(value, filename) {
|
||||||
|
if (value instanceof domFile.DomFile) {
|
||||||
|
return new domFile.DomFile([value], filename || value.name, {
|
||||||
|
type: value.type,
|
||||||
|
});
|
||||||
|
} else if (value instanceof blob.Blob) {
|
||||||
|
return new domFile.DomFile([value], filename || "blob", {
|
||||||
|
type: value.type,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return String(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class FormDataBase {
|
class FormDataBase {
|
||||||
[dataSymbol] = [];
|
[dataSymbol] = [];
|
||||||
|
|
||||||
append(
|
append(name, value, filename) {
|
||||||
name,
|
|
||||||
value,
|
|
||||||
filename,
|
|
||||||
) {
|
|
||||||
requiredArguments("FormData.append", arguments.length, 2);
|
requiredArguments("FormData.append", arguments.length, 2);
|
||||||
name = String(name);
|
name = String(name);
|
||||||
if (value instanceof domFile.DomFile) {
|
this[dataSymbol].push([name, parseFormDataValue(value, filename)]);
|
||||||
this[dataSymbol].push([name, value]);
|
|
||||||
} else if (value instanceof blob.Blob) {
|
|
||||||
const dfile = new domFile.DomFile([value], filename || "blob", {
|
|
||||||
type: value.type,
|
|
||||||
});
|
|
||||||
this[dataSymbol].push([name, dfile]);
|
|
||||||
} else {
|
|
||||||
this[dataSymbol].push([name, String(value)]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(name) {
|
delete(name) {
|
||||||
|
@ -74,11 +75,7 @@
|
||||||
return this[dataSymbol].some((entry) => entry[0] === name);
|
return this[dataSymbol].some((entry) => entry[0] === name);
|
||||||
}
|
}
|
||||||
|
|
||||||
set(
|
set(name, value, filename) {
|
||||||
name,
|
|
||||||
value,
|
|
||||||
filename,
|
|
||||||
) {
|
|
||||||
requiredArguments("FormData.set", arguments.length, 2);
|
requiredArguments("FormData.set", arguments.length, 2);
|
||||||
name = String(name);
|
name = String(name);
|
||||||
|
|
||||||
|
@ -89,19 +86,7 @@
|
||||||
while (i < this[dataSymbol].length) {
|
while (i < this[dataSymbol].length) {
|
||||||
if (this[dataSymbol][i][0] === name) {
|
if (this[dataSymbol][i][0] === name) {
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (value instanceof domFile.DomFile) {
|
this[dataSymbol][i][1] = parseFormDataValue(value, filename);
|
||||||
this[dataSymbol][i][1] = value;
|
|
||||||
} else if (value instanceof blob.Blob) {
|
|
||||||
this[dataSymbol][i][1] = new domFile.DomFile(
|
|
||||||
[value],
|
|
||||||
filename || "blob",
|
|
||||||
{
|
|
||||||
type: value.type,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
this[dataSymbol][i][1] = String(value);
|
|
||||||
}
|
|
||||||
found = true;
|
found = true;
|
||||||
} else {
|
} else {
|
||||||
this[dataSymbol].splice(i, 1);
|
this[dataSymbol].splice(i, 1);
|
||||||
|
@ -113,16 +98,7 @@
|
||||||
|
|
||||||
// Otherwise, append entry to the context object’s entry list.
|
// Otherwise, append entry to the context object’s entry list.
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (value instanceof domFile.DomFile) {
|
this[dataSymbol].push([name, parseFormDataValue(value, filename)]);
|
||||||
this[dataSymbol].push([name, value]);
|
|
||||||
} else if (value instanceof blob.Blob) {
|
|
||||||
const dfile = new domFile.DomFile([value], filename || "blob", {
|
|
||||||
type: value.type,
|
|
||||||
});
|
|
||||||
this[dataSymbol].push([name, dfile]);
|
|
||||||
} else {
|
|
||||||
this[dataSymbol].push([name, String(value)]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue