diff options
Diffstat (limited to 'src/data/serialize.js')
-rw-r--r-- | src/data/serialize.js | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/data/serialize.js b/src/data/serialize.js index 9d4e888..8cac330 100644 --- a/src/data/serialize.js +++ b/src/data/serialize.js @@ -1,22 +1,26 @@ -// serialize-util.js: simple interface and utility functions for converting +// serialize.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})); +} + +export function toCommentaryRefs(entries) { + return entries?.map(({artist, ...props}) => ({artist: toRef(artist), ...props})); } // Interface @@ -24,15 +28,18 @@ 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`); - } + 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])])); + 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); } |