forked from readmeio/remove-undefined-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
119 lines (103 loc) · 3.46 KB
/
index.test.ts
File metadata and controls
119 lines (103 loc) · 3.46 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { describe, test, it, expect } from 'vitest';
import removeUndefinedObjects from '../src/index.js';
describe('typings', () => {
it('should not blow away typings from supplied objects', () => {
const obj = removeUndefinedObjects({
key: 'buster',
});
expect(obj).toBeDefined();
});
});
test('should leave primitives alone', () => {
expect(removeUndefinedObjects(1234)).toBe(1234);
expect(removeUndefinedObjects('1234')).toBe('1234');
expect(removeUndefinedObjects(null)).toBeNull();
expect(removeUndefinedObjects()).toBeUndefined();
expect(removeUndefinedObjects(undefined)).toBeUndefined();
});
test('should leave only truthy primitives alone when removeAllFalsy is true', () => {
expect(removeUndefinedObjects(1234, { removeAllFalsy: true })).toBe(1234);
expect(removeUndefinedObjects('1234', { removeAllFalsy: true })).toBe('1234');
expect(removeUndefinedObjects(null, { removeAllFalsy: true })).toBeUndefined();
expect(removeUndefinedObjects(undefined, { removeAllFalsy: true })).toBeUndefined();
});
test("should also remove '' and null values when removeAllFalsy is true", () => {
expect(removeUndefinedObjects({ value: 1234 }, { removeAllFalsy: true })).toStrictEqual({ value: 1234 });
expect(removeUndefinedObjects({ value: '1234' }, { removeAllFalsy: true })).toStrictEqual({ value: '1234' });
expect(removeUndefinedObjects({ value: null }, { removeAllFalsy: true })).toBeUndefined();
expect(removeUndefinedObjects({ value: undefined }, { removeAllFalsy: true })).toBeUndefined();
});
test('should not remove empty arrays when preserveEmptyArray is true', () => {
expect(removeUndefinedObjects({ value: [] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
expect(removeUndefinedObjects({ value: [undefined] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
expect(removeUndefinedObjects({ value: [null] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
});
test('should remove empty objects with only empty properties', () => {
const obj = {
a: {
b: {},
c: {
d: {},
},
},
};
expect(removeUndefinedObjects(obj)).toBeUndefined();
});
test('should remove empty objects with only undefined properties', () => {
const obj = {
a: {
b: undefined,
c: {
d: undefined,
},
},
};
expect(removeUndefinedObjects(obj)).toBeUndefined();
});
test('should remove empty arrays from within object', () => {
const obj = {
a: {
b: undefined,
c: {
d: undefined,
},
},
d: [1234, undefined],
e: [],
f: null,
g: [null, undefined, null],
};
expect(removeUndefinedObjects(obj)).toStrictEqual({
d: [1234],
f: null,
});
});
test('should remove empty arrays and falsy values from within object when removeAllFalsy is true', () => {
const obj = {
a: {
b: undefined,
c: {
d: undefined,
},
},
d: [1234, undefined],
e: [],
f: null,
g: [null, undefined, null],
};
expect(removeUndefinedObjects(obj, { removeAllFalsy: true })).toStrictEqual({
d: [1234],
});
});
test('should remove undefined and null values from arrays', () => {
expect(removeUndefinedObjects([undefined, undefined])).toBeUndefined();
expect(removeUndefinedObjects([null])).toBeUndefined();
expect(removeUndefinedObjects(['1234', null, undefined, { a: null, b: undefined }, ' ', ''])).toStrictEqual([
'1234',
{
a: null,
},
' ',
'',
]);
});