25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
export const dict = {
|
|
keys: (dict: Record<string, any>) => Object.keys(dict),
|
|
values: (dict: Record<string, any>) => Object.values(dict),
|
|
entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
|
|
'has?': (dict: Record<string, any>, key: string) => key in dict,
|
|
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
|
|
merge: (...dicts: Record<string, any>[]) => Object.assign({}, ...dicts),
|
|
'empty?': (dict: Record<string, any>) => Object.keys(dict).length === 0,
|
|
map: async (dict: Record<string, any>, cb: Function) => {
|
|
const result: Record<string, any> = {}
|
|
for (const [key, value] of Object.entries(dict)) {
|
|
result[key] = await cb(value, key)
|
|
}
|
|
return result
|
|
},
|
|
filter: async (dict: Record<string, any>, cb: Function) => {
|
|
const result: Record<string, any> = {}
|
|
for (const [key, value] of Object.entries(dict)) {
|
|
if (await cb(value, key)) result[key] = value
|
|
}
|
|
return result
|
|
},
|
|
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
|
|
}
|