diff options
author | (quasar) nebula <towerofnix@gmail.com> | 2021-06-27 13:11:26 -0300 |
---|---|---|
committer | (quasar) nebula <towerofnix@gmail.com> | 2021-06-27 13:11:34 -0300 |
commit | 73bbd2853e7a6ecc143d9d71dcca522e84375e30 (patch) | |
tree | b74cef0a95d96ad627bd2780fd2198129d4aa046 /src/thing | |
parent | 0d31ffdd127d4a199b8944ddb81f9013c45cd83c (diff) |
aggregate shenanigans left uncommitted
Diffstat (limited to 'src/thing')
-rw-r--r-- | src/thing/album.js | 21 | ||||
-rw-r--r-- | src/thing/structures.js | 13 |
2 files changed, 30 insertions, 4 deletions
diff --git a/src/thing/album.js b/src/thing/album.js index be37489d..e99cfc36 100644 --- a/src/thing/album.js +++ b/src/thing/album.js @@ -1,6 +1,7 @@ import Thing from './thing.js'; import { + validateDirectory, validateReference } from './structures.js'; @@ -10,22 +11,33 @@ import { } from '../util/sugar.js'; export default class Album extends Thing { + #directory = null; #tracks = []; static updateError = { + directory: Thing.extendPropertyError('directory'), tracks: Thing.extendPropertyError('tracks') }; update(source) { - withAggregate(({ wrap, call, map }) => { - if (source.tracks) { - this.#tracks = map(source.tracks, t => validateReference('track')(t) && t, { - errorClass: this.constructor.updateError.tracks + const err = this.constructor.updateError; + + withAggregate(({ nest, filter, throws }) => { + + if (source.directory) { + nest(throws(err.directory), ({ call }) => { + if (call(validateDirectory, source.directory)) { + this.#directory = source.directory; + } }); } + + if (source.tracks) + this.#tracks = filter(source.tracks, validateReference('track'), throws(err.tracks)); }); } + get directory() { return this.#directory; } get tracks() { return this.#tracks; } } @@ -35,6 +47,7 @@ console.log('tracks (before):', album.tracks); try { album.update({ + directory: 'oh yes', tracks: [ 'lol', 123, diff --git a/src/thing/structures.js b/src/thing/structures.js index e1bf06c0..89c9bd39 100644 --- a/src/thing/structures.js +++ b/src/thing/structures.js @@ -1,5 +1,18 @@ // Generic structure utilities common across various Thing types. +export function validateDirectory(directory) { + if (typeof directory !== 'string') + throw new TypeError(`Expected a string, got ${directory}`); + + if (directory.length === 0) + throw new TypeError(`Expected directory to be non-zero length`); + + if (directory.match(/[^a-zA-Z0-9\-]/)) + throw new TypeError(`Expected only letters, numbers, and dash, got "${directory}"`); + + return true; +} + export function validateReference(type = '') { return ref => { if (typeof ref !== 'string') |