UNPKG

@liberquack/utils

Version:
153 lines (152 loc) 3.84 kB
/// <reference lib="ES2020"/> export function listToDictionary(list, key) { return list.reduce((acc, item) => { return { ...acc, [item[key]]: item }; }, {}); } export function dictionaryToList(dict) { return Object.keys(dict).map(key => dict[key]); } export function dictionaryForEach(dict, cb) { Object.keys(dict).forEach(key => { cb(dict[key], key); }); } /** * Generates a new dictionary from the original * * @example * dictionaryMap({a: 1, b: 2}, (key, value) => [value, key]) * // {1: "a", 2: "b"} * * @param dict * @param cb */ export function dictionaryMap(dict, cb) { const entries = dictionaryMapList(dict, cb); return Object.fromEntries(entries); } /** * Returns a list for each entry * * @example * dictionaryMap({a: 1, b: 2}, (value, key) => value + key) * // ["a1", "b2"] * * @param dict * @param cb */ export function dictionaryMapList(dict, cb) { return Object.keys(dict).map((key, index) => { return cb(key, dict[key], index); }); } /** * Reduce values of the dictionary * * @example key * let list = [{gender: M, id: 1}, {gender: M, id: 2}, {gender: F, id: 3}] * listToDictionaryAcc(list, "gender") * * // Result: * // { * // M: [ * // {gender: M, id: 1}, * // {gender: M, id: 2} * // ], * // F: [ * // {gender: F, id: 3} * // ] * // } * * @param list * @param key */ export function listToDictionaryAcc(list, key) { let acc = {}; for (let item of list) { let previousValue = acc[item[key]]; acc[item[key]] = previousValue ? [...previousValue, item] : [item]; } return acc; } /** * Reduce values of the dictionary * * * @example predicate * let list = [{gender: M, id: 1}, {gender: M, id: 2}, {gender: F, id: 3}] * let predicate = (item) => item.id % 2 * listToDictionaryPartitioner(["even","odd"], list, predicate) * * // Result: * // { * // "even": [ * // {gender: M, id: 1}, * // {gender: M, id: 2} * // ], * // "odd": [ * // {gender: F, id: 3} * // ] * // } * * @param list * @param partitions * @param predicate */ export function listToDictionaryPartitioner(list, partitions, predicate) { let acc = partitions.reduce((a, b) => { a[b] = []; return a; }, {}); for (let item of list) { let group = partitions[predicate(item)] ?? "undefined"; let previousValue = acc[group]; acc[group] = previousValue ? [...previousValue, item] : [item]; } return acc; } /** * Accumulate dictionary list, resulting a new dictionary of lists * @example * let list = [ * {a: 5}, * {a: 5}, * {b: 10}, * ] * listToDictionaryMergedKeys(list) * // {a: [5,5], b:[10]} * * @param list */ export function listToDictionaryMergedKeys(list) { let acc = {}; for (let obj of list) { const properties = Object.keys(obj); properties.forEach((property) => { const keyAcc = (acc[property] || []); const value = obj[property]; const nextAcc = [...keyAcc, value]; acc[property] = nextAcc; }); } return acc; } /** * Reduce dictionary values * @example * let dict = {a: 10, b: 20} * dictionaryAcc(dict, 0, (acc, value: number, key: string) => acc + value) * //30 * * @param dict * @param acc * @param reducer */ export function dictionaryAcc(dict, acc, reducer) { const result = Object.keys(dict).reduce((reducAcc, key) => { return reducer(reducAcc, dict[key], key); }, acc); return result; } //# sourceMappingURL=dictionary.js.map