Skip to content

Commit

Permalink
feat: flat.filterOutParams takes more properties
Browse files Browse the repository at this point in the history
also fixed error when passing undefined in an argumet thrown an error
  • Loading branch information
wojtek-krysiak committed Oct 24, 2020
1 parent 328ffc4 commit 478af85
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
52 changes: 52 additions & 0 deletions src/utils/flat/filter-out-params.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { expect } from 'chai'
import { filterOutParams } from './filter-out-params'

describe('filterOutParams', () => {
const params = {
name: 'John',
surname: 'Doe',
age: 31,
'meta.description': 'very ugly',
'meta.title': 'cto',
'meta.otherInfo': 'he stinks',
metadetaksamosone: 'this is a steroid',
}

it('filters params for given property', () => {
expect(filterOutParams(params, 'age')).to.deep.equal({
name: 'John',
surname: 'Doe',
'meta.description': 'very ugly',
'meta.title': 'cto',
'meta.otherInfo': 'he stinks',
metadetaksamosone: 'this is a steroid',
})
})

it('filter params for nested property', () => {
expect(filterOutParams(params, 'meta')).to.deep.equal({
name: 'John',
surname: 'Doe',
age: 31,
metadetaksamosone: 'this is a steroid',
})
})

it('returns all objects when there is no match', () => {
expect(filterOutParams(params, 'nothingIsThere')).to.deep.eq(params)
})

it('filter by multiple properties when they are given', () => {
expect(filterOutParams(params, ['name', 'meta'])).to.deep.equal({
surname: 'Doe',
age: 31,
metadetaksamosone: 'this is a steroid',
})
})

it('does not throw an error when user passes undefined as a propertyPath', () => {
expect(() => {
filterOutParams(params, ['name', undefined as unknown as string])
}).not.to.throw()
})
})
28 changes: 18 additions & 10 deletions src/utils/flat/filter-out-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import { FlattenParams } from './flat.types'
* @load ./filter-out-params.doc.md
* @memberof flat
* @param {FlattenParams} params
* @param {string} property
* @param {string | Array<string>} properties
* @returns {FlattenParams}
*/
const filterOutParams = (params: FlattenParams, property: string): FlattenParams => {
const regex = propertyKeyRegex(property)
const filterOutParams = (
params: FlattenParams,
properties: string | Array<string>,
): FlattenParams => {
const propertyArray = Array.isArray(properties) ? properties : [properties]

// filter all keys which starts with property
return Object.keys(params)
.filter(key => !key.match(regex))
.reduce((memo, key) => ({
...memo,
[key]: (params[key] as string),
}), {} as FlattenParams)
return propertyArray
.filter(propertyPath => !!propertyPath)
.reduce((globalFiltered, propertyPath) => {
const regex = propertyKeyRegex(propertyPath)

return Object.keys(globalFiltered)
.filter(key => !key.match(regex))
.reduce((memo, key) => ({
...memo,
[key]: (params[key] as string),
}), {} as FlattenParams)
}, params)
}

export { filterOutParams }
6 changes: 6 additions & 0 deletions src/utils/flat/select-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ describe('selectParams', () => {
'meta.otherInfo': 'he stinks',
})
})

it('does not throw an error when user passes undefined as a propertyPath', () => {
expect(() => {
selectParams(params, ['name', undefined as unknown as string])
}).not.to.throw()
})
})
28 changes: 15 additions & 13 deletions src/utils/flat/select-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ const selectParams = (
options?: GetOptions,
): FlattenParams => {
const propertyArray = Array.isArray(properties) ? properties : [properties]
const selected = propertyArray.reduce((globalMemo, propertyPath) => {
const regex = propertyKeyRegex(propertyPath, options)
const filtered = Object.keys(params)
const selected = propertyArray
.filter(propertyPath => !!propertyPath)
.reduce((globalMemo, propertyPath) => {
const regex = propertyKeyRegex(propertyPath, options)
const filtered = Object.keys(params)
// filter all keys which starts with property path
.filter(key => key.match(regex))
.reduce((memo, key) => ({
...memo,
[key]: (params[key] as string),
}), {} as FlattenParams)
return {
...globalMemo,
...filtered,
}
}, {} as FlattenParams)
.filter(key => key.match(regex))
.reduce((memo, key) => ({
...memo,
[key]: (params[key] as string),
}), {} as FlattenParams)
return {
...globalMemo,
...filtered,
}
}, {} as FlattenParams)
return selected
}

Expand Down

0 comments on commit 478af85

Please sign in to comment.