json-transpose
v3.3.3
Published
JSON Transpose ====================================
Downloads
1,908
Readme
JSON Transpose
About
Transposes an object to another object using templated fields. Could not find a library that could safely reference fields from the source object that may not exist and compile the mapping functions for later use.
Build
The library is written using typescript, make sure to have to built the repository when testing as tests run off the final .js files
Usage
const { compile } = require('json-transpose');
const transform = compile({
someValue: '${it.some.nested.value}'
});
const result = transform({
some: {
nested: {
value: 'test'
}
}
});
console.log(result);
Output:
{
someValue: 'test'
}
Interpolation
{
...
[target field]: '${it.[path to source field]}',
...
}
Effectively you can do whatever JavaScript string templates can do.
{
...
[target field 1]: 'This is a template ${it.[path to source field]}',
[target field 2]: 'Some arithmatic ${it.[path to source field] + it.[path to source field]}',
...
}
Arrays
If the source will be an array and you don't want to transpose any of the fields just reference the array:
{
...
target array: '${it.[path to source array]}'
...
}
To transpose the items within the array use an array for the definition which will take two items:
- The source
- The mappings
{
...,
[target array]: {
$array: {
source: 'it.[path to source array]',
item: {
someValue: '${it.some.nested.value}'
}
}
}
...
}
Coercion
If the expression is the only thing in the template, the type it evaluates to will be the value assigned to the field.
Input
{
...,
string: 'Value: ${it.value}',
integer: '${it.value + 10}',
boolean: '${it.value > 10 && it.value < 20}'
...
}
Output:
{
...,
string: 'Value: 5',
integer: 15,
boolean: false
...
}
Special Objects
Certain objects have been supplied as helpers for accessing data that would otherwise be inaccessible.
_root
The root object that was passed in. This is useful when needing to reference the root document within arrays.
{
...,
[target array]: {
$array: {
source: 'it.[path to source array]',
item: {
rootValue: '${_root.value}',
someValue: '${it.some.nested.value}'
}
}
}
...
}
Custom Objects
Custom objects can be projected into expressions. json-transpose will attempt to filter out any non-field objects / operations. However, specific objects can be allowed for use.
const transform = compile({
trucatedNumber: '${Math.trunc(it.number)}',
formattedNumber: '${formatNumber(it.number)}'
}, {
customObjects: {
Math,
formatNumber(number) {
return `$${number.toFixed(2)}`;
}
}
});
transform({
number: 15.2345
});
Output:
{
truncatedNumber: 15,
formattedNumber: '$15.23'
}
String only templates
It is okay to just have a string value for a template rather than a whole object.
const transform = compile('${it.some.nested.value}');
const result = transform({
some: {
nested: {
value: 'test'
}
}
});
Output:
'test'
Missing Data Fields
If the value does not exist given the path for the field. The value will be set to null
const transform = compile({
someValue: '${it.some.nested.value}'
});
const result = transform({
some: { }
});
Output:
{
someValue: null
}
Tests
npm run test