All files / generator utils.ts

97.44% Statements 38/39
100% Branches 25/25
83.33% Functions 5/6
97.22% Lines 35/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 821x 1x   1x           1x                 1x 388x 47x 341x 19x 322x 168x 154x 70x 84x 14x 70x 61x   9x                 1x 81x   8x     24x   44x   3x   1x   1x                       240x 240x 97x 97x 214x 170x   44x     143x     1x  
import * as _ from 'lodash';
import { ValueTypeEnum } from './enums/value-type.enum';
 
export class Utils {
 
    /**
     * Read and return JSON from file path.
     * @param {string} filePath a string or a path to the file.
     */
    static getJson(filePath: string) {
        console.log('TODO return the json from file path');
    }
 
    /**
     * Get the type of value as a string.
     * @param value the value to get the type as string
     * @return {string} the type of the value.
     */
    static getType(value: any): string {
        if (_.isArray(value)) {
            return ValueTypeEnum.ARRAY;
        } else if (_.isBoolean(value)) {
            return ValueTypeEnum.BOOLEAN;
        } else if (_.isString(value)) {
            return ValueTypeEnum.STRING;
        } else if (_.isInteger(value)) {
            return ValueTypeEnum.INTEGER;
        } else if (_.isNumber(value)) {
            return ValueTypeEnum.NUMBER;
        } else if (_.isObject(value)) {
            return ValueTypeEnum.OBJECT;
        } else {
            return ValueTypeEnum.NULL;
        }
    }
 
    /**
     * Return default value depending on type.
     * @param {string} type
     * @return {any}
     */
    static getDefaultValue(type: string): any {
        switch (type) {
            case ValueTypeEnum.BOOLEAN:
                return false;
            case ValueTypeEnum.NUMBER:
            case ValueTypeEnum.INTEGER:
                return 0;
            case ValueTypeEnum.STRING:
                return '';
            case ValueTypeEnum.NULL:
                return null;
            case ValueTypeEnum.ARRAY:
                return [];
            case ValueTypeEnum.OBJECT:
                return {};
        }
    }
 
    /**
     * Test deep equality between two object with the possibility to omit recursively
     * some properties.
     * @param obj1: the first object to compare.
     * @param obj2: the second object to compare.
     * @param {string} omits: one or many names of properties to omit recursively.
     * @return {boolean}: whether the two object are identical or not.
     */
    static isEqualWithout(obj1: any, obj2: any, ...omits: string[]): boolean {
        if (_.isObject(obj1) && _.isObject(obj2)) {
            const keys = Object.keys(obj1);
            return keys.every(k => {
                if (omits.indexOf(k) === -1) {
                    return Utils.isEqualWithout(obj1[k], obj2[k], ...omits);
                }
                return true;
            });
        } else {
            return obj1 === obj2;
        }
    }
}