URLSearchParams-impl.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 query = urlencoded.serializeUrlencoded(this._list);
  31. if (query === "") {
  32. query = null;
  33. }
  34. this._url._url.query = query;
  35. }
  36. }
  37. append(name, value) {
  38. this._list.push([name, value]);
  39. this._updateSteps();
  40. }
  41. delete(name) {
  42. let i = 0;
  43. while (i < this._list.length) {
  44. if (this._list[i][0] === name) {
  45. this._list.splice(i, 1);
  46. } else {
  47. i++;
  48. }
  49. }
  50. this._updateSteps();
  51. }
  52. get(name) {
  53. for (const tuple of this._list) {
  54. if (tuple[0] === name) {
  55. return tuple[1];
  56. }
  57. }
  58. return null;
  59. }
  60. getAll(name) {
  61. const output = [];
  62. for (const tuple of this._list) {
  63. if (tuple[0] === name) {
  64. output.push(tuple[1]);
  65. }
  66. }
  67. return output;
  68. }
  69. has(name) {
  70. for (const tuple of this._list) {
  71. if (tuple[0] === name) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. set(name, value) {
  78. let found = false;
  79. let i = 0;
  80. while (i < this._list.length) {
  81. if (this._list[i][0] === name) {
  82. if (found) {
  83. this._list.splice(i, 1);
  84. } else {
  85. found = true;
  86. this._list[i][1] = value;
  87. i++;
  88. }
  89. } else {
  90. i++;
  91. }
  92. }
  93. if (!found) {
  94. this._list.push([name, value]);
  95. }
  96. this._updateSteps();
  97. }
  98. sort() {
  99. this._list.sort((a, b) => {
  100. if (a[0] < b[0]) {
  101. return -1;
  102. }
  103. if (a[0] > b[0]) {
  104. return 1;
  105. }
  106. return 0;
  107. });
  108. this._updateSteps();
  109. }
  110. [Symbol.iterator]() {
  111. return this._list[Symbol.iterator]();
  112. }
  113. toString() {
  114. return urlencoded.serializeUrlencoded(this._list);
  115. }
  116. };