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

atinc / ngx-tethys / cf65e7d1-9df0-45a5-a655-00fd832d8cdd

10 Apr 2024 07:39AM UTC coverage: 90.386% (+0.02%) from 90.364%
cf65e7d1-9df0-45a5-a655-00fd832d8cdd

Pull #3071

circleci

FrankWang117
fix(cascader): remove console
Pull Request #3071: feat(cascader): support custom options #INFR-12126

5435 of 6659 branches covered (81.62%)

Branch coverage included in aggregate %.

10 of 10 new or added lines in 2 files covered. (100.0%)

35 existing lines in 6 files now uncovered.

13170 of 13925 relevant lines covered (94.58%)

983.5 hits per line

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

86.28
/src/util/helpers/helpers.ts
1
import { _isNumberValue, coerceCssPixelValue as coerceCssPixel } from '@angular/cdk/coercion';
2
import { ElementRef, TemplateRef } from '@angular/core';
3

4
export function isUndefined(value: any): value is undefined {
4,265✔
5
    return value === undefined;
6
}
7

1,111✔
8
export function isNull(value: any): value is null {
9
    return value === null;
10
}
4,228✔
11

12
export function isUndefinedOrNull(value: any): value is undefined | null {
13
    return isUndefined(value) || isNull(value);
44,536✔
14
}
15

16
export function isArray<T = any>(value: any): value is Array<T> {
20,016✔
17
    return value && baseGetTag(value) === '[object Array]';
18
}
19

18,488✔
20
export function isEmpty(value?: any): boolean {
21
    return !(isArray(value) && value.length > 0);
22
}
15,646✔
23

24
export function isString(value?: any): value is string {
25
    return typeof value == 'string' || (!isArray(value) && isObjectLike(value) && baseGetTag(value) === '[object String]');
12,039✔
26
}
12,039✔
27

12,039✔
28
function isObjectLike(value: any): value is object {
12,039!
29
    return value !== null && typeof value === 'object';
12,039!
30
}
×
31

32
function baseGetTag(value: any) {
12,039✔
33
    const objectProto = Object.prototype;
12,037✔
34
    const hasOwnProperty = objectProto.hasOwnProperty;
35
    const toString = objectProto.toString;
2✔
36
    const symToStringTag = typeof Symbol !== 'undefined' ? Symbol.toStringTag : undefined;
2✔
37

2✔
38
    if (value == null) {
2✔
39
        return value === undefined ? '[object Undefined]' : '[object Null]';
2✔
40
    }
×
41
    if (!(symToStringTag && symToStringTag in Object(value))) {
42
        return toString.call(value);
43
    }
2✔
44
    const isOwn = hasOwnProperty.call(value, symToStringTag);
2!
45
    const tag = value[symToStringTag];
×
46
    let unmasked = false;
×
47
    try {
48
        value[symToStringTag] = undefined;
49
        unmasked = true;
×
50
    } catch (e) {}
51

52
    const result = toString.call(value);
2✔
53
    if (unmasked) {
54
        if (isOwn) {
55
            value[symToStringTag] = tag;
970✔
56
        } else {
57
            delete value[symToStringTag];
58
        }
59
    }
60
    return result;
516✔
61
}
516✔
62

63
export function isNumber(value: any): value is number {
64
    return typeof value === 'number' || (isObjectLike(value) && baseGetTag(value) === '[object Number]');
1,262,090✔
65
}
1,262,090✔
66

67
export function isObject(value: any): value is object {
68
    // Avoid a V8 JIT bug in Chrome 19-20.
9✔
69
    // See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
9✔
70
    const type = typeof value;
71
    return !!value && (type === 'object' || type === 'function');
72
}
624✔
73

74
export function isFunction(value: any): value is Function {
75
    const type = typeof value;
7,764✔
76
    return !!value && type === 'function';
7,764✔
77
}
7,764✔
78

904✔
79
export function isDate(value: any): value is Date {
80
    const type = typeof value;
7,764✔
81
    return !!value && type === 'object' && !!value.getTime;
82
}
83

439!
84
export function coerceArray<T>(value: T | T[]): T[] {
×
85
    return Array.isArray(value) ? value : [value];
86
}
439✔
87

439✔
88
export function get(object: any, path: string, defaultValue?: any): any {
439✔
89
    const paths = path.split('.');
439✔
90
    let result = object[paths.shift()];
439✔
91
    while (result && paths.length) {
439✔
92
        result = result[paths.shift()];
439✔
93
    }
439!
94
    return result === undefined ? defaultValue : result;
439!
95
}
439✔
96

