object-fns
Version:
Object utility functions
59 lines (58 loc) • 1.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
function map(obj, fn) {
const result = {};
for (const key in obj) {
const value = fn(obj[key], key, obj);
if (value !== undefined) {
result[key] = value;
}
}
return result;
}
exports.map = map;
function filterBy(obj, keys) {
const result = {};
for (const key of keys) {
if (key in obj) {
result[key] = obj[key];
}
}
return result;
}
exports.filterBy = filterBy;
function filter(obj, fn) {
const result = {};
for (const key in obj) {
if (fn(obj[key], key, obj)) {
result[key] = obj[key];
}
}
return result;
}
exports.filter = filter;
function findKey(obj, fn) {
for (const key in obj) {
if (fn(obj[key], key, obj)) {
return key;
}
}
}
exports.findKey = findKey;
function all(obj, fn) {
for (const key in obj) {
if (!fn(obj[key], key, obj)) {
return false;
}
}
return true;
}
exports.all = all;
function reduce(obj, fn, start) {
let result = start;
for (const key in obj) {
result = fn(result, obj[key], key, obj);
}
return result;
}
exports.reduce = reduce;
;