Skip to content

Commit

Permalink
add URL.canParse method
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 3, 2023
1 parent 49b1884 commit 32fcbab
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added [`URL.canParse` method](https://url.spec.whatwg.org/#dom-url-canparse), [url/763](https://github.com/whatwg/url/pull/763)
- [`Set` methods proposal](https://github.com/tc39/proposal-set-methods):
- Removed sort from `Set.prototype.intersection`, [March 2023 TC39 meeting](https://github.com/babel/proposals/issues/87#issuecomment-1478610425), [proposal-set-methods/94](https://github.com/tc39/proposal-set-methods/pull/94)
- (Async) Iterator Helpers proposals ([sync](https://github.com/tc39/proposal-iterator-helpers), [async](https://github.com/tc39/proposal-async-iterator-helpers)):
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ queueMicrotask(() => console.log('called as microtask'));
```
#### `URL` and `URLSearchParams`[⬆](#index)
[`URL` standard](https://url.spec.whatwg.org/) implementation. Modules [`web.url`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.js), [`web.url.to-json`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.to-json.js), [`web.url-search-params`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js), [`web.url-search-params.size`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.size.js).
[`URL` standard](https://url.spec.whatwg.org/) implementation. Modules [`web.url`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.js), [`web.url.can-parse`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.can-parse.js), [`web.url.to-json`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url.to-json.js), [`web.url-search-params`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.js), [`web.url-search-params.size`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.url-search-params.size.js).
```js
class URL {
constructor(url: string, base?: string);
Expand All @@ -3231,6 +3231,7 @@ class URL {
attribute hash: string;
toJSON(): string;
toString(): string;
static canParse(url: string, base?: string): boolean;
}
class URLSearchParams {
Expand All @@ -3255,11 +3256,15 @@ class URLSearchParams {
```js
core-js/proposals/url
core-js(-pure)/stable|actual|full/url
core-js(-pure)/stable|actual|full/url/can-parse
core-js/stable|actual|full/url/to-json
core-js(-pure)/stable|actual|full/url-search-params
```
[*Examples*](https://tinyurl.com/2fccy7sb):
[*Examples*](https://tinyurl.com/2ovt23zn):
```js
URL.canParse('https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment'); // => true
URL.canParse('https'); // => false

const url = new URL('https://login:password@example.com:8080/foo/bar?a=1&b=2&a=3#fragment');

console.log(url.href); // => 'https://login:password@example.com:8080/foo/bar?a=1&b=2&a=3#fragment'
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,8 @@ export const data = {
node: '10.0',
safari: '14.0',
},
'web.url.can-parse': {
},
'web.url.to-json': {
bun: '0.1.1',
chrome: '71',
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,7 @@ export default {
3.29: [
'web.url-search-params.size',
],
'3.30': [
'web.url.can-parse',
],
};
3 changes: 3 additions & 0 deletions packages/core-js/actual/url/can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var parent = require('../../stable/url/can-parse');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/full/url/can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var parent = require('../../actual/url/can-parse');

module.exports = parent;
21 changes: 21 additions & 0 deletions packages/core-js/modules/web.url.can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var validateArgumentsLength = require('../internals/validate-arguments-length');
var toString = require('../internals/to-string');

var URL = getBuiltIn('URL');

// `URL.canParse` method
// https://url.spec.whatwg.org/#dom-url-canparse
$({ target: 'URL', stat: true }, {
canParse: function canParse(url) {
var length = validateArgumentsLength(arguments.length, 1);
var urlString = toString(url);
var base = length < 2 || arguments[1] === undefined ? undefined : toString(arguments[1]);
try {
return !!new URL(urlString, base);
} catch (error) {
return false;
}
}
});
1 change: 1 addition & 0 deletions packages/core-js/proposals/url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// https://github.com/jasnell/proposal-url
require('../modules/web.url');
require('../modules/web.url.can-parse');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
5 changes: 5 additions & 0 deletions packages/core-js/stable/url/can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('../../modules/web.url');
require('../../modules/web.url.can-parse');
var path = require('../../internals/path');

module.exports = path.URL.canParse;
1 change: 1 addition & 0 deletions packages/core-js/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require('../modules/web.self');
require('../modules/web.structured-clone');
require('../modules/web.timers');
require('../modules/web.url');
require('../modules/web.url.can-parse');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
Expand Down
1 change: 1 addition & 0 deletions packages/core-js/web/url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('../modules/web.url');
require('../modules/web.url.can-parse');
require('../modules/web.url.to-json');
require('../modules/web.url-search-params');
require('../modules/web.url-search-params.size');
Expand Down
3 changes: 3 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1936,6 +1936,9 @@ GLOBAL.tests = {
// TODO: Remove this module from `core-js@4` since it's split to submodules
'web.timers': TIMERS,
'web.url.constructor': URL_AND_URL_SEARCH_PARAMS_SUPPORT,
'web.url.can-parse': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return URL.canParse;
}],
'web.url.to-json': [URL_AND_URL_SEARCH_PARAMS_SUPPORT, function () {
return URL.prototype.toJSON;
}],
Expand Down
1 change: 1 addition & 0 deletions tests/entries/unit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(typeof load(NS, 'clear-immediate') == 'function');
ok(typeof load(NS, 'queue-microtask') == 'function');
ok(typeof load(NS, 'url') == 'function');
ok(load(NS, 'url/can-parse')('a:b'));
load(NS, 'url/to-json');
ok(typeof load(NS, 'url-search-params') == 'function');
}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit-global/web.url.can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
QUnit.test('URL.canParse', assert => {
const { canParse } = URL;

assert.isFunction(canParse);
assert.arity(canParse, 1);
assert.name(canParse, 'canParse');
assert.looksNative(canParse);

assert.false(canParse(undefined), 'undefined');
assert.false(canParse(undefined, undefined), 'undefined, undefined');
assert.true(canParse('q:w'), 'q:w');
assert.true(canParse('q:w', undefined), 'q:w, undefined');
assert.false(canParse(undefined, 'q:w'), 'undefined, q:w');
assert.true(canParse('q:/w'), 'q:/w');
assert.true(canParse('q:/w', undefined), 'q:w, undefined');
assert.true(canParse(undefined, 'q:/w'), 'undefined, q:w');
assert.false(canParse('https://login:password@examp:le.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'https://login:password@examp:le.com:8080/?a=1&b=2&a=3&c=4#fragment');
assert.true(canParse('https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment');
assert.true(canParse('https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment', undefined), 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment, undefined');
assert.true(canParse('x', 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'x, https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment');

assert.throws(() => canParse(), 'no args');
assert.throws(() => canParse({ toString() { throw Error('thrower'); } }), 'conversion thrower #1');
assert.throws(() => canParse('q:w', { toString() { throw Error('thrower'); } }), 'conversion thrower #2');
});
24 changes: 24 additions & 0 deletions tests/unit-pure/web.url.can-parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import canParse from 'core-js-pure/stable/url/can-parse';

QUnit.test('URL.canParse', assert => {
assert.isFunction(canParse);
assert.arity(canParse, 1);
assert.name(canParse, 'canParse');

assert.false(canParse(undefined), 'undefined');
assert.false(canParse(undefined, undefined), 'undefined, undefined');
assert.true(canParse('q:w'), 'q:w');
assert.true(canParse('q:w', undefined), 'q:w, undefined');
assert.false(canParse(undefined, 'q:w'), 'undefined, q:w');
assert.true(canParse('q:/w'), 'q:/w');
assert.true(canParse('q:/w', undefined), 'q:w, undefined');
assert.true(canParse(undefined, 'q:/w'), 'undefined, q:w');
assert.false(canParse('https://login:password@examp:le.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'https://login:password@examp:le.com:8080/?a=1&b=2&a=3&c=4#fragment');
assert.true(canParse('https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment');
assert.true(canParse('https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment', undefined), 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment, undefined');
assert.true(canParse('x', 'https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment'), 'x, https://login:password@example.com:8080/?a=1&b=2&a=3&c=4#fragment');

assert.throws(() => canParse(), 'no args');
assert.throws(() => canParse({ toString() { throw Error('thrower'); } }), 'conversion thrower #1');
assert.throws(() => canParse('q:w', { toString() { throw Error('thrower'); } }), 'conversion thrower #2');
});

0 comments on commit 32fcbab

Please sign in to comment.