@hapi/hoek
Advanced tools
Comparing version 8.3.1 to 8.3.2
111
lib/clone.js
@@ -39,44 +39,22 @@ 'use strict'; | ||
// Built-in object types | ||
const baseProto = Types.getInternalProto(obj); | ||
let newObj; | ||
if (baseProto === Types.buffer) { | ||
return Buffer && Buffer.from(obj); // $lab:coverage:ignore$ | ||
} | ||
switch (baseProto) { | ||
case Types.buffer: | ||
return Buffer && Buffer.from(obj); // $lab:coverage:ignore$ | ||
if (baseProto === Types.date) { | ||
return new Date(obj.getTime()); | ||
} | ||
case Types.date: | ||
return new Date(obj.getTime()); | ||
if (baseProto === Types.regex) { | ||
return new RegExp(obj); | ||
} | ||
case Types.regex: | ||
return new RegExp(obj); | ||
// Generic objects | ||
case Types.array: | ||
newObj = []; | ||
break; | ||
default: | ||
if (options.prototype !== false) { // Defaults to true | ||
const proto = Object.getPrototypeOf(obj); | ||
if (proto && | ||
proto.isImmutable) { | ||
return obj; | ||
} | ||
if (internals.needsProtoHack.has(baseProto)) { | ||
newObj = new proto.constructor(); | ||
if (proto !== baseProto) { | ||
Object.setPrototypeOf(newObj, proto); | ||
} | ||
} | ||
else { | ||
newObj = Object.create(proto); | ||
} | ||
} | ||
else if (internals.needsProtoHack.has(baseProto)) { | ||
newObj = new baseProto.constructor(); | ||
} | ||
else { | ||
newObj = {}; | ||
} | ||
const newObj = internals.base(obj, baseProto, options); | ||
if (newObj === obj) { | ||
return obj; | ||
} | ||
@@ -100,8 +78,7 @@ | ||
const keys = Utils.keys(obj, options); | ||
for (let i = 0; i < keys.length; ++i) { | ||
const key = keys[i]; | ||
for (const key of keys) { | ||
if (baseProto === Types.array && | ||
key === 'length') { | ||
newObj.length = obj.length; | ||
continue; | ||
@@ -111,10 +88,18 @@ } | ||
const descriptor = Object.getOwnPropertyDescriptor(obj, key); | ||
if (descriptor && | ||
(descriptor.get || descriptor.set)) { | ||
if (descriptor) { | ||
if (descriptor.get || | ||
descriptor.set) { | ||
Object.defineProperty(newObj, key, descriptor); | ||
Object.defineProperty(newObj, key, descriptor); | ||
} | ||
else if (descriptor.enumerable) { | ||
newObj[key] = clone(obj[key], options, seen); | ||
} | ||
else { | ||
Object.defineProperty(newObj, key, { enumerable: false, writable: true, configurable: true, value: clone(obj[key], options, seen) }); | ||
} | ||
} | ||
else { | ||
Object.defineProperty(newObj, key, { | ||
enumerable: descriptor ? descriptor.enumerable : true, | ||
enumerable: true, | ||
writable: true, | ||
@@ -127,6 +112,2 @@ configurable: true, | ||
if (baseProto === Types.array) { | ||
newObj.length = obj.length; | ||
} | ||
return newObj; | ||
@@ -147,1 +128,35 @@ }; | ||
}; | ||
internals.base = function (obj, baseProto, options) { | ||
if (baseProto === Types.array) { | ||
return []; | ||
} | ||
if (options.prototype === false) { // Defaults to true | ||
if (internals.needsProtoHack.has(baseProto)) { | ||
return new baseProto.constructor(); | ||
} | ||
return {}; | ||
} | ||
const proto = Object.getPrototypeOf(obj); | ||
if (proto && | ||
proto.isImmutable) { | ||
return obj; | ||
} | ||
if (internals.needsProtoHack.has(baseProto)) { | ||
const newObj = new proto.constructor(); | ||
if (proto !== baseProto) { | ||
Object.setPrototypeOf(newObj, proto); | ||
} | ||
return newObj; | ||
} | ||
return Object.create(proto); | ||
}; |
{ | ||
"name": "@hapi/hoek", | ||
"description": "General purpose node utilities", | ||
"version": "8.3.1", | ||
"version": "8.3.2", | ||
"repository": "git://github.com/hapijs/hoek", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49835
1423