All files / src/lib/services state-util.ts

81.48% Statements 22/27
12.5% Branches 1/8
60% Functions 6/10
78.26% Lines 18/23

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 3961x 61x 61x   61x             61x         61x       61x 38x 124x 251x 70x   54x     61x 61x 38x 78x 78x     78x    
import * as fs from 'fs';
import * as mime from 'mime';
import * as YAML from 'js-yaml';
 
export const readFile: (file: string) => Promise<string> = (file) =>
    new Promise((resolve, reject) =>
        fs.readFile(file, (err, v) =>
            err ? reject(err) : resolve(v.toString('utf-8')),
        ),
    );
 
export const parseFile: (file: string, data: string) => any = (
    file: string,
    data: string,
) => (mime.getType(file) === 'text/yaml' ? YAML.load(data) : JSON.parse(data));
 
export const filterExisting: (
    keepExisting: boolean,
    existingArray: any[],
) => (item: any) => boolean =
    (keepExisting, existingArray = []) =>
    (item) => {
        if (keepExisting) {
            const found = existingArray.find((t) => t.name === item.name);
            return !found;
        }
        return true;
    };
 
export const filterEqual: (existingArray: any[]) => (item: any) => boolean =
    (existingArray = []) =>
    (item) => {
        const toggle = existingArray.find((t) => t.name === item.name);
        Iif (toggle) {
            return JSON.stringify(toggle) !== JSON.stringify(item);
        }
        return true;
    };