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

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

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

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

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

×
8
export function isNull(value: any): value is null {
9
    return value === null;
10
}
×
11

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

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

×
20
export function isEmpty(value?: any): boolean {
21
    return !(isArray(value) && value.length > 0);
22
}
×
23

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

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

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

×
38
    if (value == null) {
×
39
        return value === undefined ? '[object Undefined]' : '[object Null]';
×
40
    }
×
41
    if (!(symToStringTag && symToStringTag in Object(value))) {
42
        return toString.call(value);
43
    }
×
44
    const isOwn = hasOwnProperty.call(value, symToStringTag);
×
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);
×
53
    if (unmasked) {
54
        if (isOwn) {
55
            value[symToStringTag] = tag;
×
56
        } else {
57
            delete value[symToStringTag];
58
        }
59
    }
60
    return result;
×
61
}
×
62

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

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

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

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

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

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

×
97
export function set(object: any, path: string, value: any): void {
×
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];
×
108
        if (isObject(nested)) {
109
            if (index === lastIndex) {
110
                nested[key] = value;
×
111
                nested = nested[key];
112
                break;
113
            } else {
×
114
                if (nested[key] == null) {
115
                    nested[key] = {};
116
                }
×
117
            }
×
118
        }
×
119
        nested = nested[key];
×
120
    }
×
121
    return object;
×
122
}
123

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

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

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

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

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

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

×
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 => {
×
183
        const keyValue = item[key];
184
        (result as any)[keyValue] = item;
185
    });
×
186
    return result;
×
187
}
×
188

×
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
}
×
197

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

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

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

3!
217
export function isTemplateRef<C = any>(value: any): value is TemplateRef<C> {
218
    return value instanceof TemplateRef;
219
}
×
220

×
221
export function isHTMLElement(value: any): value is HTMLElement {
222
    return value instanceof HTMLElement;
223
}
×
224

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

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

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

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

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

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

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

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

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

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

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

×
272
    // eslint-disable-next-line @typescript-eslint/prefer-for-of
×
273
    for (let idx = 0; idx < keysA.length; idx++) {
274
        const key = keysA[idx];
×
275
        if (!bHasOwnProperty(key)) {
×
276
            return false;
×
277
        }
×
278
        if (objA[key] !== objB[key]) {
279
            return false;
280
        }
×
281
    }
×
282

283
    return true;
×
284
}
285

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

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

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

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

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

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