• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

maxwroc / battery-state-card / 7358458641

29 Dec 2023 05:07PM UTC coverage: 91.909% (+3.4%) from 88.481%
7358458641

push

github

web-flow
Merge pull request #621 from maxwroc/NotExistsOperator

Added not_exists filter operator

324 of 368 branches covered (0.0%)

Branch coverage included in aggregate %.

403 of 423 relevant lines covered (95.27%)

20.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.45
/src/utils.ts
1
export const printVersion = () => console.info(
21✔
2
    "%c BATTERY-STATE-CARD %c [VI]{version}[/VI]",
3
    "color: white; background: forestgreen; font-weight: 700;",
4
    "color: forestgreen; background: white; font-weight: 700;",
5
);
6

7
/**
8
 * Logs message in developer console
9
 * @param message Message to log
10
 * @param level Message level/importance
11
 */
12
export const log = (message: string, level: "warn" | "error" = "warn") => {
21✔
13
    console[level]("[battery-state-card] " + message);
15✔
14
}
15

16
/**
17
 * Checks whether given value is a number
18
 * @param val String value to check
19
 */
20
export const isNumber = (value: string | number): boolean =>
21✔
21
    {
22
        return (value!== undefined && value !== null && value !== '' && !isNaN(Number(value)))
450✔
23
    }
24
/**
25
 * Returns array of values regardles if given value is string array or null
26
 * @param val Value to process
27
 */
28
export const safeGetArray = <T>(val: T | T[] | undefined): T[] => {
21✔
29
    if (Array.isArray(val)) {
71✔
30
        return val;
45✔
31
    }
32

33
    return val !== undefined ? [val] : [];
26✔
34
};
35

36
/**
37
 * Converts config value to array of specified objects.
38
 * 
39
 * ISimplifiedArray config object supports simple list of strings or even an individual item. This function 
40
 * ensures we're getting an array in all situations.
41
 * 
42
 * E.g. all of the below are valid entries and can be converted to objects
43
 * 1. Single string
44
 *   my_setting: "name"
45
 * 2. Single object
46
 *   my_setting:
47
 *     by: "name"
48
 *     desc: true
49
 * 3. Array of strings
50
 *   my_setting:
51
 *     - "name"
52
 *     - "state"
53
 * 4. Array of objects
54
 *   my_setting:
55
 *     - by: "name"
56
 *     - by: "sort"
57
 *       desc: true
58
 * 
59
 * @param value Config array
60
 * @param defaultKey Key of the object to populate
61
 * @returns Array of objects
62
 */
63
export const safeGetConfigArrayOfObjects = <T>(value: ISimplifiedArray<T>, defaultKey: keyof T): T[] => {
21✔
64
    return safeGetArray(value).map(v => safeGetConfigObject(v, defaultKey));
95✔
65
}
66

67
/**
68
 * Converts string to object with given property or returns the object if it is not a string
69
 * @param value Value from the config
70
 * @param propertyName Property name of the expected config object to which value will be assigned
71
 */
72
 export const safeGetConfigObject = <T>(value: IObjectOrString<T>, propertyName: keyof T): T => {
21✔
73

74
    switch (typeof value) {
95✔
75
        case "string":
95✔
76
            const result = <any>{};
27✔
77
            result[propertyName] = value;
27✔
78
            return result;
27✔
79
        case "object":
80
            // make a copy as the original one is immutable
81
            return { ...value };
68✔
82
    }
83

84
    return value;
×
85
}
86

87
/**
88
 * Throttles given function calls. In given throttle time always the last arriving call is executed.
89
 * @param func Function to call
90
 * @param throttleMs Number of ms to wait before calling
91
 */
92
export const throttledCall = function <T extends Function>(func: T, throttleMs: number): T {
21✔
93
    let timeoutHook: any;
94
    return (<any>((...args: []) => {
213✔
95
        if (timeoutHook) {
8!
96
            // cancel previous call
97
            clearTimeout(timeoutHook);
×
98
            timeoutHook = null;
×
99
        }
100

101
        // schedule new call
102
        timeoutHook = setTimeout(() => func.apply(null, args), 100);
8✔
103
    })) as T
104
}
105

106

107
const regexPattern = /\/(.*?)\/([igm]{1,3})/
21✔
108
/**
109
 * Extracts regex from the given string
110
 * @param ruleVal Value to process
111
 * @returns Parsed regex
112
 */
113
export const getRegexFromString = (ruleVal: string): RegExp | null => {
21✔
114
    if (ruleVal[0] == "/" && ruleVal[ruleVal.length - 1] == "/") {
29✔
115
        return new RegExp(ruleVal.substr(1, ruleVal.length - 2));
10✔
116
    }
117
    else { 
118
        let matches = ruleVal.match(regexPattern)
19✔
119
        if (matches && matches.length == 3) {
19✔
120
            return new RegExp(matches[1], matches[2]);
4✔
121
        }
122
    }
123

124
    return null;
15✔
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc