forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_data.ts
149 lines (135 loc) · 4.43 KB
/
form_data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types";
import * as blob from "./blob";
import * as domFile from "./dom_file";
import { DomIterableMixin } from "./mixins/dom_iterable";
import { requiredArguments } from "./util";
const dataSymbol = Symbol("data");
class FormDataBase {
private [dataSymbol]: Array<[string, domTypes.FormDataEntryValue]> = [];
/** Appends a new value onto an existing key inside a `FormData`
* object, or adds the key if it does not already exist.
*
* formData.append('name', 'first');
* formData.append('name', 'second');
*/
append(name: string, value: string): void;
append(name: string, value: blob.DenoBlob, filename?: string): void;
append(name: string, value: string | blob.DenoBlob, filename?: string): void {
requiredArguments("FormData.append", arguments.length, 2);
name = String(name);
if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DenoFile([value], filename || name);
this[dataSymbol].push([name, dfile]);
} else {
this[dataSymbol].push([name, String(value)]);
}
}
/** Deletes a key/value pair from a `FormData` object.
*
* formData.delete('name');
*/
delete(name: string): void {
requiredArguments("FormData.delete", arguments.length, 1);
name = String(name);
let i = 0;
while (i < this[dataSymbol].length) {
if (this[dataSymbol][i][0] === name) {
this[dataSymbol].splice(i, 1);
} else {
i++;
}
}
}
/** Returns an array of all the values associated with a given key
* from within a `FormData`.
*
* formData.getAll('name');
*/
getAll(name: string): domTypes.FormDataEntryValue[] {
requiredArguments("FormData.getAll", arguments.length, 1);
name = String(name);
const values = [];
for (const entry of this[dataSymbol]) {
if (entry[0] === name) {
values.push(entry[1]);
}
}
return values;
}
/** Returns the first value associated with a given key from within a
* `FormData` object.
*
* formData.get('name');
*/
get(name: string): domTypes.FormDataEntryValue | null {
requiredArguments("FormData.get", arguments.length, 1);
name = String(name);
for (const entry of this[dataSymbol]) {
if (entry[0] === name) {
return entry[1];
}
}
return null;
}
/** Returns a boolean stating whether a `FormData` object contains a
* certain key/value pair.
*
* formData.has('name');
*/
has(name: string): boolean {
requiredArguments("FormData.has", arguments.length, 1);
name = String(name);
return this[dataSymbol].some(entry => entry[0] === name);
}
/** Sets a new value for an existing key inside a `FormData` object, or
* adds the key/value if it does not already exist.
* ref: https://xhr.spec.whatwg.org/#dom-formdata-set
*
* formData.set('name', 'value');
*/
set(name: string, value: string): void;
set(name: string, value: blob.DenoBlob, filename?: string): void;
set(name: string, value: string | blob.DenoBlob, filename?: string): void {
requiredArguments("FormData.set", arguments.length, 2);
name = String(name);
// If there are any entries in the context object’s entry list whose name
// is name, replace the first such entry with entry and remove the others
let found = false;
let i = 0;
while (i < this[dataSymbol].length) {
if (this[dataSymbol][i][0] === name) {
if (!found) {
if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DenoFile([value], filename || name);
this[dataSymbol][i][1] = dfile;
} else {
this[dataSymbol][i][1] = String(value);
}
found = true;
} else {
this[dataSymbol].splice(i, 1);
continue;
}
}
i++;
}
// Otherwise, append entry to the context object’s entry list.
if (!found) {
if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DenoFile([value], filename || name);
this[dataSymbol].push([name, dfile]);
} else {
this[dataSymbol].push([name, String(value)]);
}
}
}
get [Symbol.toStringTag](): string {
return "FormData";
}
}
export class FormData extends DomIterableMixin<
string,
domTypes.FormDataEntryValue,
typeof FormDataBase
>(FormDataBase, dataSymbol) {}