Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsonbeat

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonbeat

A step of JSON transforming flow

  • 0.1.1
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

jsonbeat

A step of JSON transforming flow

Install

npm install --save jsonbeat

Example

Define the transformer:

var jsonbeat = require('jsonbeat');

var billSchema = {
	name: 'Summary',
	inputSchema: {
		"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"]
				}
			}
		}
	},
	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
		}
	}
};

var billSchemaCallback = Object.assign({}, billSchema, {
	transform: function(data, callback) {
		var summary = data.items.reduce(function(sum, item) {
			sum.count += item.amount;
			sum.total += item.amount * item.price;
			return sum;
		}, { count: 0, total: 0 });
		var ret = {
			title: data.title + ' [' + data.customerId + ']',
			customerId: data.customerId,
			count: summary.count,
			total: summary.total
		}
		callback && callback(null, ret);
		return ret;
	}
});

var transformer1 = new jsonbeat(billSchema);

transformer1.on('Summary', function(result) {
	console.log('Summary (transformer1): %s', JSON.stringify(result));
});

var transformer2 = new jsonbeat(billSchemaCallback);

transformer2.on('Summary', function(result) {
	console.log('Summary (transformer2): %s', JSON.stringify(result));
});

Use the transformer:

// use pushSync if transform function is synchronous
var result = transformer1.pushSync({
	title: 'JsonFork Billing',
	customerId: 'hello.com',
	items: [
		{ label: 'Item#1', price: 15, amount: 2 },
		{ label: 'Item#2', price: 17, amount: 3 }
	]
});
console.log('pushSync() output: %s', JSON.stringify(result, null, 2));

// use push() + promise if transform function is asynchronous
transformer2.push({
	title: 'JsonFork Billing',
	customerId: 'hello.com',
	items: [
		{ label: 'Item#1', price: 15, amount: 2 },
		{ label: 'Item#2', price: 17, amount: 3 }
	]
}).then(function(summary) {
	console.log('push() output: %s', JSON.stringify(summary, null, 2));
});

FAQs

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