« 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: 54a278d1830255ad71da587aa50a45e6069f5f90 (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
// Base class for Things. No, we will not come up with a better name.
// Sorry not sorry! :)

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

import { getKebabCase } from '../util/wiki-data.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 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}`;
    }
}