Megatron
Install
yarn add json-megatron
Use
const Megatron = require('json-megatron');
Examples
Return a simple value and parse from string to number
const template = {
newValue: {
type: 'number',
path: '$.value',
}
};
const input = {
value: '10',
};
const result = Megatron(template, input);
// result
{
"newValue": 10,
}
Example with text-transform
We use lib TextTranform to offer string transformations.
const template = {
newValue: {
type: 'string',
path: '$.value',
textTransform: 'uppercase',
},
};
const input = {
value: 'Testing',
};
const result = Megatron(template, input);
// result
{
"newValue": 'TESTING',
}
Example with string-mask
We use lib StringMask to offer mask for strings.
const template = {
newValue: {
type: 'string',
path: '$.value',
stringMask: '000.000.000-00',
},
};
const input = {
value: 12965815620,
};
const result = Megatron(template, input);
// result
{
"newValue": '129.658.156-20',
}
Properties
Field | Value |
---|---|
type | string | date | bool | number | string | array | base64 | json | array | object | enum | xml |
path | Path of value in object. More information |
prefix | Add a text before of value |
suffix | Add a text after of value |
properties | Required to type object or array |
custom |
Write a string and inline function:
custom: "function custom(value, template, transform, input, parentInput){ return parseInt(value); }"
|
show |
Run a custom function and check if show or hidden value. Write a string and inline function:
show: "function show(value, template, transform, input, parentInput){ return true; }"
|
showIndex | Return the field "itemIndex" with o index of array. |
beforeFilter |
Run a custom function if type is array and filter before transform props with "properties". Write a string and inline function:
beforeFilter: "function beforeFilter(item){ return item === 2; }"
|
afterFilter |
Run a custom function if type is array and filter after transform props with "properties". Write a string and inline function:
afterFilter: "function afterFilter(item){ return item === 2; }"
|
binaryTree |
Transform array to binary tree.
binaryTree: { fieldId: 'id', fieldParentId: 'parent_id', fieldChildren: 'children', removeParentId: true }
|
regex |
List of regex with the following format:
regex: [ { pattern: '[^0-9\\.]+', flags: 'g' }]
|
enum |
Required to type enum passing a list with from-to:
{ processing: 'pending' }
|
textTransform |
Use lib TextTransform. More information
textTranform: "uppercase"
|
stringMask |
Use lib StringMask. More information
stringMask: "000.000.000-00"
|
xmlOptions |
Use lib XmlJS. More information
type: "xml",
xmlOptions: { compact: true }
|
crypto |
Use lib crypto. More information
crypto: { algorithm: 'sha256', key: 'pass', digest: 'base64' }
|
validation |
Use lib validatorjs. More information
validation: { rules: 'required|email', customMessage: { required: 'Field :attribute is required' } }
|