Skip to content

Commit 174514d

Browse files
committed
Include circular replacer utility + add more coverage for array tests
1 parent b733342 commit 174514d

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@makerxstudio/node-common",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"private": false,
55
"description": "A set of MakerX core NodeJS types and utilities",
66
"author": "MakerX",

src/array-helpers/distinct.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Distinct', () => {
55
it('Returns only the distinct items', () => {
66
const list = ['a', 'a', 'a', 'b', 'b', 'c']
77

8-
const distinctList = list.filter(distinct((x) => x))
8+
const distinctList = list.filter(distinct())
99

1010
expect(distinctList).toStrictEqual(['a', 'b', 'c'])
1111
})

src/array-helpers/sort-by.spec.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sortBy } from './sort-by'
1+
import { combineSortFn, sortBy, sortByIgnoreCase } from './sort-by'
22

33
describe('SortBy', () => {
44
describe('When sorting an unordered list', () => {
@@ -10,4 +10,95 @@ describe('SortBy', () => {
1010
expect(sorted).toStrictEqual(['a', 'b', 'c', 'd', 'e'])
1111
})
1212
})
13+
describe('When sorting an unordered list descending', () => {
14+
it('It orders the list descending', () => {
15+
const list = ['e', 'a', 'b', 'd', 'c']
16+
17+
const sorted = list.slice().sort(sortBy((x) => x, 'desc'))
18+
19+
expect(sorted).toStrictEqual(['e', 'd', 'c', 'b', 'a'])
20+
})
21+
})
22+
})
23+
24+
describe('SortByIgnoreCase', () => {
25+
describe('When sorting an unordered list', () => {
26+
it('It orders the list', () => {
27+
const list = ['e', 'a', 'B', 'D', 'c']
28+
29+
const sorted = list.slice().sort(sortByIgnoreCase((x) => x))
30+
31+
expect(sorted).toStrictEqual(['a', 'B', 'c', 'D', 'e'])
32+
})
33+
})
34+
})
35+
36+
describe('combineSortFn', () => {
37+
describe('When sorting an unordered list by multiple properties', () => {
38+
it('It applies sorting rules in order', () => {
39+
const users: Array<{ first: string; last: string; dob: Date }> = [
40+
{
41+
first: 'a',
42+
last: 'b',
43+
dob: new Date('2000-01-01'),
44+
},
45+
{
46+
first: 'z',
47+
last: 'a',
48+
dob: new Date('2000-01-01'),
49+
},
50+
{
51+
first: 'c',
52+
last: 'b',
53+
dob: new Date('2000-01-01'),
54+
},
55+
{
56+
first: 'a',
57+
last: 'a',
58+
dob: new Date('2000-01-01'),
59+
},
60+
{
61+
first: 'a',
62+
last: 'a',
63+
dob: new Date('2010-01-01'),
64+
},
65+
]
66+
67+
const sorted = users.slice().sort(
68+
combineSortFn(
69+
sortByIgnoreCase((x) => x.last),
70+
sortByIgnoreCase((x) => x.first),
71+
sortBy((x) => x.dob)
72+
)
73+
)
74+
75+
expect(sorted).toStrictEqual([
76+
{
77+
first: 'a',
78+
last: 'a',
79+
dob: new Date('2000-01-01'),
80+
},
81+
{
82+
first: 'a',
83+
last: 'a',
84+
dob: new Date('2010-01-01'),
85+
},
86+
{
87+
first: 'z',
88+
last: 'a',
89+
dob: new Date('2000-01-01'),
90+
},
91+
{
92+
first: 'a',
93+
last: 'b',
94+
dob: new Date('2000-01-01'),
95+
},
96+
{
97+
first: 'c',
98+
last: 'b',
99+
dob: new Date('2000-01-01'),
100+
},
101+
])
102+
})
103+
})
13104
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { getCircularReplacer } from './index'
2+
3+
describe('getCircularReplacer', () => {
4+
describe('When stringifying an object with a circular reference', () => {
5+
const objectA = {
6+
prop: 'string',
7+
b: {},
8+
}
9+
const objectB = {
10+
a: objectA,
11+
}
12+
objectA.b = objectB
13+
it('Sets circular references to undefined', () => {
14+
const str = JSON.stringify(objectA, getCircularReplacer())
15+
16+
expect(str).toBe(`{"prop":"string","b":{}}`)
17+
})
18+
})
19+
})

src/json-helpers/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Allows you to safely stringify objects which may contain circular references
3+
*
4+
* Usage:
5+
*
6+
* JSON.stringify(someObject, getCircularReplacer(), 2)
7+
*/
8+
export const getCircularReplacer = () => {
9+
const seen = new WeakSet()
10+
return (_: string, value: unknown) => {
11+
if (typeof value === 'object' && value !== null) {
12+
if (seen.has(value)) {
13+
return
14+
}
15+
seen.add(value)
16+
}
17+
if (typeof value === 'bigint') return value.toString()
18+
return value
19+
}
20+
}

0 commit comments

Comments
 (0)