« get me outta code hell

structures.js « thing « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/thing/structures.js
blob: e1bf06c05b100dd0d9b8ae602e19ed1008765fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Generic structure utilities common across various Thing types.

export function validateReference(type = '') {
    return ref => {
        if (typeof ref !== 'string')
            throw new TypeError(`Expected a string, got ${ref}`);

        if (type) {
            if (!ref.includes(':'))
                throw new TypeError(`Expected ref to begin with "${type}:", but no type specified (ref: ${ref})`);

            const typePart = ref.split(':')[0];
            if (typePart !== type)
                throw new TypeError(`Expected ref to begin with "${type}:", got "${typePart}:" (ref: ${ref})`);
        }

        return true;
    };
}