web.url.parse.js 801 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var validateArgumentsLength = require('../internals/validate-arguments-length');
  5. var toString = require('../internals/to-string');
  6. var USE_NATIVE_URL = require('../internals/url-constructor-detection');
  7. var URL = getBuiltIn('URL');
  8. // `URL.parse` method
  9. // https://url.spec.whatwg.org/#dom-url-canparse
  10. $({ target: 'URL', stat: true, forced: !USE_NATIVE_URL }, {
  11. parse: function parse(url) {
  12. var length = validateArgumentsLength(arguments.length, 1);
  13. var urlString = toString(url);
  14. var base = length < 2 || arguments[1] === undefined ? undefined : toString(arguments[1]);
  15. try {
  16. return new URL(urlString, base);
  17. } catch (error) {
  18. return null;
  19. }
  20. }
  21. });