-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
URLSearchParams-impl.js
122 lines (109 loc) · 2.62 KB
/
URLSearchParams-impl.js
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
"use strict";
const stableSortBy = require("lodash.sortby");
const urlencoded = require("./urlencoded");
exports.implementation = class URLSearchParamsImpl {
constructor(constructorArgs, { doNotStripQMark = false }) {
let init = constructorArgs[0];
this._list = [];
this._url = null;
if (!doNotStripQMark && typeof init === "string" && init[0] === "?") {
init = init.slice(1);
}
if (Array.isArray(init)) {
for (const pair of init) {
if (pair.length !== 2) {
throw new TypeError("Failed to construct 'URLSearchParams': parameter 1 sequence's element does not " +
"contain exactly two elements.");
}
this._list.push([pair[0], pair[1]]);
}
} else if (typeof init === "object" && Object.getPrototypeOf(init) === null) {
for (const name of Object.keys(init)) {
const value = init[name];
this._list.push([name, value]);
}
} else {
this._list = urlencoded.parseUrlencoded(init);
}
}
_updateSteps() {
if (this._url !== null) {
let query = urlencoded.serializeUrlencoded(this._list);
if (query === "") {
query = null;
}
this._url._url.query = query;
}
}
append(name, value) {
this._list.push([name, value]);
this._updateSteps();
}
delete(name) {
let i = 0;
while (i < this._list.length) {
if (this._list[i][0] === name) {
this._list.splice(i, 1);
} else {
i++;
}
}
this._updateSteps();
}
get(name) {
for (const tuple of this._list) {
if (tuple[0] === name) {
return tuple[1];
}
}
return null;
}
getAll(name) {
const output = [];
for (const tuple of this._list) {
if (tuple[0] === name) {
output.push(tuple[1]);
}
}
return output;
}
has(name) {
for (const tuple of this._list) {
if (tuple[0] === name) {
return true;
}
}
return false;
}
set(name, value) {
let found = false;
let i = 0;
while (i < this._list.length) {
if (this._list[i][0] === name) {
if (found) {
this._list.splice(i, 1);
} else {
found = true;
this._list[i][1] = value;
i++;
}
} else {
i++;
}
}
if (!found) {
this._list.push([name, value]);
}
this._updateSteps();
}
sort() {
this._list = stableSortBy(this._list, [0]);
this._updateSteps();
}
[Symbol.iterator]() {
return this._list[Symbol.iterator]();
}
toString() {
return urlencoded.serializeUrlencoded(this._list);
}
};