From ac1dc42272c675a0ebc7ab89c22c0500250f8afa Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 4 Dec 2023 13:36:23 +0100 Subject: [PATCH] Support the URL type (#392) --- lib/clone.js | 18 ++++++++---------- lib/deepEqual.js | 1 + lib/types.js | 2 ++ test/clone.js | 9 +++++++++ test/index.js | 9 +++++++++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/clone.js b/lib/clone.js index e64defb8..7ecd0b34 100755 --- a/lib/clone.js +++ b/lib/clone.js @@ -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 diff --git a/lib/deepEqual.js b/lib/deepEqual.js index 236945cf..a41df5a9 100755 --- a/lib/deepEqual.js +++ b/lib/deepEqual.js @@ -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; diff --git a/lib/types.js b/lib/types.js index c291b657..056049ff 100755 --- a/lib/types.js +++ b/lib/types.js @@ -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 }; @@ -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] ]); diff --git a/test/clone.js b/test/clone.js index 20754e97..7e73bd64 100755 --- a/test/clone.js +++ b/test/clone.js @@ -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(); diff --git a/test/index.js b/test/index.js index d292574e..4b45b842 100755 --- a/test/index.js +++ b/test/index.js @@ -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();