🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

jsonfork

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonfork

JSON folk-transforming tool

0.1.1
npm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

jsonfork

JSON folk-transforming tool

Install

npm install --save jsonfork

Example

Define the transformer:

var jsonfork = require('jsonfork');

var transformer = new jsonfork({
	source: {
		name: 'Billing',
		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, bill_opts) {
			return {
				title: data.title + ' [' + data.customerId + ']',
				customerId: data.customerId,
				items: data.items
			}
		},
		eventName: 'BillingDetails'
	},{
		name: 'Summary',
		transform: function(data, bill_opts) {
			var summary = data.items.reduce(function(sum, item) {
				sum.count += item.amount;
				sum.total += item.amount * item.price;
				return sum;
			}, { count: 0, total: 0 });
			if (bill_opts.tax > 0) {
				summary.total *= (1 + bill_opts.tax);
			}
			return {
				title: data.title + ' [' + data.customerId + ']',
				customerId: data.customerId,
				count: summary.count,
				total: summary.total
			}
		},
		schema: {}
	}]
});

transformer.on('BillingDetails', function(result) {
	// save to database
	console.log('Details (from event): %s', JSON.stringify(result));
});

transformer.on('Summary', function(result) {
	// push to log or send back to webpage
	console.log('Summary (from event): %s', JSON.stringify(result));
});

Use transformer to transform json data:


var bill_opts = { tax: 0.1 };

var result = transformer.transform({
	title: 'JsonFork Billing',
	customerId: 'hello.com',
	items: [
		{ label: 'Item#1', price: 15, amount: 2 },
		{ label: 'Item#2', price: 17, amount: 3 }
	]
}, bill_opts);

console.log('Result: %s', JSON.stringify(result, null, 2));

FAQs

Package last updated on 15 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts