0.1.3 • Published 7 years ago
jsonfork v0.1.3
jsonfork
JSON fork-transforming tool
Install
npm install --save jsonfork
Example
Define the myfork
:
var jsonfork = require('jsonfork');
var myfork = new jsonfork({
name: 'Billing',
source: {
schema: {
"type": "object",
"properties": {
"title": { "type": "string" },
"fromTime": { "type": "string" },
"toTime": { "type": "string" },
"customerId": { "type": "string" },
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"label": { "type": "string" },
"price": { "type": "number" },
"amount": { "type": "number" }
},
"required": ["label", "price", "amount"]
}
}
}
}
},
targets: [{
name: 'Details',
transform: function(data) {
return {
title: data.title + ' [' + data.customerId + ']',
customerId: data.customerId,
items: data.items
}
}
},{
name: 'Summary',
transform: function(data) {
var summary = data.items.reduce(function(sum, item) {
sum.count += item.amount;
sum.total += item.amount * item.price;
return sum;
}, { count: 0, total: 0 });
return {
title: data.title + ' [' + data.customerId + ']',
customerId: data.customerId,
count: summary.count,
total: summary.total
}
},
schema: {}
}]
});
myfork.outlet('Details').on('result', function(result) {
// save to database
console.log('Details (from event): %s', JSON.stringify(result));
});
myfork.outlet('Details').on('defect', function(result) {
// push error to log or enqueue
});
myfork.outlet('Summary').on('result', function(result) {
// push to log or send back to webpage
console.log('Summary (from event): %s', JSON.stringify(result));
});
myfork.outlet('Summary').on('defect', function(result) {
// push error to log or enqueue
});
Use the myfork
:
var output = myfork.pushSync({
title: 'JsonFork Billing',
customerId: 'hello.com',
items: [
{ label: 'Item#1', price: 15, amount: 2 },
{ label: 'Item#2', price: 17, amount: 3 }
]
});
console.log('Output: %s', JSON.stringify(output, null, 2));