Json transformer for use in data hygene and proxy
npm install jsonitron
JSON transformer for use in data hygene and proxy
``javascript
let jsonitron = require('jsonitron');
let userTransformer = new jsonitron.Transformer();
userTransformer.use(jsonitron.Factory.renameKey('name', 'full_name'));
userTransformer.use(jsonitron.Factory.upperCase('full_name'));
userTransformer.use(jsonitron.Factory.upperCase('friends.*.name.first'));
userTransformer.use(jsonitron.Factory.upperCase('friends.*.name.last'));
userTransformer.use(jsonitron.Factory.new('friends.*.phone', function (val, key) {
let match = ((val || '').match(/(^\d{3,3})(\d{3,3})(\d{4,4})$/) || [])
match.shift();
this.setVal(match.join('-'));
}));
let result = userTransformer.exec({
name: 'john hof',
friends: [{
phone: '1232341234',
name: {
first: 'joe',
last: 'smith'
}
}, {
phone: '2342342345',
name: {
first: 'jenny',
last: 'doe'
}
}]
});
console.log(result);
// => {
// full_name: 'JOHN HOF',
// friends: [{
// phone: '123-234-1234',
// name: {
// first: 'JOE',
// last: 'SMITH'
// }
// }, {
// phone: '234-234-2345',
// name: {
// first: 'JENNY',
// last: 'DOE'
// }
// }]
// }
`
- Usage
- Transform
- Constructor
- .setPath
- .setName
- .setAction
- .exec
- Transform Factory
- .build
- .register
- Predefined
- Transformer
- Constructor
- .use
- .useAll
- .exec
Class for creating a reusable transformation
- 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 to key and valueconfig.action
- (Function) - Action to be performed
`javascript`
// EXAMPLE ACTION:
// { user_details.name: 'john hof' }
// becomes
// { user_details.full_name: 'JOHN HOF' }
let fooTransform = new jsonitron.Transfrom({
name: 'foo',
path: 'user_details.name',
params: 'foo',
action: function (value, key, params) {
this.setVal(value.toUpperCase()); // OR .setValue
this.setkey('full_name');
}
});
- Set the path on which to perform the action
- Accepts: Stringprop.subProps[0].foo
- Expects: JSON syntax:
- Allows: as a wildcard: prop.subProps..foo
`javascriptprop.subProps.*.foo
fooTransform.setPath();`
// Applies to all elements in subProps:
// {
// prop: {
// subProps: [
// { foo: 'bar' }
// { foo: 'baz' }
// ]
// }
// }
//
// AND
// {
// prop: {
// subProps: {
// biz: { foo: 'bar' }
// faz: { foo: 'baz' }
// }
// }
// }
- Set the name of the transform
- Accepts: String
`javascript`
fooTransform.setName('fooRenamed');
- Set the action to be performed
- Accepts: Function(value, key, params)
- Should accept: [READ ONLY]this
- - refers to:this.setKey('String')
- - update the key name of the property this.setValue
- - update the value of the properythis.setVal
- - see this.setValue
`javascript`
fooTransform.setAction(function (value, key, params) {
this.setVal(value.toUpperCase()); // OR .setValue
this.setkey('full_name');
});
- Execute the transform on an object
- Accepts: ObjectObject
- Returns: () result of the transform
`javascript`
let result = fooTransform.exec({ object: { to: ['transform'] } });
Utility for generating and accessing transforms
- 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 transformObject
- Returns: () result of the transform
`javascript`
jsonitron.Factory.build('foo.bar', function (v, k, p) {
// See Transfrom object documentation
} , { some: 'param' }, 'fooTransform');
- Register a predefined transform
- Accepts:
- name (String) - unique name of the transformaction
- (Function) - action to perform (see Transform object documentation)
`javascript
jsonitron.Factory.register('fooAction', function (v, k, p) {
// See Transfrom object documentation
});
jsonitron.Factory.fooAction('foo.bar') // returns new Transform
jsonitron.Factory.createFooAction('foo.bar') // returns new Transform
jsonitron.Factory.buildFooAction('foo.bar') // returns new Transform
jsonitron.Factory.newFooAction('foo.bar') // returns new Transform
`
- 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.upperCaseKey(path)
- Key
- - 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.renameKey(path, newName)
- Misc
- { test: 'foo '}
- convert last property in the path to use the new name
- => renameKey('test', 'bar') => { bar: 'foo' }
A thin wrapper to execute a set of transforms
- Returns a new Transform object
`javascript`
let transformer = new jsonitron.Transfromer();
- Add a transform to the internal set of transforms
- Accepts:
- Transform (Function)[required] - transform to be executed
`javascript`
transformer.use(TransformFactory.upperCase('foo.bar'));
- Add a set of transforms to the internal set of transforms
- Accepts:
- Transform (Function)[required] - transform to be executed
`javascript`
transformer.useAll([
TransformFactory.upperCase('foo.bar'),
TransformFactory.lowerCase('foo.biz'),
]);
- Execute the set of transforms on an object
- Accepts: ObjectObject
- Returns: () result of the transforms
`javascript``
let result = transformer.exec({ object: { to: ['transform'] } });