439✔
97
export function set(object: any, path: string, value: any): void {
439✔
98
    if (object == null) {
99
        return object;
100
    }
×
101
    const paths = path.split('.');
×
102
    let index = -1;
103
    const length = paths.length;
104
    const lastIndex = length - 1;
105
    let nested = object;
×
106
    while (nested !== null && ++index < length) {
107
        const key = paths[index];
439✔
108
        if (isObject(nested)) {
109
            if (index === lastIndex) {
110
                nested[key] = value;
8✔
111
                nested = nested[key];
112
                break;
113
            } else {
×
114
                if (nested[key] == null) {
115
                    nested[key] = {};
116
                }
28!
117
            }
28✔
118
        }
28✔
119
        nested = nested[key];
3✔
120
    }
3✔
121
    return object;
1✔
122
}
123

2✔
124
export function isBoolean(value: any): value is boolean {
1✔
125
    return value === true || value === false || (isObjectLike(value) && baseGetTag(value) === '[object Boolean]');
126
}
1!
127

×
128
export function isClass(value: any): value is Function {
129
    return isFunction(value) && /^\s*class\s+/.test(value.toString());
130
}
131

26✔
132
export function htmlElementIsEmpty(element: HTMLElement) {
133
    if (element && element.childNodes) {
134
        const nodes = element.childNodes;
38✔
135
        for (let i = 0; i < nodes.length; i++) {
38✔
136
            const node = nodes[i];
38✔
137
            if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).outerHTML.toString().trim().length !== 0) {
38!
138
                return false;
×
139
            } else if (node.nodeType === Node.TEXT_NODE && node.textContent.toString().trim().length !== 0) {
38✔
140
                return false;
38✔
141
            } else if (node.nodeType !== Node.COMMENT_NODE) {
38✔
142
                return false;
38✔
143
            }
32✔
144
        }
145
    }
146
    return true;
6✔
147
}
148

149
export function hexToRgb(hexValue: string, alpha?: number): string {
150
    const rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
3✔
151
    const hex = hexValue.replace(rgx, (m: any, r: any, g: any, b: any) => r + r + g + g + b + b);
2✔
152
    const rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
1✔
153
    if (!rgb) return;
154
    const resultR = parseInt(rgb[1], 16);
155
    const resultG = parseInt(rgb[2], 16);
1✔
156
    const resultB = parseInt(rgb[3], 16);
157
    if (!isUndefinedOrNull(alpha)) {
158
        return `rgba(${resultR}, ${resultG}, ${resultB}, ${alpha})`;
159
    } else {
1✔
160
        return `rgb(${resultR}, ${resultG}, ${resultB})`;
161
    }
162
}
14✔
163

28✔
164
export function dateToUnixTimestamp(date: Date | number): number {
165
    if (isNumber(date)) {
166
        if (date.toString().length === 10) {
31✔
167
            return date as number;
31✔
168
        } else {
60✔
169
            return parseInt(((date as number) / 1000).toFixed(0), 10);
60✔
170
        }
171
    } else {
31✔
172
        return parseInt(((date as Date).getTime() / 1000).toFixed(0), 10);
173
    }
174
}
5✔
175

5✔
176
export function clamp(value: number, min = 0, max = 100) {
9✔
177
    return Math.max(min, Math.min(max, value));
9✔
178
}
179

5✔
180
export function keyBy<T>(array: T[], key: T extends object ? keyof T : never): { [key: string]: T } {
181
    const result: { [key: string]: T } = {};
182
    array.forEach(item => {
8✔
183
        const keyValue = item[key];
184
        (result as any)[keyValue] = item;
185
    });
8!
186
    return result;
8✔
187
}
16✔
188

16✔
189
export function indexKeyBy<T>(array: T[], key: T extends object ? keyof T : never): { [key: string]: number } {
190
    const result: { [key: string]: number } = {};
191
    array.forEach((item, index) => {
192
        const keyValue = item[key];
×
193
        (result as any)[keyValue] = index;
194
    });
195
    return result;
196
}
34✔
197

198
function upperFirst(string: string): string {
199
    return string.slice(0, 1).toUpperCase() + string.slice(1);
14,860✔
200
}
201

