Skip to content

Commit

Permalink
Support the URL type (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil authored Dec 4, 2023
1 parent c3460d6 commit ac1dc42
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
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

0 comments on commit ac1dc42

Please sign in to comment.