trigger.dev
Version:
A Command-Line Interface for Trigger.dev (v3) projects
19 lines • 913 B
JavaScript
/** Generates an object from an array or object. Simpler than reduce or _.transform. The KeyValueGenerator passes (key, value) if the input is an object, and (value, i) if it is an array. The return object from each iteration is merged into the accumulated object. Return null to skip an item. */
export function keyValueBy(input,
// if no keyValue is given, sets all values to true
keyValue, accum = {}) {
const isArray = Array.isArray(input);
keyValue =
keyValue || ((key) => ({ [key]: true }));
// considerably faster than Array.prototype.reduce
Object.entries(input || {}).forEach(([key, value], i) => {
const o = isArray
? keyValue(value, i, accum)
: keyValue(key, value, accum);
Object.entries(o || {}).forEach((entry) => {
accum[entry[0]] = entry[1];
});
});
return accum;
}
//# sourceMappingURL=keyValueBy.js.map