npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

jsonbeat

v0.2.0

Published

A step in JSON transforming flow

Downloads

16

Readme

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
		}
	},
	outputSchema: {
		"type": "object",
		"properties": {
			"title": { "type": "string" },
			"customerId": { "type": "string" },
			"count": { "type": "number", "minimum": 0 },
			"total": { "type": "number" }
		}
	}
};

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

var billSchemaPromise = Object.assign({}, billSchema, {
	transform: function(data) {
		return new Promise(function(resolve, reject) {
			var summary = data.items.reduce(function(sum, item) {
				sum.count += item.amount;
				sum.total += item.amount * item.price;
				return sum;
			}, { count: 0, total: 0 });
			setTimeout(function() {
				resolve({
					title: data.title + ' [' + data.customerId + ']',
					customerId: data.customerId,
					count: summary.count,
					total: summary.total
				});
			}, 100);
		})
	}
});

var transformer1 = new jsonbeat(billSchema);
transformer1.outlet.on('result', function(result) {
	console.log('Summary (transformer1): %s', JSON.stringify(result));
});

var transformer2 = new jsonbeat(billSchemaCallback);
transformer2.outlet.on('result', function(result) {
	console.log('Summary (transformer2): %s', JSON.stringify(result));
});

var transformer3 = new jsonbeat(billSchemaPromise);
transformer3.outlet.on('result', function(result) {
	console.log('Summary (transformer3): %s', JSON.stringify(result));
});

Use the transformer

// use pushSync() if transform function is synchronous
var result = transformer1.pushSync({
	title: 'JsonBeat 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: 'JsonBeat 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));
}).catch(function(err) {
	console.log('push() error: %s', JSON.stringify(err, null, 2));
});

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