web.url-search-params.has.js 1.2 KB

12345678910111213141516171819202122232425262728
  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 getAll = uncurryThis(URLSearchParamsPrototype.getAll);
  9. var $has = uncurryThis(URLSearchParamsPrototype.has);
  10. var params = new $URLSearchParams('a=1');
  11. // `undefined` case is a Chromium 117 bug
  12. // https://bugs.chromium.org/p/v8/issues/detail?id=14222
  13. if (params.has('a', 2) || !params.has('a', undefined)) {
  14. defineBuiltIn(URLSearchParamsPrototype, 'has', function has(name /* , value */) {
  15. var length = arguments.length;
  16. var $value = length < 2 ? undefined : arguments[1];
  17. if (length && $value === undefined) return $has(this, name);
  18. var values = getAll(this, name); // also validates `this`
  19. validateArgumentsLength(length, 1);
  20. var value = toString($value);
  21. var index = 0;
  22. while (index < values.length) {
  23. if (values[index++] === value) return true;
  24. } return false;
  25. }, { enumerable: true, unsafe: true });
  26. }