common.ts 3.0 KB
Newer Older
B
Boris Sekachev 已提交
1 2 3 4
// Copyright (C) 2020 Intel Corporation
//
// SPDX-License-Identifier: MIT

5 6 7
export interface Attribute {
    id: number;
    name: string;
8
    input_type: string;
9 10 11 12 13 14
    mutable: boolean;
    values: string[];
}

export interface Label {
    name: string;
D
Dmitry Kalinin 已提交
15
    color: string;
16 17 18 19 20 21
    id: number;
    attributes: Attribute[];
}

let id = 0;

22
function validateParsedAttribute(attr: Attribute): void {
V
Vitaliy Nishukov 已提交
23
    if (typeof attr.name !== 'string') {
24 25 26
        throw new Error(`Type of attribute name must be a string. Got value ${attr.name}`);
    }

V
Vitaliy Nishukov 已提交
27 28
    if (!['number', 'undefined'].includes(typeof attr.id)) {
        throw new Error(
29
            `Attribute: "${attr.name}". Type of attribute id must be a number or undefined. Got value ${attr.id}`,
V
Vitaliy Nishukov 已提交
30
        );
31 32 33
    }

    if (!['checkbox', 'number', 'text', 'radio', 'select'].includes((attr.input_type || '').toLowerCase())) {
34
        throw new Error(`Attribute: "${attr.name}". Unknown input type: ${attr.input_type}`);
35 36
    }

V
Vitaliy Nishukov 已提交
37 38
    if (typeof attr.mutable !== 'boolean') {
        throw new Error(
39
            `Attribute: "${attr.name}". Mutable flag must be a boolean value. Got value ${attr.mutable}`,
V
Vitaliy Nishukov 已提交
40
        );
41 42 43
    }

    if (!Array.isArray(attr.values)) {
V
Vitaliy Nishukov 已提交
44
        throw new Error(
45
            `Attribute: "${attr.name}". Attribute values must be an array. Got type ${typeof attr.values}`,
V
Vitaliy Nishukov 已提交
46
        );
47 48
    }

B
Boris Sekachev 已提交
49 50 51 52
    if (!attr.values.length) {
        throw new Error(`Attribute: "${attr.name}". Attribute values array mustn't be empty`);
    }

53
    for (const value of attr.values) {
V
Vitaliy Nishukov 已提交
54
        if (typeof value !== 'string') {
55
            throw new Error(`Attribute: "${attr.name}". Each value must be a string. Got value ${value}`);
56 57 58 59 60
        }
    }
}

export function validateParsedLabel(label: Label): void {
V
Vitaliy Nishukov 已提交
61
    if (typeof label.name !== 'string') {
62 63 64
        throw new Error(`Type of label name must be a string. Got value ${label.name}`);
    }

V
Vitaliy Nishukov 已提交
65 66
    if (!['number', 'undefined'].includes(typeof label.id)) {
        throw new Error(
67
            `Label "${label.name}". Type of label id must be only a number or undefined. Got value ${label.id}`,
V
Vitaliy Nishukov 已提交
68
        );
69 70
    }

V
Vitaliy Nishukov 已提交
71
    if (typeof label.color !== 'string') {
72
        throw new Error(`Label "${label.name}". Label color must be a string. Got ${typeof label.color}`);
B
Boris Sekachev 已提交
73 74
    }

75
    if (!label.color.match(/^#[0-9a-fA-F]{6}$|^$/)) {
V
Vitaliy Nishukov 已提交
76 77 78 79
        throw new Error(
            `Label "${label.name}". ` +
                `Type of label color must be only a valid color string. Got value ${label.color}`,
        );
D
Dmitry Kalinin 已提交
80 81
    }

82
    if (!Array.isArray(label.attributes)) {
83
        throw new Error(`Label "${label.name}". Attributes must be an array. Got type ${typeof label.attributes}`);
84 85 86 87 88 89 90
    }

    for (const attr of label.attributes) {
        validateParsedAttribute(attr);
    }
}

91 92 93 94 95 96 97 98 99 100 101 102 103
export function idGenerator(): number {
    return --id;
}

export function equalArrayHead(arr1: string[], arr2: string[]): boolean {
    for (let i = 0; i < arr1.length; i++) {
        if (arr1[i] !== arr2[i]) {
            return false;
        }
    }

    return true;
}