Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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.2
  • npm
  • Socket score

Version published
Maintainers
1
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
			}
		},
		eventName: 'BillingDetails'
	},{
		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.on(myfork.getOutputEventName('Details'), function(result) {
	// save to database
	console.log('Details (from event): %s', JSON.stringify(result));
});

myfork.on(myfork.getErrorEventName('Details'), function(result) {
	// push error to log or enqueue
});

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

myfork.on(myfork.getErrorEventName('Summary'), function(result) {
	// push error to log or enqueue
});

Use the myfork:

var result = 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('Result: %s', JSON.stringify(result, null, 2));

FAQs

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc