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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
function validateParsedAttribute(attr: Attribute): void {
    if (typeof (attr.name) !== 'string') {
        throw new Error(`Type of attribute name must be a string. Got value ${attr.name}`);
    }

    if (!['number', 'undefined'].includes(typeof (attr.id))) {
        throw new Error(`Attribute: "${attr.name}". `
        + `Type of attribute id must be a number or undefined. Got value ${attr.id}`);
    }

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

    if (typeof (attr.mutable) !== 'boolean') {
        throw new Error(`Attribute: "${attr.name}". `
        + `Mutable flag must be a boolean value. Got value ${attr.mutable}`);
    }

    if (!Array.isArray(attr.values)) {
        throw new Error(`Attribute: "${attr.name}". `
        + `Attribute values must be an array. Got type ${typeof (attr.values)}`);
    }

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


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    for (const value of attr.values) {
        if (typeof (value) !== 'string') {
            throw new Error(`Attribute: "${attr.name}". `
            + `Each value must be a string. Got value ${value}`);
        }
    }
}

export function validateParsedLabel(label: Label): void {
    if (typeof (label.name) !== 'string') {
        throw new Error(`Type of label name must be a string. Got value ${label.name}`);
    }

    if (!['number', 'undefined'].includes(typeof (label.id))) {
        throw new Error(`Label "${label.name}". `
        + `Type of label id must be only a number or undefined. Got value ${label.id}`);
    }

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

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

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

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

90 91 92 93 94 95 96 97 98 99 100 101 102
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;
}