@devgrid/common
Version:
Some useful primitives
67 lines • 2.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.entries = exports.values = exports.keys = void 0;
const predicates_1 = require("./predicates");
const objectOwnProps = Object.getOwnPropertyNames(Object.getPrototypeOf({}));
const _keys = (obj, enumOnly, followProto) => {
if (!followProto) {
return enumOnly ? Object.keys(obj) : Object.getOwnPropertyNames(obj);
}
const props = [];
const seen = new Set();
const protoChain = [];
let current = obj;
while (current) {
protoChain.unshift(current);
current = Object.getPrototypeOf(current);
}
for (const proto of protoChain) {
const keys = enumOnly ?
Object.keys(proto) :
Object.getOwnPropertyNames(proto);
for (const key of keys) {
if (!seen.has(key) && !objectOwnProps.includes(key)) {
seen.add(key);
props.push(key);
}
}
}
return props;
};
const keys = (obj, { enumOnly = true, followProto = false, all = false } = {}) => {
if ((0, predicates_1.isNil)(obj)) {
return [];
}
if (all) {
enumOnly = false;
followProto = true;
}
return _keys(obj, enumOnly, followProto);
};
exports.keys = keys;
const values = (obj, { enumOnly = true, followProto = false, all = false } = {}) => {
if ((0, predicates_1.isNil)(obj)) {
return [];
}
if (all) {
enumOnly = false;
followProto = true;
}
if (!followProto && enumOnly) {
return Object.values(obj);
}
return _keys(obj, enumOnly, followProto).map(key => obj[key]);
};
exports.values = values;
const entries = (obj, { enumOnly = true, followProto = false, all = false } = {}) => {
if ((0, predicates_1.isNil)(obj)) {
return [];
}
if (all) {
enumOnly = false;
followProto = true;
}
return _keys(obj, enumOnly, followProto).map(key => [key, obj[key]]);
};
exports.entries = entries;
//# sourceMappingURL=entries.js.map
;