object-lib
Advanced tools
Comparing version 2.0.0 to 2.0.1
"use strict"; | ||
const contains = (haystack, needle) => { | ||
const needleType = typeof needle; | ||
const haystackType = typeof haystack; | ||
const objectScan = require('object-scan'); | ||
if (needleType !== haystackType) { | ||
return false; | ||
} | ||
const scanner = objectScan(['**'], { | ||
rtn: 'context', | ||
abort: true, | ||
breakFn: ({ | ||
isLeaf, | ||
isMatch, | ||
property, | ||
value, | ||
context | ||
}) => { | ||
const { | ||
stack | ||
} = context; | ||
const last = stack[stack.length - 1]; | ||
if (needleType === 'object') { | ||
const needleIsArray = Array.isArray(needle); | ||
const haystackIsArray = Array.isArray(haystack); | ||
if (isMatch && !(property in last)) { | ||
context.result = false; | ||
return true; | ||
} | ||
if (needleIsArray !== haystackIsArray) { | ||
return false; | ||
} // exact match for arrays | ||
const current = isMatch ? last[property] : last; | ||
if (needleIsArray) { | ||
if (needle.length !== haystack.length) { | ||
return false; | ||
if (isLeaf) { | ||
if (value !== current) { | ||
context.result = false; | ||
return true; | ||
} | ||
} else if (value instanceof Object !== current instanceof Object || Array.isArray(value) !== Array.isArray(current) || Array.isArray(value) && value.length !== current.length) { | ||
context.result = false; | ||
return true; | ||
} | ||
return needle.every((e, idx) => contains(haystack[idx], e)); | ||
} // subset match for object | ||
stack.push(current); | ||
return false; | ||
}, | ||
filterFn: ({ | ||
context | ||
}) => { | ||
context.stack.pop(); | ||
return context.result !== true; | ||
} | ||
}); | ||
return Object.keys(needle).every(key => contains(haystack[key], needle[key])); | ||
} // default comparison | ||
return haystack === needle; | ||
}; | ||
module.exports = contains; | ||
module.exports = (tree, subtree) => { | ||
const { | ||
result | ||
} = scanner(subtree, { | ||
stack: [tree], | ||
result: true | ||
}); | ||
return result; | ||
}; |
{ | ||
"name": "object-lib", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "repository": { |
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
11364
164