Skip to content

Commit

Permalink
feat: pass multiple arguments to flat.selectParams
Browse files Browse the repository at this point in the history
* refactor previous filter-params file to 2 separate files
  • Loading branch information
wojtek-krysiak committed Oct 11, 2020
1 parent 2680cae commit 046292d
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .cspell.json
Expand Up @@ -8,7 +8,7 @@
"camelize", "datepicker", "camelcase", "fullwidth", "wysiwig", "Helvetica", "Neue",
"Arial", "nowrap", "textfield", "scrollable", "flexbox", "treal", "xxxl",
"adminbro", "Checkmark", "overridable", "Postgres", "Hana", "Wojtek", "Krysiak", "bigint",
"borderless"
"borderless", "metadetaksamosone"
],
"ignorePaths": [
"src/frontend/assets/**/*"
Expand Down
25 changes: 25 additions & 0 deletions src/utils/flat/filter-out-params.ts
@@ -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 }
47 changes: 0 additions & 47 deletions src/utils/flat/filter-params.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/utils/flat/flat-module.ts
@@ -1,7 +1,8 @@
import { flatten, unflatten } from 'flat'

import { DELIMITER } from './constants'
import { selectParams, filterOutParams } from './filter-params'
import { selectParams } from './select-params'
import { filterOutParams } from './filter-out-params'
import { set } from './set'
import { get } from './get'
import { pathToParts } from './path-to-parts'
Expand Down
2 changes: 1 addition & 1 deletion src/utils/flat/get.ts
@@ -1,6 +1,6 @@
import { unflatten } from 'flat'
import { DELIMITER } from './constants'
import { selectParams } from './filter-params'
import { selectParams } from './select-params'
import { FlattenParams } from '../flat'
import { propertyKeyRegex } from './property-key-regex'

Expand Down
49 changes: 49 additions & 0 deletions src/utils/flat/select-params.spec.ts
@@ -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',
})
})
})
30 changes: 30 additions & 0 deletions src/utils/flat/select-params.ts
@@ -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 }

0 comments on commit 046292d

Please sign in to comment.