..
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 | /**
* isArrayOfType check if the array contains only the specified type
* @param array: the array to be checked
* @param type: the type to be checked
* @returns true if the array contains only the specified type
*/
export function isArrayOfType(array: any[], type: string): boolean {
return array.filter((i) => typeof i === type).length === array.length;
}
/**
* range - returns an array of numbers from start to end
* @param start: the start of the range
* @param end: the end of the range
* @param step: the step of the range
* @returns an array of numbers from start to end
*/
export function range(
start: number,
end: number,
step = 1,
remove?: Array<number>
): Array<number> {
// Check if start, end and step are integers
if (
!Number.isInteger(start) ||
!Number.isInteger(end) ||
!Number.isInteger(step)
) {
throw new Error('range parameters must be integers');
}
const rangeArray: Array<number> = [];
// Start must be less than end
if (start > end) {
throw new Error('starting value must be less than end value');
}
// Step must be greater than 0
if (step <= 0) {
throw new Error('step must be greater than 0');
}
// Generate an array from start to end
for (let i = start; i <= end; i += step) {
if (remove) {
if (!remove.includes(i)) {
rangeArray.push(i);
}
} else {
rangeArray.push(i);
}
}
return rangeArray;
}
/**
* flattenJSON - converts a nested JSON object into a simple JSON object
* @param object: the object to be flattened
* @returns the flattened object
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function flattenJSON(data: any): any {
const result: any = {};
function recurse(cur: any, prop: string) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
// eslint-disable-next-line no-var
for (var i = 0, l = cur.length; i < l; i++)
recurse(cur[i], prop + '[' + i + ']');
if (l == 0) result[prop] = [];
} else {
let isEmpty = true;
for (const p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop + '.' + p : p);
}
if (isEmpty && prop) result[prop] = {};
}
}
recurse(data, '');
return result;
}
/**
* isValidJSONObject - checks if the object is a valid JSON object or a valid JSON string
* @param object: the object to be checked
* @returns true if the object is a valid JSON object or a valid JSON string
*/
export function isValidJSONObject(object: any): boolean {
try {
JSON.parse(JSON.stringify(object));
return true;
} catch (e) {
return false;
}
}
|
|