object-scan
Advanced tools
Comparing version 14.4.4 to 15.0.0
@@ -227,3 +227,3 @@ "use strict"; | ||
setValues(obj, Object.entries(obj).filter(([k]) => k !== '').map(e => e[1]).reverse()); | ||
setValues(obj, Object.values(obj).reverse()); | ||
lastDepth = depth; | ||
@@ -230,0 +230,0 @@ } |
@@ -16,3 +16,12 @@ "use strict"; | ||
module.exports = (haystack_, searches_, ctx) => { | ||
const root = ctx.beforeFn === undefined ? haystack_ : ctx.beforeFn(haystack_, ctx.context); | ||
const state = { | ||
haystack: haystack_, | ||
context: ctx.context | ||
}; | ||
if (ctx.beforeFn !== undefined) { | ||
const r = ctx.beforeFn(state); | ||
assert(r === undefined, 'beforeFn must not return'); | ||
} | ||
const stack = [false, searches_, null, 0]; | ||
@@ -25,3 +34,3 @@ const path = []; | ||
let isMatch; | ||
let haystack = root; | ||
let haystack = state.haystack; | ||
const kwargs = { | ||
@@ -70,2 +79,8 @@ getKey: () => formatPath(path, ctx), | ||
getGproperty: () => path[path.length - 2], | ||
get gproperty() { | ||
return kwargs.getGproperty(); | ||
}, | ||
getProperty: () => path[path.length - 1], | ||
@@ -77,2 +92,8 @@ | ||
getGparent: () => parents[parents.length - 2], | ||
get gparent() { | ||
return kwargs.getGparent(); | ||
}, | ||
getParent: () => parents[parents.length - 1], | ||
@@ -113,3 +134,3 @@ | ||
context: ctx.context | ||
context: state.context | ||
}; | ||
@@ -120,2 +141,6 @@ const result = Result(kwargs, ctx); | ||
if ('' in searches_[0] && (ctx.useArraySelector || !Array.isArray(state.haystack))) { | ||
stack[1] = [...stack[1], searches_[0]['']]; | ||
} | ||
do { | ||
@@ -141,3 +166,3 @@ depth = stack.pop(); | ||
} else { | ||
haystack = root; | ||
haystack = state.haystack; | ||
} | ||
@@ -166,14 +191,6 @@ | ||
const autoTraverseArray = ctx.useArraySelector === false && Array.isArray(haystack); | ||
const searchesIn = searches; | ||
if (!autoTraverseArray) { | ||
if (compiler.isLastLeafMatch(searches)) { | ||
stack.push(true, searches, segment, depth); | ||
isMatch = true; | ||
} else if ('' in searches[0]) { | ||
assert(searches.length === 1); | ||
stack.push(true, [searches[0]['']], segment, depth); | ||
isMatch = true; | ||
searches = [searches[0]['']]; | ||
} | ||
if (!autoTraverseArray && compiler.isLastLeafMatch(searches)) { | ||
stack.push(true, searches, segment, depth); | ||
isMatch = true; | ||
} | ||
@@ -198,6 +215,10 @@ | ||
if (autoTraverseArray) { | ||
searchesOut.push(...searchesIn); | ||
searchesOut.push(...searches); | ||
if ('' in searches[0]) { | ||
searchesOut.push(searches[0]['']); | ||
} | ||
} else { | ||
for (let sIdx = 0, sLen = searchesIn.length; sIdx !== sLen; sIdx += 1) { | ||
const search = searchesIn[sIdx]; | ||
for (let sIdx = 0, sLen = searches.length; sIdx !== sLen; sIdx += 1) { | ||
const search = searches[sIdx]; | ||
@@ -226,3 +247,10 @@ if (compiler.getWildcard(search).anyMatch(key)) { | ||
return ctx.afterFn === undefined ? result.get() : ctx.afterFn(result.get(), ctx.context); | ||
state.result = result.get(); | ||
if (ctx.afterFn !== undefined) { | ||
const r = ctx.afterFn(state); | ||
assert(r === undefined, 'afterFn must not return'); | ||
} | ||
return state.result; | ||
}; |
@@ -39,3 +39,3 @@ "use strict"; | ||
assert(typeof ctx.abort === 'boolean'); | ||
assert([undefined, 'context', 'key', 'value', 'entry', 'property', 'parent', 'parents', 'isMatch', 'matchedBy', 'excludedBy', 'traversedBy', 'isCircular', 'isLeaf', 'depth', 'bool', 'count'].includes(opts.rtn) || Array.isArray(opts.rtn) && opts.rtn.every(e => ['key', 'value', 'entry', 'property', 'parent', 'parents', 'isMatch', 'matchedBy', 'excludedBy', 'traversedBy', 'isCircular', 'isLeaf', 'depth'].includes(e))); | ||
assert([undefined, 'context', 'key', 'value', 'entry', 'property', 'gproperty', 'parent', 'gparent', 'parents', 'isMatch', 'matchedBy', 'excludedBy', 'traversedBy', 'isCircular', 'isLeaf', 'depth', 'bool', 'count'].includes(opts.rtn) || Array.isArray(opts.rtn) && opts.rtn.every(e => ['key', 'value', 'entry', 'property', 'gproperty', 'parent', 'gparent', 'parents', 'isMatch', 'matchedBy', 'excludedBy', 'traversedBy', 'isCircular', 'isLeaf', 'depth'].includes(e))); | ||
assert(typeof ctx.joined === 'boolean'); | ||
@@ -42,0 +42,0 @@ assert(typeof ctx.useArraySelector === 'boolean'); |
{ | ||
"name": "object-scan", | ||
"version": "14.4.4", | ||
"version": "15.0.0", | ||
"description": "Traverse object hierarchies using matching and callbacks.", | ||
@@ -74,3 +74,3 @@ "main": "lib/index.js", | ||
"nyc": "15.1.0", | ||
"object-scan": "14.4.2", | ||
"object-scan": "14.4.4", | ||
"semantic-release": "17.4.2", | ||
@@ -77,0 +77,0 @@ "smart-fs": "2.0.2", |
@@ -323,3 +323,5 @@ # Object-Scan | ||
- `property`: current parent property. | ||
- `gproperty`: current grandparent property. | ||
- `parent`: current parent. | ||
- `gparent`: current grandparent. | ||
- `parents`: array of form `[parent, grandparent, ...]`. | ||
@@ -338,3 +340,5 @@ - `isMatch`: true iff last targeting needle exists and is non-excluding. | ||
- `getProperty`: function that returns `property` | ||
- `getGproperty`: function that returns `gproperty` | ||
- `getParent`: function that returns `parent` | ||
- `getGparent`: function that returns `gparent` | ||
- `getParents`: function that returns `parents` | ||
@@ -418,4 +422,4 @@ - `getIsMatch`: function that returns `isMatch` | ||
When defined, this function is called before traversal as `beforeFn(haystack, context)` | ||
and the return value is then traversed. | ||
When defined, this function is called before traversal as `beforeFn(state = { haystack, context })` | ||
and `state.haystack` is then traversed using `state.context`. | ||
@@ -430,3 +434,3 @@ _Examples_: | ||
joined: true, | ||
beforeFn: (hs, context) => [hs, context], | ||
beforeFn: (state) => { /* eslint-disable no-param-reassign */ state.haystack = [state.haystack, state.context]; }, | ||
rtn: 'key' | ||
@@ -443,4 +447,4 @@ })(haystack, { b: 0 }); | ||
When defined, this function is called after traversal as `afterFn(result, context)` | ||
and the return value is returned from the search invocation. | ||
When defined, this function is called after traversal as `afterFn(state = { result, haystack, context })` | ||
and `state.result` is then returned from the search invocation. | ||
@@ -454,3 +458,3 @@ _Examples_: | ||
objectScan(['**'], { | ||
afterFn: (result, context) => result + context, | ||
afterFn: (state) => { /* eslint-disable no-param-reassign */ state.result += state.context; }, | ||
rtn: 'count' | ||
@@ -592,3 +596,5 @@ })(haystack, 5); | ||
- `property`: as passed into `filterFn` | ||
- `gproperty`: as passed into `filterFn` | ||
- `parent`: as passed into `filterFn` | ||
- `gparent`: as passed into `filterFn` | ||
- `parents`: as passed into `filterFn` | ||
@@ -605,3 +611,3 @@ - `isMatch`: as passed into `filterFn` | ||
Or, when set as an `array`, can contain any of: `key`, `value`, `entry`, `property`, `parent`, `parents`, `isMatch`, `matchedBy`, `excludedBy`, `traversedBy`, `isCircular`, `isLeaf`, `depth` | ||
Or, when set as an `array`, can contain any of the above except `context`, `bool` and `count`. | ||
@@ -608,0 +614,0 @@ |
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
60304
858
988