-
Notifications
You must be signed in to change notification settings - Fork 659
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pass multiple arguments to flat.selectParams
* refactor previous filter-params file to 2 separate files
- Loading branch information
1 parent
2680cae
commit 046292d
Showing
7 changed files
with
108 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { propertyKeyRegex } from './property-key-regex' | ||
import { FlattenParams } from './flat.types' | ||
|
||
/** | ||
* | ||
* From all params it removes keys starting with property | ||
* | ||
* @memberof module:flat | ||
* @param {FlattenParams} params | ||
* @param {string} property | ||
* @new In version 3.3 | ||
*/ | ||
const filterOutParams = (params: FlattenParams, property: string): FlattenParams => { | ||
const regex = propertyKeyRegex(property) | ||
|
||
// 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) | ||
} | ||
|
||
export { filterOutParams } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { expect } from 'chai' | ||
import { selectParams } from './select-params' | ||
|
||
describe('selectParams', () => { | ||
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('selects params for given property', () => { | ||
expect(selectParams(params, 'age')).to.deep.equal({ | ||
age: 31, | ||
}) | ||
}) | ||
|
||
it('select params for nested property', () => { | ||
expect(selectParams(params, 'meta')).to.deep.equal({ | ||
'meta.description': 'very ugly', | ||
'meta.title': 'cto', | ||
'meta.otherInfo': 'he stinks', | ||
}) | ||
}) | ||
|
||
it('returns empty object when there is no match', () => { | ||
expect(selectParams(params, 'nothingIsThere')).to.deep.eq({}) | ||
}) | ||
|
||
it('returns multiple properties when they are given', () => { | ||
expect(selectParams(params, 'name', 'surname')).to.deep.equal({ | ||
name: 'John', | ||
surname: 'Doe', | ||
}) | ||
}) | ||
|
||
it('does not one property when is empty for multi-properties', () => { | ||
expect(selectParams(params, 'name', 'surname', 'meta', 'empty')).to.deep.equal({ | ||
name: 'John', | ||
surname: 'Doe', | ||
'meta.description': 'very ugly', | ||
'meta.title': 'cto', | ||
'meta.otherInfo': 'he stinks', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { propertyKeyRegex } from './property-key-regex' | ||
import { FlattenParams } from './flat.types' | ||
|
||
/** | ||
* | ||
* From all params it selects only those starting with property | ||
* | ||
* @memberof module:flat | ||
* @param {FlattenParams} params | ||
* @param {...string} properties | ||
* @new In version 3.3 | ||
*/ | ||
const selectParams = (params: FlattenParams, ...properties: Array<string>): FlattenParams => ( | ||
properties.reduce((globalMemo, property) => { | ||
const regex = propertyKeyRegex(property) | ||
const filtered = Object.keys(params) | ||
// filter all keys which starts with property | ||
.filter(key => key.match(regex)) | ||
.reduce((memo, key) => ({ | ||
...memo, | ||
[key]: (params[key] as string), | ||
}), {} as FlattenParams) | ||
return { | ||
...globalMemo, | ||
...filtered, | ||
} | ||
}, {} as FlattenParams) | ||
) | ||
|
||
export { selectParams } |