JS Library representing utility allowing assigning a chain of transformations to be executed over input value.
npm i --save data-transformation-utils
import { Transformer, TwoWayTransformer } from 'data-transformation-utils'
const transformer = new Transformer(transformations)
default usage
const transformativeValue = {
massProp: 1000,
stringProp: '',
}
const transformationChain = [
( object ) => {
object.massProp = object.massProp / 1000
return object
},
( object ) => {
object.stringProp = object.stringProp === '' ? null : object.stringProp
return object
}
]
const transformer = new Transformer( transformationChain )
transformer.applyTransformations( transformativeValue )
{
massProp: 1,
stringProp: null
}
Ignore changing the data type in the transformation functions
const transformativeValue = null
const transformations = [
( value ) => value + '13', // always returns string
( value ) => {
return {
value
}
}
]
const transformer = new Transformer( transformations )
const transformedValue = transformer.applyTransformations( transformativeValue, false )
console.log( transformedValue )
{
value: "null13"
}
const transformativeValue = {
massProp: 1000,
stringProp: '',
}
const directTransformation = [
( object ) => {
object.massProp = object.massProp / 1000
return object
},
( object ) => {
object.stringProp = object.stringProp === '' ? null : object.stringProp
return object
}
]
const inverseTransformation = [
( object ) => {
object.massProp = object.massProp * 1000
return object
},
( object ) => {
object.stringProp = object.stringProp === null ? '' : object.stringProp
return object
}
]
const transformerMods = {}
transformerMods[ TwoWayTransformer.MODS.DIRECT ] = directTransformation
transformerMods[ TwoWayTransformer.MODS.INVERSE ] = inverseTransformation
const transformer = new TwoWayTransformer( transformerMods )
const directTransformedValue = transformer.directTranform( transformativeValue )
const inverseTransformedValue = transformer.inverseTransform( directTransformedValue )
console.log( inverseTransformedValue )
{
massProp: 1000,
stringProp: ""
}
Arg | Type | Default | Description |
---|---|---|---|
transformationChain | array | undefined | transformation function array |
Arg | Type | Default | Description |
---|---|---|---|
transformativeValue | any | undefined | Value to be transformed by the function chain |
keepTypeof | boolean | true | Flag responsible for tracking the returned data type from converter functions |
property | Description |
---|---|
DIRECT | Direct transformation of data |
INVERSE | Inverse transformation |
The sequence of transformation functions in a chain is important
Arg | Type | Default | Description |
---|---|---|---|
transformationMods | object | undefined | Object of transformation chains corresponding to a particular mod |
Pseudocode:
transformationMods = {
TwoWayTransformer.MODS.DIRECT: array[],
TwoWayTransformer.MODS.INVERSE: array[]
}
Arg | Type | Default | Description |
---|---|---|---|
transformativeValue | any | undefined | Value to be transformed by the function chain |
keepTypeof | boolean | true | Flag responsible for tracking the returned data type from converter functions |
Arg | Type | Default | Description |
---|---|---|---|
transformativeValue | any | undefined | Value to be transformed by the function chain |
keepTypeof | boolean | true | Flag responsible for tracking the returned data type from converter functions |