Passed
Push — main ( 411ece...81a98c )
by Yuri
01:09 queued 15s
created

src/index.ts   A

Complexity

Total Complexity 11
Complexity/F 3.67

Size

Lines of Code 42
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 11
mnd 8
bc 8
fnc 3
bpm 2.6666
cpm 3.6666
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.ts ➔ isNonSortableObject 0 13 1
1
/**
2
 * Function to sort JSON data.
3
 * @param {unknown} data - The data to be sorted.
4
 * @param {boolean} [asc=true] - Whether to sort in ascending order.
5
 * @returns {unknown} The sorted data.
6
 */
7 1
export function sort(data: unknown, asc: boolean = true): unknown {
8 105
  if (!data || typeof data !== 'object') {
9 61
    return data;
10
  }
11
12 44
  if (Array.isArray(data)) {
13 27
    return data.map(item => sort(item, asc));
14
  }
15
16 35
  if (isNonSortableObject(data)) {
17 11
    return data;
18
  }
19
20 24
  const sortedEntries = Object.entries(data as Record<string, unknown>)
21 52
    .sort(([a], [b]) => asc ? a.localeCompare(b) : b.localeCompare(a));
22
  
23 24
  return Object.fromEntries(
24 60
    sortedEntries.map(([key, value]) => [key, sort(value, asc)])
25
  );
26
}
27
28
function isNonSortableObject(obj: object): boolean {
29 35
  return (
30
    obj instanceof Date ||
31
    obj instanceof RegExp ||
32
    obj instanceof Function ||
33
    obj instanceof Error ||
34
    obj instanceof Map ||
35
    obj instanceof Set ||
36
    obj instanceof WeakMap ||
37
    obj instanceof WeakSet ||
38
    obj instanceof Promise ||
39
    Symbol.iterator in Object(obj)
40
  );
41
}
42