1.0.1 • Published 2 years ago

data-transformation-utils v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

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

ArgTypeDefaultDescription
transformationChainarrayundefinedtransformation function array

applyTransformations( transformativeValue, keepTypeof )

ArgTypeDefaultDescription
transformativeValueanyundefinedValue to be transformed by the function chain
keepTypeofbooleantrueFlag responsible for tracking the returned data type from converter functions

TwoWayTransformer

static MODS

propertyDescription
DIRECTDirect transformation of data
INVERSEInverse transformation

The sequence of transformation functions in a chain is important

constructor( transformationMods )

ArgTypeDefaultDescription
transformationModsobjectundefinedObject of transformation chains corresponding to a particular mod
Pseudocode:

transformationMods = {
        TwoWayTransformer.MODS.DIRECT: array[],
        TwoWayTransformer.MODS.INVERSE: array[]
}

directTransform( transformativeValue, keepTypeof)

ArgTypeDefaultDescription
transformativeValueanyundefinedValue to be transformed by the function chain
keepTypeofbooleantrueFlag responsible for tracking the returned data type from converter functions

inverseTransform( transformativeValue, keepTypeof)

ArgTypeDefaultDescription
transformativeValueanyundefinedValue to be transformed by the function chain
keepTypeofbooleantrueFlag responsible for tracking the returned data type from converter functions