URLSearchParams-impl.js 2.9 KB

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