URLSearchParams-impl.js 2.6 KB

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