Skip to content

Commit 478af85

Browse files
feat: flat.filterOutParams takes more properties
also fixed error when passing undefined in an argumet thrown an error
1 parent 328ffc4 commit 478af85

File tree

4 files changed

+91
-23
lines changed

4 files changed

+91
-23
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from 'chai'
2+
import { filterOutParams } from './filter-out-params'
3+
4+
describe('filterOutParams', () => {
5+
const params = {
6+
name: 'John',
7+
surname: 'Doe',
8+
age: 31,
9+
'meta.description': 'very ugly',
10+
'meta.title': 'cto',
11+
'meta.otherInfo': 'he stinks',
12+
metadetaksamosone: 'this is a steroid',
13+
}
14+
15+
it('filters params for given property', () => {
16+
expect(filterOutParams(params, 'age')).to.deep.equal({
17+
name: 'John',
18+
surname: 'Doe',
19+
'meta.description': 'very ugly',
20+
'meta.title': 'cto',
21+
'meta.otherInfo': 'he stinks',
22+
metadetaksamosone: 'this is a steroid',
23+
})
24+
})
25+
26+
it('filter params for nested property', () => {
27+
expect(filterOutParams(params, 'meta')).to.deep.equal({
28+
name: 'John',
29+
surname: 'Doe',
30+
age: 31,
31+
metadetaksamosone: 'this is a steroid',
32+
})
33+
})
34+
35+
it('returns all objects when there is no match', () => {
36+
expect(filterOutParams(params, 'nothingIsThere')).to.deep.eq(params)
37+
})
38+
39+
it('filter by multiple properties when they are given', () => {
40+
expect(filterOutParams(params, ['name', 'meta'])).to.deep.equal({
41+
surname: 'Doe',
42+
age: 31,
43+
metadetaksamosone: 'this is a steroid',
44+
})
45+
})
46+
47+
it('does not throw an error when user passes undefined as a propertyPath', () => {
48+
expect(() => {
49+
filterOutParams(params, ['name', undefined as unknown as string])
50+
}).not.to.throw()
51+
})
52+
})

src/utils/flat/filter-out-params.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ import { FlattenParams } from './flat.types'
55
* @load ./filter-out-params.doc.md
66
* @memberof flat
77
* @param {FlattenParams} params
8-
* @param {string} property
8+
* @param {string | Array<string>} properties
99
* @returns {FlattenParams}
1010
*/
11-
const filterOutParams = (params: FlattenParams, property: string): FlattenParams => {
12-
const regex = propertyKeyRegex(property)
11+
const filterOutParams = (
12+
params: FlattenParams,
13+
properties: string | Array<string>,
14+
): FlattenParams => {
15+
const propertyArray = Array.isArray(properties) ? properties : [properties]
1316

14-
// filter all keys which starts with property
15-
return Object.keys(params)
16-
.filter(key => !key.match(regex))
17-
.reduce((memo, key) => ({
18-
...memo,
19-
[key]: (params[key] as string),
20-
}), {} as FlattenParams)
17+
return propertyArray
18+
.filter(propertyPath => !!propertyPath)
19+
.reduce((globalFiltered, propertyPath) => {
20+
const regex = propertyKeyRegex(propertyPath)
21+
22+
return Object.keys(globalFiltered)
23+
.filter(key => !key.match(regex))
24+
.reduce((memo, key) => ({
25+
...memo,
26+
[key]: (params[key] as string),
27+
}), {} as FlattenParams)
28+
}, params)
2129
}
2230

2331
export { filterOutParams }

src/utils/flat/select-params.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ describe('selectParams', () => {
4646
'meta.otherInfo': 'he stinks',
4747
})
4848
})
49+
50+
it('does not throw an error when user passes undefined as a propertyPath', () => {
51+
expect(() => {
52+
selectParams(params, ['name', undefined as unknown as string])
53+
}).not.to.throw()
54+
})
4955
})

src/utils/flat/select-params.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ const selectParams = (
1515
options?: GetOptions,
1616
): FlattenParams => {
1717
const propertyArray = Array.isArray(properties) ? properties : [properties]
18-
const selected = propertyArray.reduce((globalMemo, propertyPath) => {
19-
const regex = propertyKeyRegex(propertyPath, options)
20-
const filtered = Object.keys(params)
18+
const selected = propertyArray
19+
.filter(propertyPath => !!propertyPath)
20+
.reduce((globalMemo, propertyPath) => {
21+
const regex = propertyKeyRegex(propertyPath, options)
22+
const filtered = Object.keys(params)
2123
// filter all keys which starts with property path
22-
.filter(key => key.match(regex))
23-
.reduce((memo, key) => ({
24-
...memo,
25-
[key]: (params[key] as string),
26-
}), {} as FlattenParams)
27-
return {
28-
...globalMemo,
29-
...filtered,
30-
}
31-
}, {} as FlattenParams)
24+
.filter(key => key.match(regex))
25+
.reduce((memo, key) => ({
26+
...memo,
27+
[key]: (params[key] as string),
28+
}), {} as FlattenParams)
29+
return {
30+
...globalMemo,
31+
...filtered,
32+
}
33+
}, {} as FlattenParams)
3234
return selected
3335
}
3436

0 commit comments

Comments
 (0)