src/object-sorter.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 42
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 31
mnd 0
bc 0
fnc 4
dl 0
loc 42
ccs 14
cts 14
cp 1
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A object-sorter.ts ➔ collectObjectEntries 0 9 1
A object-sorter.ts ➔ createSortedObject 0 11 1
A object-sorter.ts ➔ sortObjectEntries 0 7 1
A object-sorter.ts ➔ sortObject 0 5 1
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