@nex-ui/utils
Version:
Utility functions for React components.
33 lines (29 loc) • 970 B
JavaScript
var is = require('./is.cjs');
const isNotNullish = (element)=>element != null;
function walkObject(target, fn, options = {}) {
const { predicate, getKey } = options;
function inner(value, path = []) {
if (is.isPlainObject(value) || Array.isArray(value)) {
const result = {};
for (const [prop, child] of Object.entries(value)){
const key = getKey?.(prop, child) ?? prop;
const childPath = [
...path,
key
];
if (predicate?.(value, childPath)) {
return fn(value, path);
}
const next = inner(child, childPath);
if (isNotNullish(next)) {
result[key] = next;
}
}
return result;
}
return fn(value, path);
}
return inner(target);
}
exports.walkObject = walkObject;
;