anki/ts/lib/lazy.ts
2021-04-15 13:09:49 +02:00

26 lines
723 B
TypeScript

export function lazyProperties(
object: Record<string, unknown>,
properties: Record<string, () => unknown>
): void {
const propertyDescriptorMap = Object.entries(properties)
.map(([name, getter]: [string, () => unknown]): [
string,
PropertyDescriptor
] => [
name,
{
get: getter,
enumerable: true,
},
])
.reduce(
(
accumulator: PropertyDescriptorMap,
[name, property]
): PropertyDescriptorMap => ((accumulator[name] = property), accumulator),
{}
);
Object.defineProperties(object, propertyDescriptorMap);
}