« get me outta code hell

thing.js « thing « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/thing/thing.js
blob: 2d6def62112affafc271af742063c7d4576bb875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Base class for Things. No, we will not come up with a better name.
// Sorry not sorry! :)

import CacheableObject from './cacheable-object.js';

import {
    validateArrayItems,
} from './validators.js';

import { getKebabCase } from '../util/wiki-data.js';
import find from '../util/find.js';

export default class Thing extends CacheableObject {
    static referenceType = Symbol('Thing.referenceType');

    static directoryExpose = {
        dependencies: ['name'],
        transform(directory, { name }) {
            if (directory === null && name === null)
                return null;
            else if (directory === null)
                return getKebabCase(name);
            else
                return directory;
        }
    };

    static genContribsExpose(contribsByRefProperty) {
        return {
            dependencies: ['artistData', contribsByRefProperty],
            compute: ({ artistData, [contribsByRefProperty]: contribsByRef }) => (
                (contribsByRef && artistData
                    ? (contribsByRef
                        .map(({ who: ref, what }) => ({
                            who: find.artist(ref, {wikiData: {artistData}}),
                            what
                        }))
                        .filter(({ who }) => who))
                    : [])
            )
        };
    }

    static genWikiDataProperty(thingClass) {
        return {
            flags: {update: true},
            update: {
                validate: validateArrayItems(x => x instanceof thingClass)
            }
        };
    }

    static getReference(thing) {
        if (!thing.constructor[Thing.referenceType])
            throw TypeError(`Passed Thing is ${thing.constructor.name}, which provides no [Thing.referenceType]`);

        if (!thing.directory)
            throw TypeError(`Passed ${thing.constructor.name} is missing its directory`);

        return `${thing.constructor[Thing.referenceType]}:${thing.directory}`;
    }
}