object-lib
Advanced tools
Comparing version 2.0.3 to 2.0.4
"use strict"; | ||
const assert = require('assert'); | ||
const objectScan = require('object-scan'); | ||
module.exports = (logic_ = {}) => { | ||
const logic = { | ||
'**': null, | ||
...logic_ | ||
}; | ||
const last = arr => arr[arr.length - 1]; | ||
const last = arr => arr[arr.length - 1]; | ||
const mkChild = ref => { | ||
if (!(ref instanceof Object)) { | ||
return ref; | ||
} | ||
const mkChild = ref => { | ||
if (!(ref instanceof Object)) { | ||
return ref; | ||
} | ||
return Array.isArray(ref) ? [] : {}; | ||
}; | ||
return Array.isArray(ref) ? [] : {}; | ||
}; | ||
const populate = (obj, key, fn) => { | ||
if (!(key in obj)) { | ||
// eslint-disable-next-line no-param-reassign | ||
obj[key] = fn(); | ||
return true; | ||
} | ||
const populate = (obj, key, fn, force = false) => { | ||
if (force === true || !(key in obj)) { | ||
// eslint-disable-next-line no-param-reassign | ||
obj[key] = fn(); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
return false; | ||
const incompatible = (a, b) => !(a instanceof Object) || !(b instanceof Object) || Array.isArray(a) !== Array.isArray(b); | ||
module.exports = (logic_ = {}) => { | ||
const logic = { | ||
'**': null, | ||
...logic_ | ||
}; | ||
const scanner = objectScan(Object.keys(logic), { | ||
@@ -40,3 +43,2 @@ reverse: false, | ||
}) => { | ||
if (!isMatch) return; | ||
const { | ||
@@ -48,25 +50,45 @@ stack, | ||
const current = last(stack); | ||
const bestNeedle = last(matchedBy); | ||
const groupBy = typeof logic[bestNeedle] === 'function' ? logic[bestNeedle](value) : logic[bestNeedle]; | ||
if (!Array.isArray(current) || groupBy === null) { | ||
if (Array.isArray(current)) { | ||
current.push(mkChild(value)); | ||
stack.push(last(current)); | ||
} else { | ||
populate(current, property, () => mkChild(value), !(value instanceof Object)); | ||
stack.push(current[property]); | ||
if (!isMatch) { | ||
if (incompatible(current, value)) { | ||
stack[0] = mkChild(value); | ||
} | ||
} else { | ||
const groupId = `${bestNeedle}.${groupBy}: ${path.join('.')}`; | ||
populate(groups, groupId, () => ({})); | ||
const groupEntryId = value[groupBy]; | ||
if (populate(groups[groupId], groupEntryId, () => mkChild(value))) { | ||
current.push(groups[groupId][groupEntryId]); | ||
return false; | ||
} | ||
if (!(current instanceof Object)) { | ||
stack.push(null); | ||
return true; | ||
} | ||
if (!Array.isArray(current)) { | ||
if (!(property in current) || incompatible(current[property], value)) { | ||
current[property] = mkChild(value); | ||
} | ||
path.push(`${groupBy}=${groupEntryId}`); | ||
stack.push(groups[groupId][groupEntryId]); | ||
stack.push(current[property]); | ||
return false; | ||
} | ||
const bestNeedle = last(matchedBy); | ||
const groupBy = typeof logic[bestNeedle] === 'function' ? logic[bestNeedle](value) : logic[bestNeedle]; | ||
if (groupBy === null) { | ||
current.push(value); | ||
stack.push(null); | ||
return true; | ||
} | ||
const groupId = `${bestNeedle}.${groupBy}: ${path.join('.')}`; | ||
populate(groups, groupId, () => ({})); | ||
const groupEntryId = value instanceof Object ? value[groupBy] : undefined; | ||
if (populate(groups[groupId], groupEntryId, () => mkChild(value))) { | ||
current.push(groups[groupId][groupEntryId]); | ||
} | ||
path.push(`${groupBy}=${groupEntryId}`); | ||
stack.push(groups[groupId][groupEntryId]); | ||
return false; | ||
}, | ||
@@ -89,11 +111,12 @@ filterFn: ({ | ||
return (...args) => { | ||
const result = mkChild(last(args)); | ||
const stack = [undefined]; | ||
const groups = {}; | ||
args.forEach(arg => scanner(arg, { | ||
stack: [result], | ||
stack, | ||
groups, | ||
path: [] | ||
})); | ||
return result; | ||
assert(stack.length === 1); | ||
return stack[0]; | ||
}; | ||
}; |
{ | ||
"name": "object-lib", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"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
11928
190