@hapi/hoek
Advanced tools
Comparing version 6.2.3 to 6.2.4
@@ -5,34 +5,12 @@ 'use strict'; | ||
const Types = require('./types'); | ||
// Declare internals | ||
const internals = { | ||
arrayType: Symbol('array'), | ||
bufferType: Symbol('buffer'), | ||
dateType: Symbol('date'), | ||
errorType: Symbol('error'), | ||
genericType: Symbol('generic'), | ||
mapType: Symbol('map'), | ||
promiseType: Symbol('promise'), | ||
regexType: Symbol('regex'), | ||
setType: Symbol('set'), | ||
weakMapType: Symbol('weak-map'), | ||
weakSetType: Symbol('weak-set'), | ||
mismatched: Symbol('mismatched') | ||
mismatched: null | ||
}; | ||
internals.typeMap = { | ||
'[object Array]': internals.arrayType, | ||
'[object Date]': internals.dateType, | ||
'[object Error]': internals.errorType, | ||
'[object Map]': internals.mapType, | ||
'[object Promise]': internals.promiseType, | ||
'[object RegExp]': internals.regexType, | ||
'[object Set]': internals.setType, | ||
'[object WeakMap]': internals.weakMapType, | ||
'[object WeakSet]': internals.weakSetType | ||
}; | ||
module.exports = function (obj, ref, options) { | ||
@@ -67,7 +45,7 @@ | ||
switch (instanceType) { | ||
case internals.bufferType: | ||
case Types.buffer: | ||
return Buffer.prototype.equals.call(obj, ref); | ||
case internals.promiseType: | ||
case Types.promise: | ||
return obj === ref; | ||
case internals.regexType: | ||
case Types.regex: | ||
return obj.toString() === ref.toString(); | ||
@@ -95,15 +73,2 @@ case internals.mismatched: | ||
internals.getInternalType = function (obj) { | ||
const { typeMap, bufferType, genericType } = internals; | ||
if (obj instanceof Buffer) { | ||
return bufferType; | ||
} | ||
const objName = Object.prototype.toString.call(obj); | ||
return typeMap[objName] || genericType; | ||
}; | ||
internals.getSharedType = function (obj, ref, checkPrototype) { | ||
@@ -116,7 +81,7 @@ | ||
return internals.getInternalType(obj); | ||
return Types.getInternalProto(obj); | ||
} | ||
const type = internals.getInternalType(obj); | ||
if (type !== internals.getInternalType(ref)) { | ||
const type = Types.getInternalProto(obj); | ||
if (type !== Types.getInternalProto(ref)) { | ||
return internals.mismatched; | ||
@@ -168,3 +133,3 @@ } | ||
if (instanceType === internals.arrayType) { | ||
if (instanceType === Types.array) { | ||
if (options.part) { | ||
@@ -196,3 +161,3 @@ // Check if any index match any other index | ||
} | ||
else if (instanceType === internals.setType) { | ||
else if (instanceType === Types.set) { | ||
if (obj.size !== ref.size) { | ||
@@ -227,3 +192,3 @@ return false; | ||
} | ||
else if (instanceType === internals.mapType) { | ||
else if (instanceType === Types.map) { | ||
if (obj.size !== ref.size) { | ||
@@ -243,3 +208,3 @@ return false; | ||
} | ||
else if (instanceType === internals.errorType) { | ||
else if (instanceType === Types.error) { | ||
// Always check name and message | ||
@@ -246,0 +211,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
@param obj - The value being compared. | ||
@param ref - The reference va;ue used for comparison. | ||
@param ref - The reference value used for comparison. | ||
@@ -18,3 +18,3 @@ @return true when the two values are equal, otherwise false. | ||
Allow partial match. | ||
@default false | ||
@@ -26,3 +26,3 @@ */ | ||
Compare the objects' prototypes. | ||
@default true | ||
@@ -34,3 +34,3 @@ */ | ||
Compare symbol properties. | ||
@default false | ||
@@ -59,3 +59,3 @@ */ | ||
Clone the object's prototype. | ||
@default true | ||
@@ -67,3 +67,3 @@ */ | ||
Include symbol properties. | ||
@default false | ||
@@ -133,8 +133,8 @@ */ | ||
*/ | ||
export function intersect(array1: intersect.Array, array2: intersect.Array, justFirst?: false): any[]; | ||
export function intersect(array1: intersect.Array, array2: intersect.Array, justFirst: true): any; | ||
export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, justFirst?: false): Array<T1 | T2>; | ||
export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, justFirst: true): T1 | T2; | ||
declare namespace intersect { | ||
type Array = any[] | Set<any> | null; | ||
type Array<T> = ArrayLike<T> | Set<T> | null; | ||
} | ||
@@ -161,3 +161,3 @@ | ||
Perform a deep comparison. | ||
@default false | ||
@@ -169,3 +169,3 @@ */ | ||
Allow only one occurrence of each value. | ||
@default false | ||
@@ -177,3 +177,3 @@ */ | ||
Allow only values explicitly listed. | ||
@default false | ||
@@ -185,3 +185,3 @@ */ | ||
Allow partial match. | ||
@default false | ||
@@ -193,3 +193,3 @@ */ | ||
Include symbol properties. | ||
@default false | ||
@@ -210,3 +210,3 @@ */ | ||
*/ | ||
export function flatten(array: any[], target?: any[]): any[]; | ||
export function flatten<T>(array: ArrayLike<T | ReadonlyArray<T>>, target?: ArrayLike<T | ReadonlyArray<T>>): T[]; | ||
@@ -230,3 +230,3 @@ | ||
String to split chain path on. Defaults to '.'. | ||
@default false | ||
@@ -238,3 +238,3 @@ */ | ||
Value to return if the path or value is not present. No default value. | ||
@default false | ||
@@ -246,3 +246,3 @@ */ | ||
If true, will throw an error on missing member in the chain. Default to false. | ||
@default false | ||
@@ -254,3 +254,3 @@ */ | ||
If true, allows traversing functions for properties. false will throw an error if a function is part of the chain. | ||
@default false | ||
@@ -364,3 +364,3 @@ */ | ||
*/ | ||
export function once(method: () => any): () => void; | ||
export function once<T extends Function>(method: T): T; | ||
@@ -409,5 +409,3 @@ | ||
Returns a Promise that never resolves. | ||
@return A Promise. | ||
*/ | ||
export function block(): Promise<void>; |
127
lib/index.js
@@ -11,2 +11,3 @@ 'use strict'; | ||
const Escape = require('./escape'); | ||
const Types = require('./types'); | ||
@@ -16,3 +17,5 @@ | ||
const internals = {}; | ||
const internals = { | ||
needsProtoHack: new Set([Types.set, Types.map, Types.weakSet, Types.weakMap]) | ||
}; | ||
@@ -42,33 +45,21 @@ | ||
const baseProto = Types.getInternalProto(obj); | ||
let newObj; | ||
let cloneDeep = false; | ||
const isArray = Array.isArray(obj); | ||
if (!isArray) { | ||
if (Buffer.isBuffer(obj)) { | ||
newObj = Buffer.from(obj); | ||
} | ||
else if (obj instanceof Date) { | ||
newObj = new Date(obj.getTime()); | ||
} | ||
else if (obj instanceof RegExp) { | ||
newObj = new RegExp(obj); | ||
} | ||
else if (obj instanceof Set) { | ||
newObj = new Set(); | ||
cloneDeep = true; | ||
for (const val of obj) { | ||
newObj.add(exports.clone(val)); | ||
} | ||
} | ||
else if (obj instanceof Map) { | ||
newObj = new Map(); | ||
cloneDeep = true; | ||
for (let [key, value] of obj) { | ||
value = exports.clone(value); | ||
newObj.set(key, value); | ||
} | ||
} | ||
else { | ||
if (options.prototype !== false) { // Defaults to true | ||
switch (baseProto) { | ||
case Types.buffer: | ||
return Buffer.from(obj); | ||
case Types.date: | ||
return new Date(obj.getTime()); | ||
case Types.regex: | ||
return new RegExp(obj); | ||
case Types.array: | ||
newObj = []; | ||
break; | ||
default: | ||
if (options.prototype !== false) { // Defaults to true | ||
const proto = Object.getPrototypeOf(obj); | ||
@@ -78,53 +69,67 @@ if (proto && | ||
newObj = obj; | ||
return obj; | ||
} | ||
if (internals.needsProtoHack.has(baseProto)) { | ||
newObj = new proto.constructor(); | ||
if (proto !== baseProto) { | ||
Object.setPrototypeOf(newObj, proto); | ||
} | ||
} | ||
else { | ||
newObj = Object.create(proto); | ||
cloneDeep = true; | ||
} | ||
} | ||
else if (internals.needsProtoHack.has(baseProto)) { | ||
newObj = new baseProto.constructor(); | ||
} | ||
else { | ||
newObj = {}; | ||
cloneDeep = true; | ||
} | ||
} | ||
seen.set(obj, newObj); // Set seen, since obj could recurse | ||
if (baseProto === Types.set) { | ||
for (const value of obj) { | ||
newObj.add(exports.clone(value, options, seen)); | ||
} | ||
} | ||
else { | ||
newObj = []; | ||
cloneDeep = true; | ||
else if (baseProto === Types.map) { | ||
for (const [key, value] of obj) { | ||
newObj.set(key, exports.clone(value, options, seen)); | ||
} | ||
} | ||
seen.set(obj, newObj); | ||
const keys = internals.keys(obj, options); | ||
for (let i = 0; i < keys.length; ++i) { | ||
const key = keys[i]; | ||
if (cloneDeep) { | ||
const keys = internals.keys(obj, options); | ||
for (let i = 0; i < keys.length; ++i) { | ||
const key = keys[i]; | ||
if (baseProto === Types.array && | ||
key === 'length') { | ||
if (isArray && key === 'length') { | ||
continue; | ||
} | ||
continue; | ||
} | ||
const descriptor = Object.getOwnPropertyDescriptor(obj, key); | ||
if (descriptor && | ||
(descriptor.get || | ||
descriptor.set)) { | ||
const descriptor = Object.getOwnPropertyDescriptor(obj, key); | ||
if (descriptor && | ||
(descriptor.get || | ||
descriptor.set)) { | ||
Object.defineProperty(newObj, key, descriptor); | ||
} | ||
else { | ||
Object.defineProperty(newObj, key, { | ||
enumerable: descriptor ? descriptor.enumerable : true, | ||
writable: true, | ||
configurable: true, | ||
value: exports.clone(obj[key], options, seen) | ||
}); | ||
} | ||
Object.defineProperty(newObj, key, descriptor); | ||
} | ||
if (isArray) { | ||
newObj.length = obj.length; | ||
else { | ||
Object.defineProperty(newObj, key, { | ||
enumerable: descriptor ? descriptor.enumerable : true, | ||
writable: true, | ||
configurable: true, | ||
value: exports.clone(obj[key], options, seen) | ||
}); | ||
} | ||
} | ||
if (baseProto === Types.array) { | ||
newObj.length = obj.length; | ||
} | ||
return newObj; | ||
@@ -131,0 +136,0 @@ }; |
{ | ||
"name": "@hapi/hoek", | ||
"description": "General purpose node utilities", | ||
"version": "6.2.3", | ||
"version": "6.2.4", | ||
"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
9
1108
43083