| 1 | "use strict";
|
|---|
| 2 | const stableSortBy = require("lodash/sortBy");
|
|---|
| 3 | const urlencoded = require("./urlencoded");
|
|---|
| 4 |
|
|---|
| 5 | exports.implementation = class URLSearchParamsImpl {
|
|---|
| 6 | constructor(globalObject, constructorArgs, { doNotStripQMark = false }) {
|
|---|
| 7 | let init = constructorArgs[0];
|
|---|
| 8 | this._list = [];
|
|---|
| 9 | this._url = null;
|
|---|
| 10 |
|
|---|
| 11 | if (!doNotStripQMark && typeof init === "string" && init[0] === "?") {
|
|---|
| 12 | init = init.slice(1);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | if (Array.isArray(init)) {
|
|---|
| 16 | for (const pair of init) {
|
|---|
| 17 | if (pair.length !== 2) {
|
|---|
| 18 | throw new TypeError("Failed to construct 'URLSearchParams': parameter 1 sequence's element does not " +
|
|---|
| 19 | "contain exactly two elements.");
|
|---|
| 20 | }
|
|---|
| 21 | this._list.push([pair[0], pair[1]]);
|
|---|
| 22 | }
|
|---|
| 23 | } else if (typeof init === "object" && Object.getPrototypeOf(init) === null) {
|
|---|
| 24 | for (const name of Object.keys(init)) {
|
|---|
| 25 | const value = init[name];
|
|---|
| 26 | this._list.push([name, value]);
|
|---|
| 27 | }
|
|---|
| 28 | } else {
|
|---|
| 29 | this._list = urlencoded.parseUrlencodedString(init);
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | _updateSteps() {
|
|---|
| 34 | if (this._url !== null) {
|
|---|
| 35 | let query = urlencoded.serializeUrlencoded(this._list);
|
|---|
| 36 | if (query === "") {
|
|---|
| 37 | query = null;
|
|---|
| 38 | }
|
|---|
| 39 | this._url._url.query = query;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | append(name, value) {
|
|---|
| 44 | this._list.push([name, value]);
|
|---|
| 45 | this._updateSteps();
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | delete(name) {
|
|---|
| 49 | let i = 0;
|
|---|
| 50 | while (i < this._list.length) {
|
|---|
| 51 | if (this._list[i][0] === name) {
|
|---|
| 52 | this._list.splice(i, 1);
|
|---|
| 53 | } else {
|
|---|
| 54 | i++;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | this._updateSteps();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | get(name) {
|
|---|
| 61 | for (const tuple of this._list) {
|
|---|
| 62 | if (tuple[0] === name) {
|
|---|
| 63 | return tuple[1];
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | return null;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | getAll(name) {
|
|---|
| 70 | const output = [];
|
|---|
| 71 | for (const tuple of this._list) {
|
|---|
| 72 | if (tuple[0] === name) {
|
|---|
| 73 | output.push(tuple[1]);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | return output;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | has(name) {
|
|---|
| 80 | for (const tuple of this._list) {
|
|---|
| 81 | if (tuple[0] === name) {
|
|---|
| 82 | return true;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | return false;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | set(name, value) {
|
|---|
| 89 | let found = false;
|
|---|
| 90 | let i = 0;
|
|---|
| 91 | while (i < this._list.length) {
|
|---|
| 92 | if (this._list[i][0] === name) {
|
|---|
| 93 | if (found) {
|
|---|
| 94 | this._list.splice(i, 1);
|
|---|
| 95 | } else {
|
|---|
| 96 | found = true;
|
|---|
| 97 | this._list[i][1] = value;
|
|---|
| 98 | i++;
|
|---|
| 99 | }
|
|---|
| 100 | } else {
|
|---|
| 101 | i++;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | if (!found) {
|
|---|
| 105 | this._list.push([name, value]);
|
|---|
| 106 | }
|
|---|
| 107 | this._updateSteps();
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | sort() {
|
|---|
| 111 | this._list = stableSortBy(this._list, [0]);
|
|---|
| 112 | this._updateSteps();
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | [Symbol.iterator]() {
|
|---|
| 116 | return this._list[Symbol.iterator]();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | toString() {
|
|---|
| 120 | return urlencoded.serializeUrlencoded(this._list);
|
|---|
| 121 | }
|
|---|
| 122 | };
|
|---|