summaryrefslogtreecommitdiff
path: root/cli/js/url_search_params.ts
blob: 5b7f0ecd82a2de691885bff22921b30cf43c7e96 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { URL } from "./url.ts";
import { requiredArguments, isIterable } from "./util.ts";

export class URLSearchParams {
  private params: Array<[string, string]> = [];
  private url: URL | null = null;

  constructor(init: string | string[][] | Record<string, string> = "") {
    if (typeof init === "string") {
      this._handleStringInitialization(init);
      return;
    }

    if (Array.isArray(init) || isIterable(init)) {
      this._handleArrayInitialization(init);
      return;
    }

    if (Object(init) !== init) {
      return;
    }

    if (init instanceof URLSearchParams) {
      this.params = init.params;
      return;
    }

    // Overload: record<USVString, USVString>
    for (const key of Object.keys(init)) {
      this.append(key, init[key]);
    }
  }

  private updateSteps(): void {
    if (this.url === null) {
      return;
    }

    let query: string | null = this.toString();
    if (query === "") {
      query = null;
    }

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (this.url as any)._parts.query = query;
  }

  /** Appends a specified key/value pair as a new search parameter.
   *
   *       searchParams.append('name', 'first');
   *       searchParams.append('name', 'second');
   */
  append(name: string, value: string): void {
    requiredArguments("URLSearchParams.append", arguments.length, 2);
    this.params.push([String(name), String(value)]);
    this.updateSteps();
  }

  /** Deletes the given search parameter and its associated value,
   * from the list of all search parameters.
   *
   *       searchParams.delete('name');
   */
  delete(name: string): void {
    requiredArguments("URLSearchParams.delete", arguments.length, 1);
    name = String(name);
    let i = 0;
    while (i < this.params.length) {
      if (this.params[i][0] === name) {
        this.params.splice(i, 1);
      } else {
        i++;
      }
    }
    this.updateSteps();
  }

  /** Returns all the values associated with a given search parameter
   * as an array.
   *
   *       searchParams.getAll('name');
   */
  getAll(name: string): string[] {
    requiredArguments("URLSearchParams.getAll", arguments.length, 1);
    name = String(name);
    const values = [];
    for (const entry of this.params) {
      if (entry[0] === name) {
        values.push(entry[1]);
      }
    }

    return values;
  }

  /** Returns the first value associated to the given search parameter.
   *
   *       searchParams.get('name');
   */
  get(name: string): string | null {
    requiredArguments("URLSearchParams.get", arguments.length, 1);
    name = String(name);
    for (const entry of this.params) {
      if (entry[0] === name) {
        return entry[1];
      }
    }

    return null;
  }

  /** Returns a Boolean that indicates whether a parameter with the
   * specified name exists.
   *
   *       searchParams.has('name');
   */
  has(name: string): boolean {
    requiredArguments("URLSearchParams.has", arguments.length, 1);
    name = String(name);
    return this.params.some((entry): boolean => entry[0] === name);
  }

  /** Sets the value associated with a given search parameter to the
   * given value. If there were several matching values, this method
   * deletes the others. If the search parameter doesn't exist, this
   * method creates it.
   *
   *       searchParams.set('name', 'value');
   */
  set(name: string, value: string): void {
    requiredArguments("URLSearchParams.set", arguments.length, 2);

    // If there are any name-value pairs whose name is name, in list,
    // set the value of the first such name-value pair to value
    // and remove the others.
    name = String(name);
    value = String(value);
    let found = false;
    let i = 0;
    while (i < this.params.length) {
      if (this.params[i][0] === name) {
        if (!found) {
          this.params[i][1] = value;
          found = true;
          i++;
        } else {
          this.params.splice(i, 1);
        }
      } else {
        i++;
      }
    }

    // Otherwise, append a new name-value pair whose name is name
    // and value is value, to list.
    if (!found) {
      this.append(name, value);
    }

    this.updateSteps();
  }

  /** Sort all key/value pairs contained in this object in place and
   * return undefined. The sort order is according to Unicode code
   * points of the keys.
   *
   *       searchParams.sort();
   */
  sort(): void {
    this.params = this.params.sort((a, b): number =>
      a[0] === b[0] ? 0 : a[0] > b[0] ? 1 : -1
    );
    this.updateSteps();
  }

  /** Calls a function for each element contained in this object in
   * place and return undefined. Optionally accepts an object to use
   * as this when executing callback as second argument.
   *
   *       searchParams.forEach((value, key, parent) => {
   *         console.log(value, key, parent);
   *       });
   *
   */
  forEach(
    callbackfn: (value: string, key: string, parent: this) => void,
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    thisArg?: any
  ): void {
    requiredArguments("URLSearchParams.forEach", arguments.length, 1);

    if (typeof thisArg !== "undefined") {
      callbackfn = callbackfn.bind(thisArg);
    }

    for (const [key, value] of this.entries()) {
      callbackfn(value, key, this);
    }
  }

  /** Returns an iterator allowing to go through all keys contained
   * in this object.
   *
   *       for (const key of searchParams.keys()) {
   *         console.log(key);
   *       }
   */
  *keys(): IterableIterator<string> {
    for (const entry of this.params) {
      yield entry[0];
    }
  }

  /** Returns an iterator allowing to go through all values contained
   * in this object.
   *
   *       for (const value of searchParams.values()) {
   *         console.log(value);
   *       }
   */
  *values(): IterableIterator<string> {
    for (const entry of this.params) {
      yield entry[1];
    }
  }

  /** Returns an iterator allowing to go through all key/value
   * pairs contained in this object.
   *
   *       for (const [key, value] of searchParams.entries()) {
   *         console.log(key, value);
   *       }
   */
  *entries(): IterableIterator<[string, string]> {
    yield* this.params;
  }

  /** Returns an iterator allowing to go through all key/value
   * pairs contained in this object.
   *
   *       for (const [key, value] of searchParams[Symbol.iterator]()) {
   *         console.log(key, value);
   *       }
   */
  *[Symbol.iterator](): IterableIterator<[string, string]> {
    yield* this.params;
  }

  /** Returns a query string suitable for use in a URL.
   *
   *        searchParams.toString();
   */
  toString(): string {
    return this.params
      .map(
        (tuple): string =>
          `${encodeURIComponent(tuple[0])}=${encodeURIComponent(tuple[1])}`
      )
      .join("&");
  }

  private _handleStringInitialization(init: string): void {
    // Overload: USVString
    // If init is a string and starts with U+003F (?),
    // remove the first code point from init.
    if (init.charCodeAt(0) === 0x003f) {
      init = init.slice(1);
    }

    for (const pair of init.split("&")) {
      // Empty params are ignored
      if (pair.length === 0) {
        continue;
      }
      const position = pair.indexOf("=");
      const name = pair.slice(0, position === -1 ? pair.length : position);
      const value = pair.slice(name.length + 1);
      this.append(decodeURIComponent(name), decodeURIComponent(value));
    }
  }

  private _handleArrayInitialization(
    init: string[][] | Iterable<[string, string]>
  ): void {
    // Overload: sequence<sequence<USVString>>
    for (const tuple of init) {
      // If pair does not contain exactly two items, then throw a TypeError.
      if (tuple.length !== 2) {
        throw new TypeError(
          "URLSearchParams.constructor tuple array argument must only contain pair elements"
        );
      }
      this.append(tuple[0], tuple[1]);
    }
  }
}