npm.io
2.0.0 • Published 5 years ago

nn-transformer

Licence
ISC
Version
2.0.0
Deps
2
Size
18 kB
Vulns
0
Weekly
0

nn-transformer

Recursively transforms every marked object in an array or object.

Object Transformations

When passing a value to the transform function any marked objects will be transformed using a transformer function. A marked object is any object with a "*" value. If value of "*" is the name of a transformer then the object will be passed to the transformer function. If the value of "*" is not a named transformer then the default transformer will be used.

Usage

transform

  • value: any - The value to transform.
  • configuration: object - The transform configuration.
    • filter (optional): FilterFn | string[] | string - Constraints.
    • maxLevel (optional): number - The maximum depth. Default is 1024.
    • transformer: TransformerFn - Default transformer function.
    • transformers: object - Object containing named TransformerFn.
  • options: object (optional) - Arbitrary object to pass to the transformer function.

FilterFn

  • Params
    • value: any
  • Returns
    • boolean - true to accept the value or false if value is unacceptable.

TransformerFn

  • Params
    • args: object
      • level: number - Indicates how deep an object will be traversed during the transformation.
      • property: string - The property being transformed.
      • value: any - The value being transformed.
      • options: object - Object of options values passed to the transformer function.
      • transform: ( value: any ) => any - Use to transform a nested value.
  • Returns
    • any - The transformed value.

Build-in Transformers

  • *map
    • This transformer will transform nested values.
  • *raw
    • This transformer will return the object without any transformations, including nested objects. This transformer is also useful for filtering out values.

Examples

import { transform } from 'nn-transformer';

let configuration =
{
	transformer ( { level, property, value, options, transform } )
	{
		return options[ value.prop ];
	},
	transformers:
	{
		"A": ( { value, options } ) => ( options[ value.prop ] )
	}
};


await transform( 123, configuration );
// No effect, returns 123


// Array
await transform( [ 123, 456, { "*": "*raw", value: 789 } ], configuration );
// Returns [ 123, 456, { value: 789 } ]


// Object (using transformer function)
await transform( { "A": "ABC", "B": { "*": "A", "prop": "DEF" } }, configuration, { "DEF": 123 } );
// Returns { "A": "ABC", "B": 123 }


// Object (uses default transformer because there is no transformer named '*')
await transform( { "A": "ABC", "B": { "*": "*", "prop": "DEF" } }, configuration, { "DEF": 123 } );
// Returns { "A": "ABC", "B": 123 }


// Object (filter values)
let data =
{
	"A": "ABC",
	"B": { "*": { "transformer": "*raw", "filter": "B", "value": "DEF" } }, // Note: The value will replace this object.
	"C": { "*": { "transformer": "*raw", "filter": "C", "value": "GHI" } },
	"D": { "*": { "transformer": "*raw", "filter": "D", "*": "JKL" } } // Note: The '*' value will replace the '*' object.
};

let configuration =
{
	filter: [ 'B', 'D' ]
};

await transform( data, configuration );
// Returns { "A": "ABC", "B": "DEF", "D": { "*": "JKL" } }