diff options
Diffstat (limited to 'src/data/serialize.js')
-rw-r--r-- | src/data/serialize.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/data/serialize.js b/src/data/serialize.js new file mode 100644 index 0000000..8cac330 --- /dev/null +++ b/src/data/serialize.js @@ -0,0 +1,45 @@ +// serialize.js: simple interface and utility functions for converting +// Things into a directly serializeable format + +// Utility functions + +export function id(x) { + return x; +} + +export function toRef(thing) { + return thing?.constructor.getReference(thing); +} + +export function toRefs(things) { + return things?.map(toRef); +} + +export function toContribRefs(contribs) { + return contribs?.map(({who, what}) => ({who: toRef(who), what})); +} + +export function toCommentaryRefs(entries) { + return entries?.map(({artist, ...props}) => ({artist: toRef(artist), ...props})); +} + +// Interface + +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])]) + ); +} + +export function serializeThings(things) { + return things.map(serializeThing); +} |