Skip to content

Commit

Permalink
feat: add filterOutParams, selectParams to flat
Browse files Browse the repository at this point in the history
also:
* fix global.File when running in node
* improve cypress tests speed by not strogin videos on pass
  • Loading branch information
wojtek-krysiak committed Oct 11, 2020
1 parent 6bb8aa9 commit 4ba1552
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
3 changes: 2 additions & 1 deletion example-app/cypress.json
@@ -1,3 +1,4 @@
{
"baseUrl": "http://localhost:3000/admin"
"baseUrl": "http://localhost:3000/admin",
"videoUploadOnPasses": false
}
6 changes: 3 additions & 3 deletions src/backend/adapters/record/base-record.ts
@@ -1,6 +1,6 @@
import _ from 'lodash'
import { flat } from '../../../utils/flat'
import { ParamsType, ParamsTypeValue } from './params.type'
import { ParamsType } from './params.type'
import BaseResource from '../resource/base-resource'
import ValidationError, { RecordError, PropertyErrors } from '../../utils/errors/validation-error'
import { RecordJSON } from '../../../frontend/interfaces'
Expand Down Expand Up @@ -73,7 +73,7 @@ class BaseRecord {
* @return {any} unflatten data under given path
* @new in version 3.3
*/
get(propertyPath: string | undefined): ParamsTypeValue | object {
get(propertyPath: string | undefined): any {
return flat.get(this.params, propertyPath)
}

Expand All @@ -99,7 +99,7 @@ class BaseRecord {
* @return {object | undefined}
*/
namespaceParams(prefix: string): Record<string, any> | void {
return flat.filterParams(this.params, prefix)
return flat.selectParams(this.params, prefix)
}

/**
Expand Down
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { useHistory, useLocation } from 'react-router'
import { ActionResponse } from '../../../backend/actions/action.interface'
import { appendForceRefresh } from '../../components/actions/utils/append-force-refresh'
Expand Down
28 changes: 26 additions & 2 deletions src/utils/flat/filter-params.ts
Expand Up @@ -2,13 +2,15 @@ import { propertyKeyRegex } from './property-key-regex'
import { FlattenParams } from '.'

/**
*
* From all params it selects only those starting with property
*
* @memberof module:flat
* @param {FlattenParams} params
* @param {string} property
* @new In version 3.3
*/
const filterParams = (params: FlattenParams, property: string): FlattenParams => {
const selectParams = (params: FlattenParams, property: string): FlattenParams => {
const regex = propertyKeyRegex(property)

// filter all keys which starts with property
Expand All @@ -20,4 +22,26 @@ const filterParams = (params: FlattenParams, property: string): FlattenParams =>
}), {} as FlattenParams)
}

export { filterParams }
/**
*
* 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 { selectParams, filterOutParams }
8 changes: 5 additions & 3 deletions src/utils/flat/flat-module.ts
@@ -1,7 +1,7 @@
import { flatten, unflatten } from 'flat'

import { DELIMITER } from './constants'
import { filterParams } from './filter-params'
import { selectParams, filterOutParams } from './filter-params'
import { set } from './set'
import { get } from './get'
import { pathToParts } from './path-to-parts'
Expand All @@ -15,7 +15,8 @@ export type FlatModuleType = {
unflatten: typeof unflatten;
set: typeof set;
get: typeof get;
filterParams: typeof filterParams;
selectParams: typeof selectParams;
filterOutParams: typeof filterOutParams;
DELIMITER: typeof DELIMITER;
pathToParts: typeof pathToParts;
}
Expand Down Expand Up @@ -52,7 +53,8 @@ export const flat: FlatModuleType = {
unflatten,
set,
get,
filterParams,
selectParams,
filterOutParams,
DELIMITER,
pathToParts,
}
8 changes: 4 additions & 4 deletions src/utils/flat/get.ts
@@ -1,6 +1,6 @@
import { unflatten } from 'flat'
import { DELIMITER } from './constants'
import { filterParams } from './filter-params'
import { selectParams } from './filter-params'
import { FlattenParams } from '../flat'
import { propertyKeyRegex } from './property-key-regex'

Expand All @@ -24,11 +24,11 @@ const get = (params: FlattenParams = {}, propertyPath?: string): any => {
}

const regex = propertyKeyRegex(propertyPath)
const filteredParams = filterParams(params, propertyPath)
const selectedParams = selectParams(params, propertyPath)

const nestedProperties = Object.keys(filteredParams).reduce((memo, key) => ({
const nestedProperties = Object.keys(selectedParams).reduce((memo, key) => ({
...memo,
[key.replace(regex, `${TEMP_HOLDING_KEY}${DELIMITER}`)]: filteredParams[key],
[key.replace(regex, `${TEMP_HOLDING_KEY}${DELIMITER}`)]: selectedParams[key],
}), {} as FlattenParams)

if (Object.keys(nestedProperties).length) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/flat/set.spec.ts
Expand Up @@ -52,6 +52,10 @@ describe('module:flat.set', () => {
it('sets empty array', () => {
expect(set(params, newPropertyName, [])).to.deep.include({ [newPropertyName]: [] })
})

it('does nothing when setting undefined to some random key', () => {
expect(set(params, newPropertyName, undefined)).to.deep.equal(params)
})
})

context('passing array', () => {
Expand Down
11 changes: 8 additions & 3 deletions src/utils/flat/set.ts
Expand Up @@ -4,9 +4,14 @@ import { FlattenParams } from '../flat'
import { propertyKeyRegex } from './property-key-regex'
import { pathToParts } from './path-to-parts'

const isObject = (value: any): boolean => (
typeof value === 'object' && !(value instanceof File) && value !== null
)
const isObject = (value: any): boolean => {
// Node environment
if (typeof File === 'undefined') {
return typeof value === 'object' && value !== null
}
// Window environment
return typeof value === 'object' && !(value instanceof File) && value !== null
}

/**
*
Expand Down

0 comments on commit 4ba1552

Please sign in to comment.