obj-walker
Version:
Walk or map over objects in a depth-first preorder or postorder manner.
61 lines (60 loc) • 1.85 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ECMA_SIZES = exports.getRoot = exports.defTraverse = exports.parentIsArray = exports.defShouldSkip = exports.isObjectOrArray = void 0;
const fp_1 = __importDefault(require("lodash/fp"));
/**
* Is the value a plain object or an array?
*/
const isObjectOrArray = (x) => {
if ((x && typeof x === 'object') || Array.isArray(x)) {
return true;
}
return false;
};
exports.isObjectOrArray = isObjectOrArray;
/**
* Skip if the value is undefined and the node's parent is not array.
*/
const defShouldSkip = (val, node) => val === undefined && !(0, exports.parentIsArray)(node);
exports.defShouldSkip = defShouldSkip;
/**
* Is the parent of the node an array?
*/
const parentIsArray = (node) => {
const parent = node.parents[0];
return Array.isArray(parent);
};
exports.parentIsArray = parentIsArray;
/**
* Is the value a non-empty object or array? If yes, then traverse it.
*/
const defTraverse = (x) => (0, exports.isObjectOrArray)(x) && !fp_1.default.isEmpty(x) && x;
exports.defTraverse = defTraverse;
const getRoot = (obj, jsonCompat = false) => {
const rootCommon = { path: [], isLeaf: false, isRoot: true };
return jsonCompat
? { key: '', val: obj, parents: [{ '': obj }], ...rootCommon }
: { key: undefined, val: obj, parents: [], ...rootCommon };
};
exports.getRoot = getRoot;
/**
* Size in bytes
*/
exports.ECMA_SIZES = {
STRING: 2,
BOOLEAN: 4,
BYTES: 4,
NUMBER: 8,
Int8Array: 1,
Uint8Array: 1,
Uint8ClampedArray: 1,
Int16Array: 2,
Uint16Array: 2,
Int32Array: 4,
Uint32Array: 4,
Float32Array: 4,
Float64Array: 8,
};