data-transformation-utils

1.0.1 • Public • Published

data-transformation-utils

JS Library representing utility allowing assigning a chain of transformations to be executed over input value.


Installation

npm i --save data-transformation-utils


Getting Started

import { Transformer, TwoWayTransformer } from 'data-transformation-utils'

const transformer = new Transformer(transformations)



Examples of usage

Transformer


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 )

Result:


{
  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 )

Result:


{
  value: "null13"
}

TwoWayTransformer


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 )

Result:


{
  massProp: 1000,
  stringProp: ""
}



Class Properties


Transformation


constructor( transformationChain )


Arg Type Default Description
transformationChain array undefined transformation function array


applyTransformations( transformativeValue, keepTypeof )


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



TwoWayTransformer


static MODS

property Description
DIRECT Direct transformation of data
INVERSE Inverse transformation

The sequence of transformation functions in a chain is important



constructor( transformationMods )


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[]
}


directTransform( transformativeValue, keepTypeof)


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


inverseTransform( transformativeValue, keepTypeof)


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

Readme

Keywords

Package Sidebar

Install

npm i data-transformation-utils

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

85.2 kB

Total Files

22

Last publish

Collaborators

  • cbrgpl