Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-scan

Package Overview
Dependencies
Maintainers
1
Versions
200
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-scan - npm Package Compare versions

Comparing version 18.3.0 to 18.3.1

lib/core/find-util.js

14

lib/core/compiler-iterator.js

@@ -7,5 +7,5 @@ import iterator from '../generic/iterator.js';

iterator(tree, (type, wc) => {
iterator(tree, (type, v) => {
if (type === 'RM') {
if (wc.excluded === true) {
if (v.excluded === true) {
excluded = false;

@@ -15,3 +15,3 @@ }

} else if (type === 'ADD') {
if (wc.excluded === true) {
if (v.excluded === true) {
if (excluded) {

@@ -23,12 +23,12 @@ throw new Error(`Redundant Exclusion: "${needle}"`);

const toAdd = [];
const wcParent = stack[stack.length - 2];
const vParent = stack[stack.length - 2];
stack[stack.length - 1]
.forEach(([cur, parent]) => onAdd(cur, parent, wc, wcParent, (e) => toAdd.push([e, cur])));
stack.push(wc, toAdd);
.forEach(([cur, parent]) => onAdd(cur, parent, v, vParent, (e) => toAdd.push([e, cur])));
stack.push(v, toAdd);
} else {
stack[stack.length - 1]
.filter(([cur]) => cur !== tower)
.forEach(([cur, parent]) => onFin(cur, parent, wc[wc.length - 1], excluded));
.forEach(([cur, parent]) => onFin(cur, parent, v[v.length - 1], excluded));
}
});
};
/* compile needles to hierarchical map object */
import parser from './parser.js';
import iterator from './compiler-iterator.js';
import { defineProperty } from '../generic/helper.js';
import { Wildcard } from './wildcard.js';
import { Ref } from './ref.js';
import { Ref } from './parser-ref.js';
import { Node } from './node.js';
const LEAF = Symbol('leaf');
const markLeaf = (input, match, readonly) => defineProperty(input, LEAF, match, readonly);
export const isLeaf = (input) => LEAF in input;
export const isMatch = (input) => input !== undefined && input[LEAF] === true;
const ROOTS = Symbol('roots');
const setRoots = (input, roots) => defineProperty(input, ROOTS, roots);
export const getRoots = (input) => input[ROOTS];
const HAS_MATCHES = Symbol('has-matches');
const setHasMatches = (input) => defineProperty(input, HAS_MATCHES, true);
export const hasMatches = (input) => input[HAS_MATCHES] === true;
const merge = (input, symbol, ...values) => {
const target = input[symbol];
if (target === undefined) {
defineProperty(input, symbol, values);
} else {
target.push(...values.filter((v) => !target.includes(v)));
}
};
const LEAF_NEEDLES = Symbol('leaf-needles');
const addLeafNeedle = (input, needle) => merge(input, LEAF_NEEDLES, needle);
export const getLeafNeedles = (input) => input[LEAF_NEEDLES] || [];
const LEAF_NEEDLES_EXCLUDE = Symbol('leaf-needles-exclude');
const addLeafNeedleExclude = (input, needle) => merge(input, LEAF_NEEDLES_EXCLUDE, needle);
export const getLeafNeedlesExclude = (input) => input[LEAF_NEEDLES_EXCLUDE] || [];
const LEAF_NEEDLES_MATCH = Symbol('leaf-needles-match');
const addLeafNeedleMatch = (input, needle) => merge(input, LEAF_NEEDLES_MATCH, needle);
export const getLeafNeedlesMatch = (input) => input[LEAF_NEEDLES_MATCH] || [];
const NEEDLES = Symbol('needles');
const addNeedle = (input, needle) => merge(input, NEEDLES, needle);
export const getNeedles = (input) => input[NEEDLES];
const INDEX = Symbol('index');
const setIndex = (input, index, readonly) => defineProperty(input, INDEX, index, readonly);
export const getIndex = (input) => input[INDEX];
const ORDER = Symbol('order');
const setOrder = (input, order) => defineProperty(input, ORDER, order);
export const getOrder = (input) => input[ORDER];
const WILDCARD = Symbol('wildcard');
const setWildcard = (input, wildcard) => defineProperty(input, WILDCARD, wildcard);
export const getWildcard = (input) => input[WILDCARD];
const VALUES = Symbol('values');
const setValues = (input, entries) => defineProperty(input, VALUES, entries);
const addValues = (input, values) => merge(input, VALUES, ...values);
export const getValues = (input) => input[VALUES];
export const matchedBy = (searches) => Array
.from(new Set(searches.flatMap((e) => getLeafNeedlesMatch(e))));
export const excludedBy = (searches) => Array
.from(new Set(searches.flatMap((e) => getLeafNeedlesExclude(e))));
export const traversedBy = (searches) => Array
.from(new Set(searches.flatMap((e) => getNeedles(e))));
export const isLastLeafMatch = (searches) => {
let maxLeafIndex = Number.MIN_SAFE_INTEGER;
let maxLeaf = null;
for (let idx = 0, len = searches.length; idx < len; idx += 1) {
const s = searches[idx];
const index = getIndex(s);
if (index !== undefined && index > maxLeafIndex) {
maxLeafIndex = index;
maxLeaf = s;
}
}
return maxLeaf !== null && isMatch(maxLeaf);
};
const applyNeedle = (tower, needle, tree, ctx) => {
iterator(tower, needle, tree, {
onAdd: (cur, parent, wc, wcParent, next) => {
addNeedle(cur, needle);
if (wc instanceof Ref) {
if (wc.left === true) {
if (wc.isStarRec) {
wc.setPointer(cur);
onAdd: (cur, parent, v, vParent, next) => {
cur.addNeedle(needle);
if (v instanceof Ref) {
if (v.left === true) {
if (v.isStarRec) {
v.setPointer(cur);
}
wc.setNode({});
ctx.stack.push(cur, wc.node, true);
next(wc.node);
v.setNode(new Node('*', ctx));
ctx.links.push(cur, v.node);
next(v.node);
} else {
// eslint-disable-next-line no-param-reassign
wc.target = 'target' in wcParent ? wcParent.target : parent[wcParent.value];
ctx.stack.push(wc.target, wc.node, true);
if (wc.pointer !== null) {
next(wc.pointer);
wc.setPointer(null);
v.target = 'target' in vParent ? vParent.target : parent.get(vParent.value);
ctx.links.push(v.target, v.node);
if (v.pointer !== null) {
next(v.pointer);
v.setPointer(null);
}

@@ -109,5 +32,4 @@ next(cur);

const redundantRecursion = (
wcParent !== undefined
&& wc.isStarRec
&& wc.value === wcParent.value
v.isStarRec
&& v.value === vParent?.value
);

@@ -118,40 +40,27 @@ if (redundantRecursion && ctx.strict) {

if (!redundantRecursion) {
if (!(wc.value in cur)) {
const child = {};
// eslint-disable-next-line no-param-reassign
cur[wc.value] = child;
ctx.stack.push(cur, child, false);
if (ctx.orderByNeedles) {
setOrder(child, ctx.counter);
}
setWildcard(child, wc);
let node = cur.get(v.value);
if (node === undefined) {
node = new Node(v.value, ctx);
cur.add(node);
}
next(cur[wc.value]);
next(node);
} else {
// eslint-disable-next-line no-param-reassign
wc.target = cur;
v.target = cur;
}
if (wc.isStarRec) {
if (v.isStarRec) {
next(cur);
}
},
onFin: (cur, parent, wc, excluded) => {
if (ctx.strict && wc.isSimpleStarRec) {
const unnecessary = Object.keys(parent).filter((k) => !['**', ''].includes(k));
onFin: (cur, parent, v, excluded) => {
if (ctx.strict && v.isSimpleStarRec) {
const unnecessary = parent.children.filter(({ value }) => !['', '**'].includes(value));
if (unnecessary.length !== 0) {
throw new Error(`Needle Target Invalidated: "${parent[unnecessary[0]][NEEDLES][0]}" by "${needle}"`);
throw new Error(`Needle Target Invalidated: "${unnecessary[0].needles[0]}" by "${needle}"`);
}
}
addNeedle(cur, needle);
if (ctx.strict && LEAF_NEEDLES in cur) {
throw new Error(`Redundant Needle Target: "${cur[LEAF_NEEDLES][0]}" vs "${needle}"`);
if (ctx.strict && cur.leafNeedles.length !== 0) {
throw new Error(`Redundant Needle Target: "${cur.leafNeedles[0]}" vs "${needle}"`);
}
addLeafNeedle(cur, needle, ctx.strict);
if (excluded) {
addLeafNeedleExclude(cur, needle);
} else {
addLeafNeedleMatch(cur, needle);
}
markLeaf(cur, !excluded, ctx.strict);
setIndex(cur, ctx.counter, ctx.strict);
cur.finish(needle, excluded, ctx.counter);
ctx.counter += 1;

@@ -163,37 +72,35 @@ }

const finalizeTower = (tower, ctx) => {
const { stack } = ctx;
const links = [];
while (stack.length !== 0) {
const link = stack.pop();
const child = stack.pop();
const parent = stack.pop();
const { links } = ctx;
while (links.length !== 0) {
const child = links.pop();
const parent = links.pop();
const { children } = parent;
parent.children = [...child.children.filter((c) => !children.includes(c)), ...children];
}
if (!(VALUES in child)) {
setValues(child, Object.values(child).reverse());
// todo: uncomment and remove below for ordering fix
// if (ctx.useArraySelector === false) {
// tower.setRoots(tower.children
// .filter(({ isStarRec, value }) => isStarRec || value === ''));
// }
const { nodes } = ctx;
while (nodes.length !== 0) {
const node = nodes.pop();
const { children } = node;
children.reverse();
if (children.some(({ matches }) => matches)) {
node.markMatches();
}
if (link) {
links.push(parent, child);
}
if (isMatch(child)) {
setHasMatches(child);
}
if (hasMatches(child) && !hasMatches(parent)) {
setHasMatches(parent);
}
}
setValues(tower, Object.values(tower).reverse());
for (let idx = 0, len = links.length; idx < len; idx += 2) {
const parent = links[idx];
const child = links[idx + 1];
addValues(parent, getValues(child));
}
// todo: replace with commented code above
if (ctx.useArraySelector === false) {
const roots = [];
if ('' in tower) {
roots.push(tower['']);
const child = tower.get('');
if (child !== undefined) {
roots.push(child);
}
roots.push(...getValues(tower).filter((e) => getWildcard(e).isStarRec));
setRoots(tower, roots);
roots.push(...tower.children.filter(({ isStarRec }) => isStarRec));
tower.setRoots(roots);
}

@@ -203,5 +110,7 @@ };

export const compile = (needles, ctx) => {
const tower = {};
ctx.counter = 0;
ctx.stack = [];
ctx.links = [];
ctx.nodes = [];
ctx.regex = {};
const tower = new Node('*', ctx);
for (let idx = 0; idx < needles.length; idx += 1) {

@@ -212,5 +121,4 @@ const needle = needles[idx];

}
setWildcard(tower, new Wildcard('*', false));
finalizeTower(tower, ctx);
return tower;
};
import {
getWildcard, excludedBy, traversedBy,
hasMatches, matchedBy, isLastLeafMatch,
getValues, getOrder, getRoots
} from './compiler.js';
excludedBy, traversedBy, matchedBy, isLastLeafMatch, formatPath
} from './find-util.js';
import Result from './find-result.js';
import { toPath } from '../generic/helper.js';
const formatPath = (input, ctx) => (ctx.joined ? toPath(input) : [...input]);
export default (haystack_, searches_, ctx) => {
export default (haystack_, search_, ctx) => {
const state = {

@@ -22,3 +17,3 @@ haystack: haystack_,

}
const stack = [false, searches_, null, 0];
const stack = [false, [search_], null, 0];
const path = [];

@@ -104,4 +99,7 @@ const parents = [];

if ('' in searches_[0] && (ctx.useArraySelector || !Array.isArray(state.haystack))) {
stack[1] = [...stack[1], searches_[0]['']];
if (ctx.useArraySelector || !Array.isArray(state.haystack)) {
const child = search_.get('');
if (child !== undefined) {
stack[1].push(child);
}
}

@@ -142,3 +140,3 @@

if (!searches.some((s) => hasMatches(s))) {
if (!searches.some(({ matches }) => matches)) {
// eslint-disable-next-line no-continue

@@ -173,3 +171,3 @@ continue;

if (depth === 0) {
searchesOut.push(...getRoots(searches[0]));
searchesOut.push(...search_.roots);
}

@@ -179,12 +177,12 @@ } else {

const search = searches[sIdx];
if (getWildcard(search).recMatch(key)) {
if (search.recMatch(key)) {
searchesOut.push(search);
}
const values = getValues(search);
let eIdx = values.length;
const { children } = search;
let eIdx = children.length;
// eslint-disable-next-line no-plusplus
while (eIdx--) {
const value = values[eIdx];
if (getWildcard(value).typeMatch(key, isArray)) {
searchesOut.push(value);
const child = children[eIdx];
if (child.typeMatch(key, isArray)) {
searchesOut.push(child);
}

@@ -195,3 +193,3 @@ }

if (ctx.orderByNeedles) {
searchesOut.index = Buffer.from(searchesOut.map((e) => getOrder(e)).sort());
searchesOut.index = Buffer.from(searchesOut.map(({ order }) => order).sort());
let checkIdx = stack.length - 3;

@@ -198,0 +196,0 @@ const checkIdxMin = checkIdx - kIdx * 4;

import assert from '../generic/assert.js';
import { defineProperty } from '../generic/helper.js';
import { Wildcard } from './wildcard.js';
import { Ref } from './ref.js';
import { Value } from './parser-value.js';
import { Ref } from './parser-ref.js';
const IS_EXCLUDED = Symbol('is-excluded');
const markExcluded = (input) => defineProperty(input, IS_EXCLUDED, true);
const isExcluded = (input) => input[IS_EXCLUDED] === true;
const throwError = (msg, input, context = {}) => {

@@ -22,2 +17,4 @@ throw new Error(Object.entries(context)

const arraySelectorRegex = /^[?*+\d]+$/;
export default (input) => {

@@ -32,3 +29,3 @@ let cResult = new Set();

const newChild = (asOr) => {
if (isExcluded(cResult)) {
if (cResult.excluded === true) {
assert(excludeNext === false);

@@ -77,3 +74,3 @@ excludeNext = true;

if (inArray && !(
/^[?*+\d]+$/.test(ele)
arraySelectorRegex.test(ele)
|| (ele.startsWith('(') && ele.endsWith(')'))

@@ -86,3 +83,3 @@ )) {

} else {
cResult.push(new Wildcard(inArray ? `[${ele}]` : ele, excludeNext));
cResult.push(new Value(inArray ? `[${ele}]` : ele, excludeNext));
excludeNext = false;

@@ -102,3 +99,3 @@ }

if (excludeNext) {
markExcluded(cResult);
cResult.excluded = true;
excludeNext = false;

@@ -105,0 +102,0 @@ }

@@ -1,2 +0,2 @@

import { Wildcard } from './wildcard.js';
import { Value } from './parser-value.js';
import Result from './parser-result.js';

@@ -11,3 +11,3 @@

if (input === '') {
return new Wildcard('', false);
return new Value('', false);
}

@@ -14,0 +14,0 @@

@@ -1,10 +0,24 @@

export const defineProperty = (target, k, v, readonly = true) => Object
.defineProperty(target, k, { value: v, writable: !readonly });
const specialChars = /[?!,.*+[\](){}\\]/g;
export const escape = (input) => input.replace(specialChars, '\\$&');
export const escapeRegex = (char) => char.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
const regex = /^\^?[^-/\\^$*+?.()|[\]{}]*\$?$/g;
export const asRegex = (regexStr) => {
if (regex.test(regexStr)) {
const start = regexStr.startsWith('^');
const end = regexStr.endsWith('$');
if (start && end) {
const value = regexStr.slice(1, -1);
return { test: (v) => v === value };
}
if (start) {
const value = regexStr.slice(1);
return { test: (v) => v.startsWith(value) };
}
if (end) {
const value = regexStr.slice(0, -1);
return { test: (v) => v.endsWith(value) };
}
return { test: (v) => v.includes(regexStr) };
}
export const asRegex = (regexStr) => {
try {

@@ -11,0 +25,0 @@ return new RegExp(regexStr);

@@ -21,3 +21,3 @@ import assert from './generic/assert.js';

const search = compile(needles, ctx); // keep separate for performance
return (haystack, context) => find(haystack, [search], {
return (haystack, context) => find(haystack, search, {
context,

@@ -24,0 +24,0 @@ ...ctx,

@@ -1,1 +0,1 @@

var e={};(()=>{e.d=(t,r)=>{for(var n in r){if(e.o(r,n)&&!e.o(t,n)){Object.defineProperty(t,n,{enumerable:true,get:r[n]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();if(typeof e!=="undefined")e.ab=new URL(".",import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/)?1:0,-1)+"/";var t={};e.d(t,{Z:()=>lib});const assert=(e,t="Internal Logic Error")=>{if(!e){throw new Error(typeof t==="function"?t():t)}};const defineProperty=(e,t,r,n=true)=>Object.defineProperty(e,t,{value:r,writable:!n});const r=/[?!,.*+[\](){}\\]/g;const helper_escape=e=>e.replace(r,"\\$&");const escapeRegex=e=>e.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&");const asRegex=e=>{try{return new RegExp(e)}catch(t){throw new Error(`Invalid Regex: "${e}"`)}};const toPath=e=>e.reduce(((e,t)=>`${e}${typeof t==="number"?`[${t}]`:`${e?".":""}${helper_escape(t)}`}`),"");const parseWildcard=e=>{let t="";let r=false;for(let n=0;n<e.length;n+=1){const s=e[n];if(!r&&s==="\\"){r=true}else if(!r&&s==="*"){t+=".*"}else if(!r&&s==="+"){t+=".+"}else if(!r&&s==="?"){t+="."}else{t+=escapeRegex(s);r=false}}return new RegExp(`^${t}$`)};const compileWildcard=e=>{if(["**","++"].includes(e)){return asRegex(".*")}if((e.startsWith("**(")||e.startsWith("++("))&&e.endsWith(")")){return asRegex(e.slice(3,-1))}if(e.startsWith("[(")&&e.endsWith(")]")){return asRegex(e.slice(2,-2))}if(e.startsWith("(")&&e.endsWith(")")){return asRegex(e.slice(1,-1))}if(e.startsWith("[")&&e.endsWith("]")){return parseWildcard(e.slice(1,-1))}return parseWildcard(e)};class Wildcard{constructor(e,t){this.value=e;this.excluded=t;this.regex=compileWildcard(e);this.isArrayTarget=e.startsWith("[")&&e.endsWith("]");this.isSimpleStarRec=e==="**";this.isSimplePlusRec=e==="++";this.isSimpleRec=this.isSimpleStarRec||this.isSimplePlusRec;this.isRegexStarRec=e.startsWith("**(")&&e.endsWith(")");this.isRegexPlusRec=e.startsWith("++(")&&e.endsWith(")");this.isRegexRec=this.isRegexStarRec||this.isRegexPlusRec;this.isStarRec=this.isSimpleStarRec||this.isRegexStarRec;this.isPlusRec=this.isSimplePlusRec||this.isRegexPlusRec;this.isRec=this.isStarRec||this.isPlusRec;this.isAnyArrayTarget=e==="[*]";this.isAnyObjTarget=e==="*"}recMatch(e){if(!this.isRec){return false}if(this.isSimpleRec){return true}return this.regex.test(e)}typeMatch(e,t){if(this.isSimpleRec){return true}if(t&&this.isAnyArrayTarget){return true}if(!t&&this.isAnyObjTarget){return true}if(t!==this.isArrayTarget&&!this.isRec){return false}return this.regex.test(e)}}class Ref{constructor(e,t=null){this.left=t===null;this.link=t===null?new Ref(e,this):t;this.type=e;this.isStarRec=this.type==="**";this.node=null;this.pointer=null}setPointer(e){this.pointer=e;this.link.pointer=e}setNode(e){this.node=e;this.link.node=e}}const n=Symbol("is-excluded");const markExcluded=e=>defineProperty(e,n,true);const isExcluded=e=>e[n]===true;const throwError=(e,t,r={})=>{throw new Error(Object.entries(r).reduce(((e,[t,r])=>`${e}, ${t} ${r}`),`${e}: ${t}`))};const getSimple=e=>{if(Array.isArray(e)){return e.length===1?e[0]:e}return e.size===1?e.values().next().value:e};const parser_result=e=>{let t=new Set;let r=false;let n=false;let s=0;const i=[];const newChild=e=>{if(isExcluded(t)){assert(n===false);n=true}i.push(t);t=e?new Set:[]};const finishChild=()=>{const e=i.pop();const r=Array.isArray(e);const n=getSimple(t);if(!r&&n instanceof Set){n.forEach((t=>e.add(t)))}else{e[r?"push":"add"](n)}t=e};newChild(false);return{setInArray:(t,n)=>{if(r===t){throwError(r?"Bad Array Start":"Bad Array Terminator",e,{char:n})}r=t},finishElement:(i,o,a,{finReq:l=false,group:c=false}={})=>{const u=s===i;if(u){if(!a.includes(e[i-1]||null)){throwError(o,e,{char:i})}s+=1}else{if(l){throwError(o,e,{char:i})}const a=e.slice(s,i);if(c&&!["**","++"].includes(a)){throwError("Bad Group Start",e,{char:i})}if(r&&!(/^[?*+\d]+$/.test(a)||a.startsWith("(")&&a.endsWith(")"))){throwError("Bad Array Selector",e,{selector:a})}if(c){t.push(new Ref(a))}else{t.push(new Wildcard(r?`[${a}]`:a,n));n=false}s=i+1}},startExclusion:t=>{if(n!==false){throwError("Redundant Exclusion",e,{char:t})}n=true},startGroup:()=>{newChild(true);if(n){markExcluded(t);n=false}newChild(false)},newGroupElement:()=>{finishChild();newChild(false)},finishGroup:r=>{if(i.length<2){throwError("Unexpected Group Terminator",e,{char:r})}finishChild();finishChild();assert(Array.isArray(t));const n=t[t.length-2];if(n instanceof Ref&&n.left===true){t.push(n.link)}},finalizeResult:()=>{finishChild();assert(n===false);if(i.length!==0){throwError("Non Terminated Group",e)}if(r){throwError("Non Terminated Array",e)}return getSimple(t)}}};const parser_throwError=(e,t,r={})=>{throw new Error(Object.entries(r).reduce(((e,[t,r])=>`${e}, ${t} ${r}`),`${e}: ${t}`))};const parse=(e,t)=>{if(e===""){return new Wildcard("",false)}const r=parser_result(e);const n=e.length;let s=false;let i=0;for(let o=0;o<n;o+=1){const n=e[o];if(s===false){if(i===0){switch(n){case".":r.finishElement(o,"Bad Path Separator",["]","}"]);break;case"[":if(!t.useArraySelector){parser_throwError("Forbidden Array Selector",e,{char:o})}r.finishElement(o,"Bad Array Start",[null,"!","{",",","}","]"]);r.setInArray(true,o);break;case"]":r.finishElement(o,"Bad Array Terminator",["}"]);r.setInArray(false,o);break;case"{":r.finishElement(o,"Bad Group Start",[null,"!",".","[","{",","],{group:true});r.startGroup();break;case",":r.finishElement(o,"Bad Group Separator",["]","}"]);r.newGroupElement();break;case"}":r.finishElement(o,"Bad Group Terminator",["]","}"]);r.finishGroup(o);break;case"!":r.finishElement(o,"Bad Exclusion",[null,".",",","{","["],{finReq:true});r.startExclusion(o);break;default:break}}switch(n){case"(":i+=1;break;case")":if(i===0){parser_throwError("Unexpected Parentheses",e,{char:o})}i-=1;break;default:break}}s=n==="\\"?!s:false}if(s!==false){parser_throwError("Dangling Escape",e,{char:n-1})}if(i!==0){parser_throwError("Unterminated Parentheses",e)}r.finishElement(n,"Bad Terminator",["]","}"]);return r.finalizeResult()};const s={parse:parse};const iterator=(e,t)=>{const r=[e];const n=[null];const s=[];const i=[];const o=[];let a=0;let l=true;while(a!==-1){const e=r[a];if(e instanceof Set){r[a]=[...e];r[a].or=true}else if(Array.isArray(e)){if(e.or!==true){r.splice(a,1,...e);n.splice(a,1,...new Array(e.length).fill(n[a]));if(n[a]!==null){i[n[a]]+=e.length-1}}else{if(s[a]===undefined){s[a]=0;i[a]=0}else if(i[a]!==0){r.splice(a+1,i[a]);n.splice(a+1,i[a]);i[a]=0}if(s[a]<e.length){r.splice(a+1,0,e[s[a]]);n.splice(a+1,0,a);s[a]=(s[a]||0)+1;i[a]+=1;l=true;a+=1}else{s[a]=0;a-=1}}}else if(l===true){o.push(e);t("ADD",e);if(a===r.length-1){t("FIN",o);l=false}else{a+=1}}else{t("RM",o.pop());a-=1}}};const compiler_iterator=(e,t,r,{onAdd:n,onFin:s})=>{const i=[[[e,null]]];let o=false;iterator(r,((r,a)=>{if(r==="RM"){if(a.excluded===true){o=false}i.length-=2}else if(r==="ADD"){if(a.excluded===true){if(o){throw new Error(`Redundant Exclusion: "${t}"`)}o=true}const e=[];const r=i[i.length-2];i[i.length-1].forEach((([t,s])=>n(t,s,a,r,(r=>e.push([r,t])))));i.push(a,e)}else{i[i.length-1].filter((([t])=>t!==e)).forEach((([e,t])=>s(e,t,a[a.length-1],o)))}}))};const i=Symbol("leaf");const markLeaf=(e,t,r)=>defineProperty(e,i,t,r);const isLeaf=e=>i in e;const isMatch=e=>e!==undefined&&e[i]===true;const o=Symbol("roots");const setRoots=(e,t)=>defineProperty(e,o,t);const getRoots=e=>e[o];const a=Symbol("has-matches");const setHasMatches=e=>defineProperty(e,a,true);const hasMatches=e=>e[a]===true;const merge=(e,t,...r)=>{const n=e[t];if(n===undefined){defineProperty(e,t,r)}else{n.push(...r.filter((e=>!n.includes(e))))}};const l=Symbol("leaf-needles");const addLeafNeedle=(e,t)=>merge(e,l,t);const getLeafNeedles=e=>e[l]||[];const c=Symbol("leaf-needles-exclude");const addLeafNeedleExclude=(e,t)=>merge(e,c,t);const getLeafNeedlesExclude=e=>e[c]||[];const u=Symbol("leaf-needles-match");const addLeafNeedleMatch=(e,t)=>merge(e,u,t);const getLeafNeedlesMatch=e=>e[u]||[];const f=Symbol("needles");const addNeedle=(e,t)=>merge(e,f,t);const getNeedles=e=>e[f];const h=Symbol("index");const setIndex=(e,t,r)=>defineProperty(e,h,t,r);const getIndex=e=>e[h];const d=Symbol("order");const setOrder=(e,t)=>defineProperty(e,d,t);const getOrder=e=>e[d];const p=Symbol("wildcard");const setWildcard=(e,t)=>defineProperty(e,p,t);const getWildcard=e=>e[p];const g=Symbol("values");const setValues=(e,t)=>defineProperty(e,g,t);const addValues=(e,t)=>merge(e,g,...t);const getValues=e=>e[g];const matchedBy=e=>Array.from(new Set(e.flatMap((e=>getLeafNeedlesMatch(e)))));const excludedBy=e=>Array.from(new Set(e.flatMap((e=>getLeafNeedlesExclude(e)))));const traversedBy=e=>Array.from(new Set(e.flatMap((e=>getNeedles(e)))));const isLastLeafMatch=e=>{let t=Number.MIN_SAFE_INTEGER;let r=null;for(let n=0,s=e.length;n<s;n+=1){const s=e[n];const i=getIndex(s);if(i!==undefined&&i>t){t=i;r=s}}return r!==null&&isMatch(r)};const applyNeedle=(e,t,r,n)=>{compiler_iterator(e,t,r,{onAdd:(e,r,s,i,o)=>{addNeedle(e,t);if(s instanceof Ref){if(s.left===true){if(s.isStarRec){s.setPointer(e)}s.setNode({});n.stack.push(e,s.node,true);o(s.node)}else{s.target="target"in i?i.target:r[i.value];n.stack.push(s.target,s.node,true);if(s.pointer!==null){o(s.pointer);s.setPointer(null)}o(e)}return}const a=i!==undefined&&s.isStarRec&&s.value===i.value;if(a&&n.strict){throw new Error(`Redundant Recursion: "${t}"`)}if(!a){if(!(s.value in e)){const t={};e[s.value]=t;n.stack.push(e,t,false);if(n.orderByNeedles){setOrder(t,n.counter)}setWildcard(t,s)}o(e[s.value])}else{s.target=e}if(s.isStarRec){o(e)}},onFin:(e,r,s,i)=>{if(n.strict&&s.isSimpleStarRec){const e=Object.keys(r).filter((e=>!["**",""].includes(e)));if(e.length!==0){throw new Error(`Needle Target Invalidated: "${r[e[0]][f][0]}" by "${t}"`)}}addNeedle(e,t);if(n.strict&&l in e){throw new Error(`Redundant Needle Target: "${e[l][0]}" vs "${t}"`)}addLeafNeedle(e,t,n.strict);if(i){addLeafNeedleExclude(e,t)}else{addLeafNeedleMatch(e,t)}markLeaf(e,!i,n.strict);setIndex(e,n.counter,n.strict);n.counter+=1}})};const finalizeTower=(e,t)=>{const{stack:r}=t;const n=[];while(r.length!==0){const e=r.pop();const t=r.pop();const s=r.pop();if(!(g in t)){setValues(t,Object.values(t).reverse())}if(e){n.push(s,t)}if(isMatch(t)){setHasMatches(t)}if(hasMatches(t)&&!hasMatches(s)){setHasMatches(s)}}setValues(e,Object.values(e).reverse());for(let e=0,t=n.length;e<t;e+=2){const t=n[e];const r=n[e+1];addValues(t,getValues(r))}if(t.useArraySelector===false){const t=[];if(""in e){t.push(e[""])}t.push(...getValues(e).filter((e=>getWildcard(e).isStarRec)));setRoots(e,t)}};const compile=(e,t)=>{const r={};t.counter=0;t.stack=[];for(let n=0;n<e.length;n+=1){const i=e[n];const o=[s.parse(i,t)];applyNeedle(r,i,o,t)}setWildcard(r,new Wildcard("*",false));finalizeTower(r,t);return r};const find_result=(e,t)=>{if(t.rtn==="context"){return{onMatch:()=>{},get:()=>e.context}}if(t.rtn==="bool"){let e=false;return{onMatch:()=>{e=true},get:()=>e}}if(t.rtn==="count"){let e=0;return{onMatch:()=>{e+=1},get:()=>e}}if(t.rtn==="sum"){let e=0;return{onMatch:({value:t})=>{e+=t},get:()=>e}}const r=[];return{onMatch:(()=>{if(typeof t.rtn==="function"){return()=>r.push(t.rtn(e))}if(Array.isArray(t.rtn)){return()=>r.push(t.rtn.map((t=>e[t])))}return()=>r.push(e[t.rtn])})(),get:()=>t.abort?r[0]:r}};const formatPath=(e,t)=>t.joined?toPath(e):[...e];const find=(e,t,r)=>{const n={haystack:e,context:r.context};if(r.beforeFn!==undefined){const e=r.beforeFn(n);if(e!==undefined){n.haystack=e}}const s=[false,t,null,0];const i=[];const o=[];let a;let l;let c;let u;let f=n.haystack;const h={getKey:()=>formatPath(i,r),get key(){return h.getKey()},getValue:()=>f,get value(){return h.getValue()},getEntry:()=>[formatPath(i,r),f],get entry(){return h.getEntry()},getIsMatch:()=>u,get isMatch(){return h.getIsMatch()},getMatchedBy:()=>matchedBy(c),get matchedBy(){return h.getMatchedBy()},getExcludedBy:()=>excludedBy(c),get excludedBy(){return h.getExcludedBy()},getTraversedBy:()=>traversedBy(c),get traversedBy(){return h.getTraversedBy()},getGproperty:()=>i[i.length-2],get gproperty(){return h.getGproperty()},getProperty:()=>i[i.length-1],get property(){return h.getProperty()},getGparent:()=>o[o.length-2],get gparent(){return h.getGparent()},getParent:()=>o[o.length-1],get parent(){return h.getParent()},getParents:()=>[...o].reverse(),get parents(){return h.getParents()},getIsCircular:()=>o.includes(f),get isCircular(){return h.getIsCircular()},getIsLeaf:()=>!(f instanceof Object),get isLeaf(){return h.getIsLeaf()},getDepth:()=>i.length,get depth(){return h.getDepth()},get result(){return h.getResult()},context:n.context};const d=find_result(h,r);h.getResult=()=>d.get();if(""in t[0]&&(r.useArraySelector||!Array.isArray(n.haystack))){s[1]=[...s[1],t[0][""]]}do{a=s.pop();l=s.pop();c=s.pop();u=s.pop();const e=i.length-a;for(let t=0;t<e;t+=1){o.pop();i.pop()}if(e===-1){o.push(f);i.push(l);f=f[l]}else if(l!==null){i[i.length-1]=l;f=o[o.length-1][l]}else{f=n.haystack}if(u){if(r.filterFn===undefined||r.filterFn(h)!==false){d.onMatch(h);if(r.abort){s.length=0}}continue}if(!c.some((e=>hasMatches(e)))){continue}const t=r.useArraySelector===false&&Array.isArray(f);if(!t&&isLastLeafMatch(c)){s.push(true,c,l,a);u=true}if((r.breakFn===undefined||r.breakFn(h)!==true)&&f instanceof Object){const e=Array.isArray(f);const n=Object.keys(f);if(!e&&r.compareFn){n.sort(r.compareFn(h))}if(!r.reverse){n.reverse()}for(let i=0,o=n.length;i<o;i+=1){const o=n[i];const l=[];if(t){l.push(...c);if(a===0){l.push(...getRoots(c[0]))}}else{for(let t=0,r=c.length;t!==r;t+=1){const r=c[t];if(getWildcard(r).recMatch(o)){l.push(r)}const n=getValues(r);let s=n.length;while(s--){const t=n[s];if(getWildcard(t).typeMatch(o,e)){l.push(t)}}}}if(r.orderByNeedles){l.index=Buffer.from(l.map((e=>getOrder(e))).sort());let t=s.length-3;const r=t-i*4;while(t!==r&&Buffer.compare(l.index,s[t].index)===1){t-=4}s.splice(t+3,0,false,l,e?Number(o):o,a+1)}else{s.push(false,l,e?Number(o):o,a+1)}}}}while(s.length!==0);n.result=d.get();if(r.afterFn!==undefined){const e=r.afterFn(n);if(e!==undefined){n.result=e}}return n.result};const expect=(e,t,r)=>{assert(r.includes(typeof e[t]),(()=>`Option "${t}" not one of [${r.join(", ")}]`))};const context=e=>{const t={filterFn:undefined,breakFn:undefined,beforeFn:undefined,afterFn:undefined,compareFn:undefined,reverse:true,orderByNeedles:false,abort:false,rtn:undefined,joined:false,useArraySelector:true,strict:true,...e};assert(Object.keys(t).length===12,"Unexpected Option provided");expect(t,"filterFn",["function","undefined"]);expect(t,"breakFn",["function","undefined"]);expect(t,"beforeFn",["function","undefined"]);expect(t,"afterFn",["function","undefined"]);expect(t,"compareFn",["function","undefined"]);expect(t,"reverse",["boolean"]);expect(t,"orderByNeedles",["boolean"]);expect(t,"abort",["boolean"]);assert(typeof t.rtn==="function"&&t.rtn.length===1||[undefined,"context","key","value","entry","property","gproperty","parent","gparent","parents","isMatch","matchedBy","excludedBy","traversedBy","isCircular","isLeaf","depth","bool","count","sum"].includes(t.rtn)||Array.isArray(t.rtn)&&t.rtn.every((e=>["key","value","entry","property","gproperty","parent","gparent","parents","isMatch","matchedBy","excludedBy","traversedBy","isCircular","isLeaf","depth"].includes(e))),'Option "rtn" is malformed');expect(t,"joined",["boolean"]);expect(t,"useArraySelector",["boolean"]);expect(t,"strict",["boolean"]);return t};const lib=(e,t={})=>{assert(Array.isArray(e),'Argument "needles" expected to be Array');assert(t instanceof Object&&!Array.isArray(t),'Argument "opts" expected to be Object');if(e.length===0){return(e,t)=>t===undefined?[]:t}const r=context(t);const n=compile(e,r);return(e,t)=>find(e,[n],{context:t,...r,rtn:r.rtn||(t===undefined?"key":"context")})};var y=t.Z;export{y as default};
var e={};(()=>{e.d=(t,r)=>{for(var n in r){if(e.o(r,n)&&!e.o(t,n)){Object.defineProperty(t,n,{enumerable:true,get:r[n]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();if(typeof e!=="undefined")e.ab=new URL(".",import.meta.url).pathname.slice(import.meta.url.match(/^file:\/\/\/\w:/)?1:0,-1)+"/";var t={};e.d(t,{Z:()=>lib});const assert=(e,t="Internal Logic Error")=>{if(!e){throw new Error(typeof t==="function"?t():t)}};class Value{constructor(e,t){this.value=e;this.excluded=t;this.isSimpleStarRec=e==="**";this.isRegexStarRec=e.startsWith("**(")&&e.endsWith(")");this.isStarRec=this.isSimpleStarRec||this.isRegexStarRec}}class Ref{constructor(e,t=null){this.left=t===null;this.link=t===null?new Ref(e,this):t;this.type=e;this.isStarRec=this.type==="**";this.node=null;this.pointer=null}setPointer(e){this.pointer=e;this.link.pointer=e}setNode(e){this.node=e;this.link.node=e}}const throwError=(e,t,r={})=>{throw new Error(Object.entries(r).reduce(((e,[t,r])=>`${e}, ${t} ${r}`),`${e}: ${t}`))};const getSimple=e=>{if(Array.isArray(e)){return e.length===1?e[0]:e}return e.size===1?e.values().next().value:e};const r=/^[?*+\d]+$/;const parser_result=e=>{let t=new Set;let n=false;let s=false;let i=0;const l=[];const newChild=e=>{if(t.excluded===true){assert(s===false);s=true}l.push(t);t=e?new Set:[]};const finishChild=()=>{const e=l.pop();const r=Array.isArray(e);const n=getSimple(t);if(!r&&n instanceof Set){n.forEach((t=>e.add(t)))}else{e[r?"push":"add"](n)}t=e};newChild(false);return{setInArray:(t,r)=>{if(n===t){throwError(n?"Bad Array Start":"Bad Array Terminator",e,{char:r})}n=t},finishElement:(l,a,c,{finReq:o=false,group:u=false}={})=>{const f=i===l;if(f){if(!c.includes(e[l-1]||null)){throwError(a,e,{char:l})}i+=1}else{if(o){throwError(a,e,{char:l})}const c=e.slice(i,l);if(u&&!["**","++"].includes(c)){throwError("Bad Group Start",e,{char:l})}if(n&&!(r.test(c)||c.startsWith("(")&&c.endsWith(")"))){throwError("Bad Array Selector",e,{selector:c})}if(u){t.push(new Ref(c))}else{t.push(new Value(n?`[${c}]`:c,s));s=false}i=l+1}},startExclusion:t=>{if(s!==false){throwError("Redundant Exclusion",e,{char:t})}s=true},startGroup:()=>{newChild(true);if(s){t.excluded=true;s=false}newChild(false)},newGroupElement:()=>{finishChild();newChild(false)},finishGroup:r=>{if(l.length<2){throwError("Unexpected Group Terminator",e,{char:r})}finishChild();finishChild();assert(Array.isArray(t));const n=t[t.length-2];if(n instanceof Ref&&n.left===true){t.push(n.link)}},finalizeResult:()=>{finishChild();assert(s===false);if(l.length!==0){throwError("Non Terminated Group",e)}if(n){throwError("Non Terminated Array",e)}return getSimple(t)}}};const parser_throwError=(e,t,r={})=>{throw new Error(Object.entries(r).reduce(((e,[t,r])=>`${e}, ${t} ${r}`),`${e}: ${t}`))};const parse=(e,t)=>{if(e===""){return new Value("",false)}const r=parser_result(e);const n=e.length;let s=false;let i=0;for(let l=0;l<n;l+=1){const n=e[l];if(s===false){if(i===0){switch(n){case".":r.finishElement(l,"Bad Path Separator",["]","}"]);break;case"[":if(!t.useArraySelector){parser_throwError("Forbidden Array Selector",e,{char:l})}r.finishElement(l,"Bad Array Start",[null,"!","{",",","}","]"]);r.setInArray(true,l);break;case"]":r.finishElement(l,"Bad Array Terminator",["}"]);r.setInArray(false,l);break;case"{":r.finishElement(l,"Bad Group Start",[null,"!",".","[","{",","],{group:true});r.startGroup();break;case",":r.finishElement(l,"Bad Group Separator",["]","}"]);r.newGroupElement();break;case"}":r.finishElement(l,"Bad Group Terminator",["]","}"]);r.finishGroup(l);break;case"!":r.finishElement(l,"Bad Exclusion",[null,".",",","{","["],{finReq:true});r.startExclusion(l);break;default:break}}switch(n){case"(":i+=1;break;case")":if(i===0){parser_throwError("Unexpected Parentheses",e,{char:l})}i-=1;break;default:break}}s=n==="\\"?!s:false}if(s!==false){parser_throwError("Dangling Escape",e,{char:n-1})}if(i!==0){parser_throwError("Unterminated Parentheses",e)}r.finishElement(n,"Bad Terminator",["]","}"]);return r.finalizeResult()};const n={parse:parse};const iterator=(e,t)=>{const r=[e];const n=[null];const s=[];const i=[];const l=[];let a=0;let c=true;while(a!==-1){const e=r[a];if(e instanceof Set){r[a]=[...e];r[a].or=true}else if(Array.isArray(e)){if(e.or!==true){r.splice(a,1,...e);n.splice(a,1,...new Array(e.length).fill(n[a]));if(n[a]!==null){i[n[a]]+=e.length-1}}else{if(s[a]===undefined){s[a]=0;i[a]=0}else if(i[a]!==0){r.splice(a+1,i[a]);n.splice(a+1,i[a]);i[a]=0}if(s[a]<e.length){r.splice(a+1,0,e[s[a]]);n.splice(a+1,0,a);s[a]=(s[a]||0)+1;i[a]+=1;c=true;a+=1}else{s[a]=0;a-=1}}}else if(c===true){l.push(e);t("ADD",e);if(a===r.length-1){t("FIN",l);c=false}else{a+=1}}else{t("RM",l.pop());a-=1}}};const compiler_iterator=(e,t,r,{onAdd:n,onFin:s})=>{const i=[[[e,null]]];let l=false;iterator(r,((r,a)=>{if(r==="RM"){if(a.excluded===true){l=false}i.length-=2}else if(r==="ADD"){if(a.excluded===true){if(l){throw new Error(`Redundant Exclusion: "${t}"`)}l=true}const e=[];const r=i[i.length-2];i[i.length-1].forEach((([t,s])=>n(t,s,a,r,(r=>e.push([r,t])))));i.push(a,e)}else{i[i.length-1].filter((([t])=>t!==e)).forEach((([e,t])=>s(e,t,a[a.length-1],l)))}}))};const s=/[?!,.*+[\](){}\\]/g;const helper_escape=e=>e.replace(s,"\\$&");const i=/^\^?[^-/\\^$*+?.()|[\]{}]*\$?$/g;const asRegex=e=>{if(i.test(e)){const t=e.startsWith("^");const r=e.endsWith("$");if(t&&r){const t=e.slice(1,-1);return{test:e=>e===t}}if(t){const t=e.slice(1);return{test:e=>e.startsWith(t)}}if(r){const t=e.slice(0,-1);return{test:e=>e.endsWith(t)}}return{test:t=>t.includes(e)}}try{return new RegExp(e)}catch(t){throw new Error(`Invalid Regex: "${e}"`)}};const toPath=e=>e.reduce(((e,t)=>`${e}${typeof t==="number"?`[${t}]`:`${e?".":""}${helper_escape(t)}`}`),"");const l=["-","/","\\","^","$","*","+","?",".","(",")","|","[","]","{","}"];const parseValue=e=>{let t="";let r=false;let n=true;for(let s=0;s<e.length;s+=1){const i=e[s];if(!r&&i==="\\"){r=true}else if(!r&&i==="*"){n=false;t+=".*"}else if(!r&&i==="+"){n=false;t+=".+"}else if(!r&&i==="?"){n=false;t+="."}else{if(l.includes(i)){n=false;t+="\\"}t+=i;r=false}}if(n){return{test:e=>e===t}}if(t===".+"){return{test:e=>e!==""}}return new RegExp(`^${t}$`)};const compileValue=e=>{if((e.startsWith("**(")||e.startsWith("++("))&&e.endsWith(")")){return asRegex(e.slice(3,-1))}if(e.startsWith("[(")&&e.endsWith(")]")){return asRegex(e.slice(2,-2))}if(e.startsWith("(")&&e.endsWith(")")){return asRegex(e.slice(1,-1))}if(e.startsWith("[")&&e.endsWith("]")){return parseValue(e.slice(1,-1))}return parseValue(e)};class Node{constructor(e,t){t.nodes.push(this);this.value=e;this.ctx=t;this.order=t.counter;this.children=[];this.match=false;this.matches=false;this.needles=[];this.leafNeedles=[];this.leafNeedlesExclude=[];this.leafNeedlesMatch=[];this.isArrayTarget=e.startsWith("[")&&e.endsWith("]");this.isSimpleStarRec=e==="**";this.isSimplePlusRec=e==="++";this.isSimpleRec=this.isSimpleStarRec||this.isSimplePlusRec;this.isRegexStarRec=e.startsWith("**(")&&e.endsWith(")");this.isRegexPlusRec=e.startsWith("++(")&&e.endsWith(")");this.isStarRec=this.isSimpleStarRec||this.isRegexStarRec;this.isPlusRec=this.isSimplePlusRec||this.isRegexPlusRec;this.isRec=this.isStarRec||this.isPlusRec;this.isAnyArrayTarget=e==="[*]";this.isAnyObjTarget=e==="*";if(this.isSimpleRec||this.isAnyObjTarget||this.isAnyArrayTarget){this.regex=null}else{const{regex:r}=t;if(!(e in r)){r[e]=compileValue(e)}this.regex=r[e]}}recMatch(e){if(!this.isRec){return false}if(this.isSimpleRec){return true}return this.regex.test(e)}typeMatch(e,t){if(this.isSimpleRec){return true}if(this.isAnyArrayTarget){return t}if(this.isAnyObjTarget){return!t}if(t!==this.isArrayTarget&&!this.isRec){return false}return this.regex.test(e)}add(e){this.children.push(e)}get(e){return this.children.find((({value:t})=>t===e))}markMatches(){this.matches=true}addNeedle(e){if(!this.needles.includes(e)){this.needles.push(e)}}setRoots(e){this.roots=e}finish(e,t,r){this.addNeedle(e);if(!this.leafNeedles.includes(e)){this.leafNeedles.push(e)}const n=t?this.leafNeedlesExclude:this.leafNeedlesMatch;if(!n.includes(e)){n.push(e)}this.match=!t;this.matches=this.match;this.index=r}}const applyNeedle=(e,t,r,n)=>{compiler_iterator(e,t,r,{onAdd:(e,r,s,i,l)=>{e.addNeedle(t);if(s instanceof Ref){if(s.left===true){if(s.isStarRec){s.setPointer(e)}s.setNode(new Node("*",n));n.links.push(e,s.node);l(s.node)}else{s.target="target"in i?i.target:r.get(i.value);n.links.push(s.target,s.node);if(s.pointer!==null){l(s.pointer);s.setPointer(null)}l(e)}return}const a=s.isStarRec&&s.value===i?.value;if(a&&n.strict){throw new Error(`Redundant Recursion: "${t}"`)}if(!a){let t=e.get(s.value);if(t===undefined){t=new Node(s.value,n);e.add(t)}l(t)}else{s.target=e}if(s.isStarRec){l(e)}},onFin:(e,r,s,i)=>{if(n.strict&&s.isSimpleStarRec){const e=r.children.filter((({value:e})=>!["","**"].includes(e)));if(e.length!==0){throw new Error(`Needle Target Invalidated: "${e[0].needles[0]}" by "${t}"`)}}if(n.strict&&e.leafNeedles.length!==0){throw new Error(`Redundant Needle Target: "${e.leafNeedles[0]}" vs "${t}"`)}e.finish(t,i,n.counter);n.counter+=1}})};const finalizeTower=(e,t)=>{const{links:r}=t;while(r.length!==0){const e=r.pop();const t=r.pop();const{children:n}=t;t.children=[...e.children.filter((e=>!n.includes(e))),...n]}const{nodes:n}=t;while(n.length!==0){const e=n.pop();const{children:t}=e;t.reverse();if(t.some((({matches:e})=>e))){e.markMatches()}}if(t.useArraySelector===false){const t=[];const r=e.get("");if(r!==undefined){t.push(r)}t.push(...e.children.filter((({isStarRec:e})=>e)));e.setRoots(t)}};const compile=(e,t)=>{t.counter=0;t.links=[];t.nodes=[];t.regex={};const r=new Node("*",t);for(let s=0;s<e.length;s+=1){const i=e[s];const l=[n.parse(i,t)];applyNeedle(r,i,l,t)}finalizeTower(r,t);return r};const getUniques=(e,t)=>{const r=[];for(let n=0,s=e.length;n<s;n+=1){const s=e[n][t];for(let e=0,t=s.length;e<t;e+=1){const t=s[e];if(!r.includes(t)){r.push(t)}}}return r};const matchedBy=e=>getUniques(e,"leafNeedlesMatch");const excludedBy=e=>getUniques(e,"leafNeedlesExclude");const traversedBy=e=>getUniques(e,"needles");const isLastLeafMatch=e=>{let t=-1;let r=false;let n=e.length;while(n--){const{index:s,match:i}=e[n];if(s>t){t=s;r=i}}return r};const formatPath=(e,t)=>t.joined?toPath(e):[...e];const find_result=(e,t)=>{if(t.rtn==="context"){return{onMatch:()=>{},get:()=>e.context}}if(t.rtn==="bool"){let e=false;return{onMatch:()=>{e=true},get:()=>e}}if(t.rtn==="count"){let e=0;return{onMatch:()=>{e+=1},get:()=>e}}if(t.rtn==="sum"){let e=0;return{onMatch:({value:t})=>{e+=t},get:()=>e}}const r=[];return{onMatch:(()=>{if(typeof t.rtn==="function"){return()=>r.push(t.rtn(e))}if(Array.isArray(t.rtn)){return()=>r.push(t.rtn.map((t=>e[t])))}return()=>r.push(e[t.rtn])})(),get:()=>t.abort?r[0]:r}};const find=(e,t,r)=>{const n={haystack:e,context:r.context};if(r.beforeFn!==undefined){const e=r.beforeFn(n);if(e!==undefined){n.haystack=e}}const s=[false,[t],null,0];const i=[];const l=[];let a;let c;let o;let u;let f=n.haystack;const h={getKey:()=>formatPath(i,r),get key(){return h.getKey()},getValue:()=>f,get value(){return h.getValue()},getEntry:()=>[formatPath(i,r),f],get entry(){return h.getEntry()},getIsMatch:()=>u,get isMatch(){return h.getIsMatch()},getMatchedBy:()=>matchedBy(o),get matchedBy(){return h.getMatchedBy()},getExcludedBy:()=>excludedBy(o),get excludedBy(){return h.getExcludedBy()},getTraversedBy:()=>traversedBy(o),get traversedBy(){return h.getTraversedBy()},getGproperty:()=>i[i.length-2],get gproperty(){return h.getGproperty()},getProperty:()=>i[i.length-1],get property(){return h.getProperty()},getGparent:()=>l[l.length-2],get gparent(){return h.getGparent()},getParent:()=>l[l.length-1],get parent(){return h.getParent()},getParents:()=>[...l].reverse(),get parents(){return h.getParents()},getIsCircular:()=>l.includes(f),get isCircular(){return h.getIsCircular()},getIsLeaf:()=>!(f instanceof Object),get isLeaf(){return h.getIsLeaf()},getDepth:()=>i.length,get depth(){return h.getDepth()},get result(){return h.getResult()},context:n.context};const d=find_result(h,r);h.getResult=()=>d.get();if(r.useArraySelector||!Array.isArray(n.haystack)){const e=t.get("");if(e!==undefined){s[1].push(e)}}do{a=s.pop();c=s.pop();o=s.pop();u=s.pop();const e=i.length-a;for(let t=0;t<e;t+=1){l.pop();i.pop()}if(e===-1){l.push(f);i.push(c);f=f[c]}else if(c!==null){i[i.length-1]=c;f=l[l.length-1][c]}else{f=n.haystack}if(u){if(r.filterFn===undefined||r.filterFn(h)!==false){d.onMatch(h);if(r.abort){s.length=0}}continue}if(!o.some((({matches:e})=>e))){continue}const p=r.useArraySelector===false&&Array.isArray(f);if(!p&&isLastLeafMatch(o)){s.push(true,o,c,a);u=true}if((r.breakFn===undefined||r.breakFn(h)!==true)&&f instanceof Object){const e=Array.isArray(f);const n=Object.keys(f);if(!e&&r.compareFn){n.sort(r.compareFn(h))}if(!r.reverse){n.reverse()}for(let i=0,l=n.length;i<l;i+=1){const l=n[i];const c=[];if(p){c.push(...o);if(a===0){c.push(...t.roots)}}else{for(let t=0,r=o.length;t!==r;t+=1){const r=o[t];if(r.recMatch(l)){c.push(r)}const{children:n}=r;let s=n.length;while(s--){const t=n[s];if(t.typeMatch(l,e)){c.push(t)}}}}if(r.orderByNeedles){c.index=Buffer.from(c.map((({order:e})=>e)).sort());let t=s.length-3;const r=t-i*4;while(t!==r&&Buffer.compare(c.index,s[t].index)===1){t-=4}s.splice(t+3,0,false,c,e?Number(l):l,a+1)}else{s.push(false,c,e?Number(l):l,a+1)}}}}while(s.length!==0);n.result=d.get();if(r.afterFn!==undefined){const e=r.afterFn(n);if(e!==undefined){n.result=e}}return n.result};const expect=(e,t,r)=>{assert(r.includes(typeof e[t]),(()=>`Option "${t}" not one of [${r.join(", ")}]`))};const context=e=>{const t={filterFn:undefined,breakFn:undefined,beforeFn:undefined,afterFn:undefined,compareFn:undefined,reverse:true,orderByNeedles:false,abort:false,rtn:undefined,joined:false,useArraySelector:true,strict:true,...e};assert(Object.keys(t).length===12,"Unexpected Option provided");expect(t,"filterFn",["function","undefined"]);expect(t,"breakFn",["function","undefined"]);expect(t,"beforeFn",["function","undefined"]);expect(t,"afterFn",["function","undefined"]);expect(t,"compareFn",["function","undefined"]);expect(t,"reverse",["boolean"]);expect(t,"orderByNeedles",["boolean"]);expect(t,"abort",["boolean"]);assert(typeof t.rtn==="function"&&t.rtn.length===1||[undefined,"context","key","value","entry","property","gproperty","parent","gparent","parents","isMatch","matchedBy","excludedBy","traversedBy","isCircular","isLeaf","depth","bool","count","sum"].includes(t.rtn)||Array.isArray(t.rtn)&&t.rtn.every((e=>["key","value","entry","property","gproperty","parent","gparent","parents","isMatch","matchedBy","excludedBy","traversedBy","isCircular","isLeaf","depth"].includes(e))),'Option "rtn" is malformed');expect(t,"joined",["boolean"]);expect(t,"useArraySelector",["boolean"]);expect(t,"strict",["boolean"]);return t};const lib=(e,t={})=>{assert(Array.isArray(e),'Argument "needles" expected to be Array');assert(t instanceof Object&&!Array.isArray(t),'Argument "opts" expected to be Object');if(e.length===0){return(e,t)=>t===undefined?[]:t}const r=context(t);const n=compile(e,r);return(e,t)=>find(e,n,{context:t,...r,rtn:r.rtn||(t===undefined?"key":"context")})};var a=t.Z;export{a as default};
{
"name": "object-scan",
"type": "module",
"version": "18.3.0",
"version": "18.3.1",
"description": "Traverse object hierarchies using matching and callbacks.",

@@ -23,3 +23,3 @@ "main": "lib/index.js",

"it": "yarn run i && yarn run t",
"ncc-analyze": "yarn clean && ncc build -m -s -o lib && npx source-map-explorer lib/index.js --only-mapped --html source.html && yarn clean"
"ncc-analyze": "yarn clean && cp -rf ./src ./lib && ncc build -m -s -o lib && npx source-map-explorer lib/index.js --only-mapped --html source.html && yarn clean"
},

@@ -53,7 +53,7 @@ "repository": {

"devDependencies": {
"@babel/core": "7.18.2",
"@babel/core": "7.18.5",
"@babel/eslint-parser": "7.18.2",
"@babel/register": "7.17.7",
"@blackflux/eslint-plugin-rules": "2.1.0",
"@blackflux/robo-config-plugin": "7.8.5",
"@blackflux/robo-config-plugin": "7.8.8",
"@vercel/ncc": "0.34.0",

@@ -65,3 +65,3 @@ "c8": "7.11.3",

"diff2html": "3.4.17",
"eslint": "8.17.0",
"eslint": "8.18.0",
"eslint-config-airbnb-base": "15.0.0",

@@ -77,4 +77,4 @@ "eslint-plugin-import": "2.26.0",

"mustache": "4.2.0",
"node-tdd": "3.4.1",
"object-scan": "18.2.1",
"node-tdd": "3.4.2",
"object-scan": "18.3.0",
"smart-fs": "3.0.1",

@@ -81,0 +81,0 @@ "stringify-object": "4.0.1",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc