# jsonbeat

> A step in JSON transforming flow

Latest version **0.2.0** (published 2017-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonbeat
pnpm add jsonbeat
yarn add jsonbeat
bun add jsonbeat
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-02-28 |
| First published | 2017-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | devebot |
| Maintainers | devebot |

## Links

- npm: https://www.npmjs.com/package/jsonbeat
- npm.io page: https://npm.io/package/jsonbeat

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^2.6.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.4.7
- [jsonvalidator](https://npm.io/package/jsonvalidator.md) ^0.1.1

## Recent versions

- 0.2.0 (latest) — 2017-02-28
- 0.1.4 — 2017-02-20
- 0.1.3 — 2017-02-18
- 0.1.2 — 2017-02-16
- 0.1.1 — 2017-02-16
- 0.1.0 — 2017-02-15

## README

# jsonbeat

> A step of JSON transforming flow

## Install

```shell
npm install --save jsonbeat
```

### Example

#### Define the `transformer`

```javascript
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`

```javascript
// 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));
});
```

---
_Source: https://npm.io/package/jsonbeat · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