202
export function camelCase(values: string[]): string {
7✔
203
    if (isArray(values)) {
204
        return values.reduce((result, word, index) => {
205
            word = word.toLowerCase();
7✔
206
            return result + (index ? upperFirst(word) : word);
207
        }, '');
208
    } else {
323✔
209
        return;
40✔
210
    }
211
}
212

283✔
213
export function generateRandomStr() {
214
    return Math.random().toString(36).substring(2);
215
}
617✔
216

622✔
217
export function isTemplateRef<C = any>(value: any): value is TemplateRef<C> {
218
    return value instanceof TemplateRef;
219
}
614✔
220

614✔
221
export function isHTMLElement(value: any): value is HTMLElement {
222
    return value instanceof HTMLElement;
223
}
21,588✔
224

225
export function isElementRef(value: any): value is ElementRef {
226
    return value instanceof ElementRef;
471✔
227
}
83✔
228

229
export function coerceBooleanProperty(value: boolean | string | number): boolean {
388✔
230
    if (value === '' || (value && value !== 'false')) {
18✔
231
        return true;
232
    } else {
370✔
233
        return false;
370✔
234
    }
370✔
235
}
222✔
236

237
export type FunctionProp<T> = (...args: any[]) => T;
148✔
238

148✔
239
export function coerceNumberValue(value: number | string): number;
162✔
240
export function coerceNumberValue<D>(value: number | string, fallback: D): number | D;
162!
UNCOV
241
export function coerceNumberValue(value: number | string, fallbackValue: number = 0): number {
×
242
    return _isNumberValue(value) ? Number(value) : fallbackValue;
243
}
162✔
244

147✔
245
export function coerceCssPixelValue(value: number | string): string {
246
    value = _isNumberValue(value) ? Number(value) : value;
247
    return coerceCssPixel(value);
1✔
248
}
249

12✔
250
export function valueFunctionProp<T>(prop: FunctionProp<T>, ...args: any[]): T {
331✔
251
    return typeof prop === 'function' ? prop(...args) : prop;
331✔
252
}
2✔
253

254
export function shallowEqual(objA?: Record<string, any>, objB?: Record<string, any>): boolean {
255
    if (objA === objB) {
329✔
256
        return true;
257
    }
331✔
258

111✔
259
    if (typeof objA !== 'object' || !objA || typeof objB !== 'object' || !objB) {
46✔
260
        return false;
261
    }
262

65✔
263
    const keysA = Object.keys(objA);
264
    const keysB = Object.keys(objB);
265

266
    if (keysA.length !== keysB.length) {
220✔
267
        return false;
268
    }
269

×
270
    const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
3!
UNCOV
271

×
272
    for (let idx = 0; idx < keysA.length; idx++) {
273
        const key = keysA[idx];
3✔
274
        if (!bHasOwnProperty(key)) {
3✔
275
            return false;
3✔
276
        }
3!
277
        if (objA[key] !== objB[key]) {
278
            return false;
279
        }
54✔
280
    }
5✔
281

282
    return true;
49✔
283
}
284

285
export function concatArray<TItem>(items: TItem | TItem[], originalItems: TItem | TItem[] = []): TItem[] {
286
    let _originalItems: TItem[] = [];
287
    if (!originalItems) {
288
        _originalItems = [];
289
    } else {
290
        _originalItems = coerceArray(originalItems);
291
    }
292
    if (items) {
293
        if (isArray(items)) {
294
            return [..._originalItems, ...items];
295
        } else {
296
            return [..._originalItems, items];
297
        }
298
    } else {
299
        return _originalItems;
300
    }
301
}
302

303
export function humanizeBytes(bytes: number, noSpace = false, fractionDigits = 1): string {
304
    if (bytes === 0) {
305
        return '0 Byte';
306
    }
307

308
    const k = 1024;
309
    const sizes: string[] = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
310
    const i: number = Math.floor(Math.log(bytes) / Math.log(k));
311

312
    return parseFloat((bytes / Math.pow(k, i)).toFixed(fractionDigits)) + (noSpace ? '' : ' ') + sizes[i];
313
}
314

315
export function isFloat(value: string): boolean {
316
    if (!value) {
317
        return false;
318
    }
319

320
    return /^[-+]?([0-9]+([.][0-9]*)?|[.][0-9]+)([Ee][-+]?[0-9]+)?$/.test(value);
321
}
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

© 2025 Coveralls, Inc