Skip to content

Commit eac1235

Browse files
Merge pull request #5 from joaopaulomoraes/refactor/improve-regex
Improve regex
2 parents 2f84937 + cf3a5af commit eac1235

File tree

4 files changed

+174
-51
lines changed

4 files changed

+174
-51
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-object-keys",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Manage complex object keys in depth",
55
"author": {
66
"name": "João Paulo Moraes",

src/remove/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ export function remove<O extends JSObject, K extends keyof O>(object: O, keys: K
1717
}
1818

1919
function withoutKey<O extends JSObject, K extends string>(object: O, key: K) {
20-
const regex = new RegExp(`"${key}":[^,}]+,?`, 'g')
20+
const regex = new RegExp(`"${key}":[^,}]+,?|,?"${key}":[^,}]+`, 'g')
2121
return withRegex(object, regex)
2222
}
2323

2424
function withoutKeys<O extends JSObject, K extends keyof O>(object: O, keys: K[]) {
25-
const regex = new RegExp(`"(${keys.join('|')})":[^,}]+,?`, 'g')
25+
const params = keys.join('|')
26+
const regex = new RegExp(`"(${params})":[^,}]+,?|,?"(${params})":[^,}]+`, 'g')
2627
return withRegex(object, regex)
2728
}
2829

src/remove/test.ts

Lines changed: 169 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,184 @@
11
import { remove } from '.'
22

3+
const user = {
4+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
5+
name: 'John Doe',
6+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
7+
8+
age: 29
9+
}
10+
11+
const userNodes = {
12+
...user,
13+
node: [
14+
{
15+
...user,
16+
node: {
17+
...user,
18+
node: {
19+
...user
20+
}
21+
}
22+
},
23+
{
24+
...user,
25+
node: {
26+
...user
27+
}
28+
},
29+
{
30+
...user,
31+
node: {
32+
...user,
33+
node: {
34+
...user
35+
}
36+
}
37+
}
38+
]
39+
}
40+
341
describe('remove', () => {
4-
it('should remove a specific key', () => {
5-
expect(remove({ id: 1, foo: 2, bar: 3 }, 'foo')).toStrictEqual({
6-
bar: 3,
7-
id: 1
42+
it('should remove the id from user', () => {
43+
expect(remove(user, 'id')).toStrictEqual({
44+
name: 'John Doe',
45+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
46+
47+
age: 29
848
})
949
})
1050

11-
it('should remove a specific key from different levels', () => {
12-
expect(
13-
remove(
51+
it('should remove the category from user', () => {
52+
expect(remove(user, 'category')).toStrictEqual({
53+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
54+
name: 'John Doe',
55+
56+
age: 29
57+
})
58+
})
59+
60+
it('should remove category and email from user', () => {
61+
expect(remove(user, ['category', 'email'])).toStrictEqual({
62+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
63+
name: 'John Doe',
64+
age: 29
65+
})
66+
})
67+
68+
it('should remove age from user', () => {
69+
expect(remove(user, 'age')).toStrictEqual({
70+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
71+
name: 'John Doe',
72+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
73+
74+
})
75+
})
76+
77+
it('should remove id and age from user', () => {
78+
expect(remove(user, ['id', 'age'])).toStrictEqual({
79+
name: 'John Doe',
80+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
81+
82+
})
83+
})
84+
85+
it('should remove id from userNodes', () => {
86+
expect(remove(userNodes, 'id')).toStrictEqual({
87+
name: 'John Doe',
88+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
89+
90+
age: 29,
91+
node: [
1492
{
15-
id: 1,
16-
foo: 1,
17-
children: {
18-
id: 2,
19-
foo: 2,
20-
children: {}
93+
name: 'John Doe',
94+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
95+
96+
age: 29,
97+
node: {
98+
name: 'John Doe',
99+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
100+
101+
age: 29,
102+
node: {
103+
name: 'John Doe',
104+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
105+
106+
age: 29
107+
}
21108
}
22109
},
23-
'foo'
24-
)
25-
).toStrictEqual({
26-
children: {
27-
children: {},
28-
id: 2
29-
},
30-
id: 1
110+
{
111+
name: 'John Doe',
112+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
113+
114+
age: 29,
115+
node: {
116+
name: 'John Doe',
117+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
118+
119+
age: 29
120+
}
121+
},
122+
{
123+
name: 'John Doe',
124+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
125+
126+
age: 29,
127+
node: {
128+
name: 'John Doe',
129+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
130+
131+
age: 29,
132+
node: {
133+
name: 'John Doe',
134+
category: '65bf5579-710d-4f56-9907-8c0bb1b2f0d2',
135+
136+
age: 29
137+
}
138+
}
139+
}
140+
]
31141
})
32142
})
33143

34-
it('should remove multiple keys', () => {
35-
expect(remove({ id: 1, foo: 2, bar: 3, baz: [] }, ['foo', 'bar'])).toStrictEqual({ baz: [], id: 1 })
36-
})
37-
})
38-
39-
it('should remove multiple keys from different levels', () => {
40-
expect(
41-
remove(
42-
{
43-
id: 1,
44-
foo: 1,
45-
bar: 1,
46-
children: {
47-
id: 2,
48-
foo: 2,
49-
bar: 2,
50-
children: {}
144+
it('should remove id and node from userNodes', () => {
145+
expect(remove(userNodes, ['email', 'category', 'age'])).toStrictEqual({
146+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
147+
name: 'John Doe',
148+
node: [
149+
{
150+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
151+
name: 'John Doe',
152+
node: {
153+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
154+
name: 'John Doe',
155+
node: {
156+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
157+
name: 'John Doe'
158+
}
159+
}
160+
},
161+
{
162+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
163+
name: 'John Doe',
164+
node: {
165+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
166+
name: 'John Doe'
167+
}
168+
},
169+
{
170+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
171+
name: 'John Doe',
172+
node: {
173+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
174+
name: 'John Doe',
175+
node: {
176+
id: 'e059d01a-7082-4b63-9c70-997491cdcf7c',
177+
name: 'John Doe'
178+
}
179+
}
51180
}
52-
},
53-
['foo', 'bar']
54-
)
55-
).toStrictEqual({
56-
children: {
57-
children: {},
58-
id: 2
59-
},
60-
id: 1
181+
]
182+
})
61183
})
62184
})

0 commit comments

Comments
 (0)