JSONitron
JSON transformer for use in data hygene and proxy
Usage
let jsonitron = ; let userTransformer = ;userTransformer;userTransformer;userTransformer;userTransformer;userTransformer; let result = userTransformer; console;// => {// full_name: 'JOHN HOF',// friends: [{// phone: '123-234-1234',// name: {// first: 'JOE',// last: 'SMITH'// }// }, {// phone: '234-234-2345',// name: {// first: 'JENNY',// last: 'DOE'// }// }]// }
Key
Transform
Class for creating a reusable transformation
Transform Constructor
- Returns a new Transform object
- Accepts:
Object
[optional]config.name
(String
) - Name of the transformconfig.path
(String
) - Path to match and perform the actionconfig.params
: (Mixed
) - Params to be passed to the action in addition tokey
andvalue
config.action
(Function
) - Action to be performed
// EXAMPLE ACTION:// { user_details.name: 'john hof' }// becomes// { user_details.full_name: 'JOHN HOF' }let fooTransform = name: 'foo' path: 'user_details.name' params: 'foo' { this; // OR .setValue this; };
.setPath
- Set the path on which to perform the action
- Accepts:
String
- Expects: JSON syntax:
prop.subProps[0].foo
- Allows:
*
as a wildcard:prop.subProps.*.foo
- Expects: JSON syntax:
fooTransform;// Applies to all elements in subProps:// {// prop: {// subProps: [// { foo: 'bar' }// { foo: 'baz' }// ]// }// }//// AND// {// prop: {// subProps: {// biz: { foo: 'bar' }// faz: { foo: 'baz' }// }// }// }
.setName
- Set the name of the transform
- Accepts:
String
fooTransform;
.setAction
- Set the action to be performed
- Accepts:
Function
- Should accept:
(value, key, params)
[READ ONLY] this
- refers to:this.setKey('String')
- update the key name of the propertythis.setValue
- update the value of the properythis.setVal
- seethis.setValue
- Should accept:
fooTransform;
.exec transform
- Execute the transform on an object
- Accepts:
Object
- Returns: (
Object
) result of the transform
let result = fooTransform;
Transform Factory
Utility for generating and accessing transforms
.build
- Build a new transform object
- Accepts:
path
(String
)[required] - path to matchaction
(Function
)[required] - action to perform (see Transform object documentation)params
(Mixed
)[optional] - additional params to pass to the actionname
(String
)[optional] - name of the transform
- Returns: (
Object
) result of the transform
jsonitronFactory;
.register
- Register a predefined transform
- Accepts:
name
(String
) - unique name of the transformaction
(Function
) - action to perform (see Transform object documentation)
jsonitronFactory; jsonitronFactory // returns new TransformjsonitronFactory // returns new TransformjsonitronFactory // returns new TransformjsonitronFactory // returns new Transform
Predefined
- Case
- Value
.upperCase(path)
- EXAMPLE OUTPUT.camelCas(path)e
- exmapleOutput.constantCase(path)
- EXAMPLE_OUTPUT.dotCase(path)
- example.output.headerCase(path)
- Example-Output.lowerCase(path)
- example output.lowerFirstCase(path)
- eXAMPLE OUTPUT.noCase(path)
- example output.paramCase(path)
- example-output.pascalCase(path)
- ExampleOutput.pathCase(path)
- example/output.sentenceCase(path)
- Example output.snakeCase(path)
- exmaple_output.swapCase(path)
- eXAMPLE oUTPUT.titleCase(path)
- Example Output.upperCase(path)
- EXAMPLE OUTPUT.upperFirstCase(path)
- Example output- Key
.upperCaseKey(path)
- EXAMPLE OUTPUT.camelCasKey(path)
- exmapleOutput.constantCaseKey(path)
- EXAMPLE_OUTPUT.dotCaseKey(path)
- example.output.headerCaseKey(path)
- Example-Output.lowerCaseKey(path)
- example output.lowerFirstCaseKey(path)
- eXAMPLE OUTPUT.noCaseKey(path)
- example output.paramCaseKey(path)
- example-output.pascalCaseKey(path)
- ExampleOutput.pathCaseKey(path)
- example/output.sentenceCaseKey(path)
- Example output.snakeCaseKey(path)
- exmaple_output.swapCaseKey(path)
- eXAMPLE oUTPUT.titleCaseKey(path)
- Example Output.upperCaseKey(path)
- EXAMPLE OUTPUT.upperFirstCaseKey(path)
- Example output
- Misc
.renameKey(path, newName)
- convert last property in the path to use the new name
{ test: 'foo '}
=>renameKey('test', 'bar')
=>{ bar: 'foo' }
Transformer
A thin wrapper to execute a set of transforms
Transformer constructor
- Returns a new Transform object
let transformer = ;
.use
- Add a transform to the internal set of transforms
- Accepts:
Transform
(Function
)[required] - transform to be executed
transformer;
.useAll
- Add a set of transforms to the internal set of transforms
- Accepts:
Transform
(Function
)[required] - transform to be executed
transformer;
.exec Transformer
- Execute the set of transforms on an object
- Accepts:
Object
- Returns: (
Object
) result of the transforms
let result = transformer;