xform
Transform objects from one structure to another
Example
var xform = ; var getDeep = ; console; // { value: 'deep value' }
Installation
npm install xform --save
Usage
Create an object parsing function by feeding a template object to xform
:
var parser = ;
The template is a plain object that represents the object structure you're expecting to receive. Values are pulled from the source object through node-parsing methods on the template:
var template = example: xformvalue // Copies value as-is;
The resulting parser is a function that accepts a source object and returns a transformed object:
var template = example: xformvalue; const parser = ; const transformed = ;
Node Parsers
A node-parsing method is simply a function that receives four arguments: value
, result
, key
, and data
.
It's up to the node parser to set a value on the result
object:
{ resultkey = value;} var template = example: nodeParser;
The data
argument is a Map
that's shared by every parser.
Since it's shared, it's possible to clobber other parsers' data, so you should namespace your keys:
data;
Built-In Parsers
xform.value([default])
Simply returns a value as-is.
If the source value is undefined
then default
is returned.
xform.memo(key)
Saves a value for later retrieval.
xform.memoValue(key)
Returns value previously saved by memo
.
xform.exclude(template)
Continues parsing template
but does nothing with the result.
Really only useful if you're using something like memo
in template
.
xform.path(expression, [template])
Performs a JSONPath query and returns the first value.
Passing a second template will traverse the result.