common.ts 2.9 KB
Newer Older
M
manasars 已提交
1
// Copyright (C) 2021 Intel Corporation
B
Boris Sekachev 已提交
2 3 4
//
// 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
    if (typeof attr.mutable !== 'boolean') {
M
manasars 已提交
38
        throw new Error(`Attribute: "${attr.name}". Mutable flag must be a boolean value. Got value ${attr.mutable}`);
39 40 41
    }

    if (!Array.isArray(attr.values)) {
M
manasars 已提交
42
        throw new Error(`Attribute: "${attr.name}". Attribute values must be an array. Got type ${typeof attr.values}`);
43 44
    }

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

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

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

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

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

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

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

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

87 88 89 90 91 92 93 94 95 96 97 98 99
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;
}