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

Support the URL type #392

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
18 changes: 8 additions & 10 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,14 @@ module.exports = internals.clone = function (obj, options = {}, _seen = null) {
// Built-in object types

const baseProto = Types.getInternalProto(obj);
if (baseProto === Types.buffer) {
return Buffer && Buffer.from(obj); // $lab:coverage:ignore$
}

if (baseProto === Types.date) {
return new Date(obj.getTime());
}

if (baseProto === Types.regex) {
return new RegExp(obj);
switch (baseProto) {
case Types.buffer:
return Buffer?.from(obj);
case Types.date:
return new Date(obj.getTime());
case Types.regex:
case Types.url:
return new baseProto.constructor(obj);
}

// Generic objects
Expand Down
1 change: 1 addition & 0 deletions lib/deepEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ internals.isDeepEqual = function (obj, ref, options, seen) {
case Types.promise:
return obj === ref;
case Types.regex:
case Types.url:
return obj.toString() === ref.toString();
case internals.mismatched:
return false;
Expand Down
2 changes: 2 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports = module.exports = {
promise: Promise.prototype,
regex: RegExp.prototype,
set: Set.prototype,
url: URL.prototype,
weakMap: WeakMap.prototype,
weakSet: WeakSet.prototype
};
Expand All @@ -23,6 +24,7 @@ internals.typeMap = new Map([
['[object Map]', exports.map],
['[object Promise]', exports.promise],
['[object Set]', exports.set],
['[object URL]', exports.url],
['[object WeakMap]', exports.weakMap],
['[object WeakSet]', exports.weakSet]
]);
Expand Down
9 changes: 9 additions & 0 deletions test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,15 @@ describe('clone()', () => {
expect(b.get(nestedObj)).to.equal(a.get(nestedObj));
});

it('clones an URL', () => {

const a = new URL('https://hapi.dev/');
const b = Hoek.clone(a);

expect(b.href).to.equal(a.href);
expect(b).to.not.shallow.equal(a);
});

it('ignores symbols', () => {

const sym = Symbol();
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,15 @@ describe('deepEqual()', () => {
expect(Hoek.deepEqual(a, new Promise(() => { }))).to.be.false();
});

it('compares urls', () => {

const a = new URL('https://hapi.dev/');

expect(Hoek.deepEqual(a, a)).to.be.true();
expect(Hoek.deepEqual(a, new URL('https://hapi.dev/?new'))).to.be.false();
expect(Hoek.deepEqual(a, {}, { prototype: false })).to.be.false();
});

it('compares buffers', () => {

expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3]))).to.be.true();
Expand Down