-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from openscript/develop
Implement transformer feature
- Loading branch information
Showing
12 changed files
with
542 additions
and
335 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
module.exports = { | ||
parser: "@typescript-eslint/parser", | ||
parser: '@typescript-eslint/parser', | ||
extends: [ | ||
"plugin:react/recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier/@typescript-eslint", | ||
"plugin:prettier/recommended" | ||
'plugin:react/recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'prettier/@typescript-eslint', | ||
'plugin:prettier/recommended' | ||
], | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
ecmaVersion: 2018, | ||
sourceType: "module" | ||
sourceType: 'module' | ||
}, | ||
rules: { | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
"react/prop-types": "off" | ||
'@typescript-eslint/explicit-function-return-type': 'off', | ||
'@typescript-eslint/explicit-module-boundary-types': 'off', | ||
'react/prop-types': 'off' | ||
}, | ||
settings: { | ||
react: { | ||
version: "detect" | ||
version: 'detect' | ||
} | ||
} | ||
}; |
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,37 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest All", | ||
"program": "${workspaceFolder}/node_modules/.bin/jest", | ||
"args": ["--runInBand"], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"disableOptimisticBPs": true, | ||
"windows": { | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest", | ||
}, | ||
"runtimeExecutable": "~/.asdf/shims/node" | ||
}, | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest Current File", | ||
"program": "${workspaceFolder}/node_modules/.bin/jest", | ||
"args": [ | ||
"${fileBasenameNoExtension}", | ||
"--config", | ||
"jest.config.js" | ||
], | ||
"console": "integratedTerminal", | ||
"internalConsoleOptions": "neverOpen", | ||
"disableOptimisticBPs": true, | ||
"windows": { | ||
"program": "${workspaceFolder}/node_modules/jest/bin/jest", | ||
}, | ||
"runtimeExecutable": "~/.asdf/shims/node" | ||
} | ||
] | ||
} |
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
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,66 @@ | ||
import { ColumnsType } from '../models/column'; | ||
import { State } from '../models/state'; | ||
import { createTransformerMiddleware } from './transformerMiddleware'; | ||
|
||
describe('validatorMiddleware', () => { | ||
type TestType = { forename: string; surname: string; email: string }; | ||
const defaultColumns: ColumnsType<TestType> = [ | ||
{ key: 'forename', label: 'Forename' }, | ||
{ key: 'surname', label: 'Surname' }, | ||
{ key: 'email', label: 'Email' } | ||
]; | ||
const middleware = createTransformerMiddleware<TestType>(); | ||
const parsed: TestType[] = [ | ||
{ forename: 'Hans', surname: 'Muster', email: '[email protected]' }, | ||
{ forename: 'Heidi', surname: ' Muster', email: '[email protected]' }, | ||
{ forename: 'Joe', surname: 'Doe', email: ' [email protected] ' } | ||
]; | ||
const trimTransformer = (value: string) => value.trim(); | ||
const markTransformer = (value: string) => `${value}!`; | ||
|
||
it('should not dispatch if there are no transformers', () => { | ||
const state: State<TestType> = { columns: defaultColumns }; | ||
const dispatchMock = jest.fn(); | ||
|
||
middleware(state, dispatchMock, { type: 'setParsed', parsed }); | ||
expect(dispatchMock).toBeCalledTimes(0); | ||
}); | ||
|
||
it('should run a transformer on all values', () => { | ||
const state: State<TestType> = { columns: defaultColumns, transformers: [trimTransformer] }; | ||
const dispatchMock = jest.fn(); | ||
|
||
middleware(state, dispatchMock, { type: 'setParsed', parsed }); | ||
|
||
expect(dispatchMock).toBeCalledWith({ | ||
type: 'setParsed', | ||
parsed: [ | ||
{ forename: 'Hans', surname: 'Muster', email: '[email protected]' }, | ||
{ forename: 'Heidi', surname: 'Muster', email: '[email protected]' }, | ||
{ forename: 'Joe', surname: 'Doe', email: '[email protected]' } | ||
] | ||
}); | ||
}); | ||
|
||
it('should run transformers on values of a certain column', () => { | ||
const state: State<TestType> = { | ||
columns: [ | ||
{ key: 'forename', label: 'Forename' }, | ||
{ key: 'surname', label: 'Surname', transformers: [trimTransformer, markTransformer] }, | ||
{ key: 'email', label: 'Email' } | ||
] | ||
}; | ||
const dispatchMock = jest.fn(); | ||
|
||
middleware(state, dispatchMock, { type: 'setParsed', parsed }); | ||
|
||
expect(dispatchMock).toBeCalledWith({ | ||
type: 'setParsed', | ||
parsed: [ | ||
{ forename: 'Hans', surname: 'Muster!', email: '[email protected]' }, | ||
{ forename: 'Heidi', surname: 'Muster!', email: '[email protected]' }, | ||
{ forename: 'Joe', surname: 'Doe!', email: ' [email protected] ' } | ||
] | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
import { State } from '../models/state'; | ||
import { Dispatch } from 'react'; | ||
import { Actions } from '../models/actions'; | ||
import { ColumnsType } from '../models/column'; | ||
import { Transformer } from '../models/transformer'; | ||
|
||
const executeGlobalTransformers = <T>(values: T[], transformer: Transformer, columns: ColumnsType<T>) => { | ||
return values.map<T>((r) => { | ||
const transformed = { ...r }; | ||
columns.forEach((c) => { | ||
transformed[c.key] = (transformer(new String(r[c.key]).toString()) as unknown) as T[keyof T]; | ||
}); | ||
return transformed; | ||
}); | ||
}; | ||
|
||
const executeColumnTransformers = <T>(values: T[], columns: ColumnsType<T>) => { | ||
return values.map<T>((r) => { | ||
const transformed = { ...r }; | ||
columns.forEach((c) => { | ||
if (c.transformers) { | ||
transformed[c.key] = (c.transformers.reduce( | ||
(acc, t) => t(acc), | ||
new String(r[c.key]).toString() | ||
) as unknown) as T[keyof T]; | ||
} | ||
}); | ||
return transformed; | ||
}); | ||
}; | ||
|
||
export const createTransformerMiddleware = <T>() => { | ||
return (state: State<T>, next: Dispatch<Actions<T>>, action: Actions<T>) => { | ||
if (action.type === 'setParsed') { | ||
let parsed = action.parsed; | ||
if (state.transformers) { | ||
parsed = state.transformers.reduce<T[]>((acc, t) => executeGlobalTransformers(acc, t, state.columns), parsed); | ||
} | ||
|
||
const hasColumnTransformers = state.columns.find((c) => c.transformers) ? true : false; | ||
if (hasColumnTransformers) { | ||
parsed = executeColumnTransformers(parsed, state.columns); | ||
} | ||
|
||
if (state.transformers || hasColumnTransformers) { | ||
next({ type: 'setParsed', parsed }); | ||
} | ||
} | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { Rule } from './rule'; | ||
import { Transformer } from './transformer'; | ||
|
||
export type ColumnsType<T> = { key: keyof T; label: string; rules?: Rule[] }[]; | ||
export type ColumnsType<T> = { key: keyof T; label: string; rules?: Rule[]; transformers?: Transformer[] }[]; |
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 @@ | ||
export type Transformer = (value: string) => string; |
Oops, something went wrong.