diff options
Diffstat (limited to 'src/data/serialize.js')
-rw-r--r-- | src/data/serialize.js | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/data/serialize.js b/src/data/serialize.js index 9d4e8885..a4206fd0 100644 --- a/src/data/serialize.js +++ b/src/data/serialize.js @@ -1,22 +1,24 @@ +/** @format */ + // serialize-util.js: simple interface and utility functions for converting // Things into a directly serializeable format // Utility functions export function id(x) { - return x; + return x; } export function toRef(thing) { - return thing?.constructor.getReference(thing); + return thing?.constructor.getReference(thing); } export function toRefs(things) { - return things?.map(toRef); + return things?.map(toRef); } export function toContribRefs(contribs) { - return contribs?.map(({ who, what }) => ({who: toRef(who), what})); + return contribs?.map(({who, what}) => ({who: toRef(who), what})); } // Interface @@ -24,15 +26,21 @@ export function toContribRefs(contribs) { export const serializeDescriptors = Symbol(); export function serializeThing(thing) { - const descriptors = thing.constructor[serializeDescriptors]; - if (!descriptors) { - throw new Error(`Constructor ${thing.constructor.name} does not provide serialize descriptors`); - } - - return Object.fromEntries(Object.entries(descriptors) - .map(([ property, transform ]) => [property, transform(thing[property])])); + const descriptors = thing.constructor[serializeDescriptors]; + if (!descriptors) { + throw new Error( + `Constructor ${thing.constructor.name} does not provide serialize descriptors` + ); + } + + return Object.fromEntries( + Object.entries(descriptors).map(([property, transform]) => [ + property, + transform(thing[property]), + ]) + ); } export function serializeThings(things) { - return things.map(serializeThing); + return things.map(serializeThing); } |