web.url-search-params.delete.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var defineBuiltIn = require('../internals/define-built-in');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toString = require('../internals/to-string');
  5. var validateArgumentsLength = require('../internals/validate-arguments-length');
  6. var $URLSearchParams = URLSearchParams;
  7. var URLSearchParamsPrototype = $URLSearchParams.prototype;
  8. var append = uncurryThis(URLSearchParamsPrototype.append);
  9. var $delete = uncurryThis(URLSearchParamsPrototype['delete']);
  10. var forEach = uncurryThis(URLSearchParamsPrototype.forEach);
  11. var push = uncurryThis([].push);
  12. var params = new $URLSearchParams('a=1&a=2&b=3');
  13. params['delete']('a', 1);
  14. // `undefined` case is a Chromium 117 bug
  15. // https://bugs.chromium.org/p/v8/issues/detail?id=14222
  16. params['delete']('b', undefined);
  17. if (params + '' !== 'a=2') {
  18. defineBuiltIn(URLSearchParamsPrototype, 'delete', function (name /* , value */) {
  19. var length = arguments.length;
  20. var $value = length < 2 ? undefined : arguments[1];
  21. if (length && $value === undefined) return $delete(this, name);
  22. var entries = [];
  23. forEach(this, function (v, k) { // also validates `this`
  24. push(entries, { key: k, value: v });
  25. });
  26. validateArgumentsLength(length, 1);
  27. var key = toString(name);
  28. var value = toString($value);
  29. var index = 0;
  30. var dindex = 0;
  31. var found = false;
  32. var entriesLength = entries.length;
  33. var entry;
  34. while (index < entriesLength) {
  35. entry = entries[index++];
  36. if (found || entry.key === key) {
  37. found = true;
  38. $delete(this, entry.key);
  39. } else dindex++;
  40. }
  41. while (dindex < entriesLength) {
  42. entry = entries[dindex++];
  43. if (!(entry.key === key && entry.value === value)) append(this, entry.key, entry.value);
  44. }
  45. }, { enumerable: true, unsafe: true });
  46. }