You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
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 fork-transforming tool

0.1.3
latest
npmnpm
Version published
Weekly downloads
6
Maintainers
1
Weekly downloads
 
Created
Source

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));

FAQs

Package last updated on 03 Mar 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