Total Complexity | 4 |
Complexity/F | 1 |
Lines of Code | 42 |
Function Count | 4 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 0 |
1 | import { SortOptions, ObjectType, SortedEntry } from './types'; |
||
2 | 1 | import { compareObjectKeys } from './comparators'; |
|
3 | |||
4 | 1 | export function sortObject(obj: ObjectType, options: SortOptions): ObjectType { |
|
5 | 1398 | const entries = collectObjectEntries(obj); |
|
6 | 1398 | const sortedEntries = sortObjectEntries(entries, options.ascending); |
|
7 | 1398 | return createSortedObject(sortedEntries, options); |
|
8 | } |
||
9 | |||
10 | // Object entry handling |
||
11 | function collectObjectEntries(obj: ObjectType): SortedEntry[] { |
||
12 | 1398 | const stringEntries = Object.entries(obj); |
|
13 | 1398 | const symbolEntries = Object.getOwnPropertySymbols(obj).map( |
|
14 | 5 | (symbol) => [symbol, obj[symbol]] as SortedEntry |
|
15 | ); |
||
16 | |||
17 | 1398 | return [...stringEntries, ...symbolEntries]; |
|
18 | } |
||
19 | |||
20 | function sortObjectEntries( |
||
21 | entries: SortedEntry[], |
||
22 | ascending: boolean |
||
23 | ): SortedEntry[] { |
||
24 | 1398 | return entries.sort(([keyA], [keyB]) => |
|
25 | 102 | compareObjectKeys(keyA, keyB, ascending) |
|
26 | ); |
||
27 | } |
||
28 | |||
29 | function createSortedObject( |
||
30 | entries: SortedEntry[], |
||
31 | options: SortOptions |
||
32 | ): ObjectType { |
||
33 | 1472 | const sortedEntries = entries.map(([key, value]) => [ |
|
34 | key, |
||
35 | sortRecursively(value, options), |
||
36 | ]); |
||
37 | |||
38 | 59 | return Object.fromEntries(sortedEntries); |
|
39 | } |
||
40 | |||
41 | import { sortRecursively } from './core'; |
||
42 |