Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use URL when available natively #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ So, `b.myself` points to `b`, not `a`. Neat!

## Changelog

### v2.1.3

#### 2019-12-16

- Use native `URL` on Node >= 10.0.0 (contributed by @iambalaam)

### v2.1.2

#### 2018-03-21
Expand Down
13 changes: 11 additions & 2 deletions clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ try {
nativeSet = function() {};
}

var nativeURL;
try {
nativeURL = URL;
} catch (_) {
nativeURL = function () { };
}

var nativePromise;
try {
nativePromise = Promise;
Expand Down Expand Up @@ -88,6 +95,8 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
child = new nativeMap();
} else if (_instanceof(parent, nativeSet)) {
child = new nativeSet();
} else if (_instanceof(parent, nativeURL)) {
child = new nativeURL(parent);
} else if (_instanceof(parent, nativePromise)) {
child = new nativePromise(function (resolve, reject) {
parent.then(function(value) {
Expand Down Expand Up @@ -187,7 +196,7 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
continue;
}
child[symbol] = _clone(parent[symbol], depth - 1);
Object.defineProperty(child, symbol, descriptor);
Object.defineProperty(child, symbol, _clone(descriptor));
}
}

Expand All @@ -200,7 +209,7 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) {
continue;
}
child[propertyName] = _clone(parent[propertyName], depth - 1);
Object.defineProperty(child, propertyName, descriptor);
Object.defineProperty(child, propertyName, _clone(descriptor));
}
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"function",
"date"
],
"version": "2.1.2",
"version": "2.1.3",
"repository": {
"type": "git",
"url": "git://github.com/pvorb/node-clone.git"
Expand Down Expand Up @@ -40,7 +40,8 @@
"Martin Jurča (https://github.com/jurca)",
"Misery Lee <[email protected]> (https://github.com/miserylee)",
"Clemens Wolff (https://github.com/c-w)",
"Sabin Thomas (https://github.com/sabinthomas)"
"Sabin Thomas (https://github.com/sabinthomas)",
"Guy Balaam (https://github.com/iambalaam)"
],
"license": "MIT",
"engines": {
Expand Down
41 changes: 41 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,47 @@ if (nativeSet) {
}
}

var nativeURLSearchParams;
try {
nativeURLSearchParams = URLSearchParams;
} catch (_) { }
if (nativeURLSearchParams) {
exports["clone a native URLSearchParams"] = function (test) {
var searchParams = new URLSearchParams('?query=value');
// regular circular expando property
searchParams.circle = searchParams;

var clonedSearchParams = clone(searchParams);
test.notEqual(searchParams, clonedSearchParams);
test.equal(clonedSearchParams, clonedSearchParams.circle);

test.done()
}
}

var nativeURL;
try {
nativeURL = URL;
} catch (_) { }
if (nativeURL) {
exports["clone a native URL"] = function (test) {
var url = new URL('https://example.com/path?query=value');
// regular circular expando property
url.circle = url;

var clonedUrl = clone(url);
test.notEqual(url, clonedUrl);
test.equal(clonedUrl.protocol, 'https:');
test.equal(clonedUrl.host, 'example.com');
test.equal(clonedUrl.pathname, '/path');
test.equal(clonedUrl.search, '?query=value');
test.equal(clonedUrl.circle, clonedUrl);
test.notEqual(url.searchParams, clonedUrl.searchParams);

test.done();
}
}

var nativePromise;
try {
nativePromise = Promise;
Expand Down