# percento

> Simple node package to mixin json variables

Latest version **1.0.13** (published 2015-08-18) · ISC license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.13 |
| Published | 2015-08-18 |
| First published | 2015-08-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Dominic Charlesworth |
| Maintainers | domtronn, subsidel |
| Keywords | javascript, json, resolve, handlebars |

## Links

- npm: https://www.npmjs.com/package/percento
- Repository: https://github.com/fmtvp/percento.js
- Homepage: https://github.com/fmtvp/percento.js#readme
- Issues: https://github.com/fmtvp/percento.js/issues
- npm.io page: https://npm.io/package/percento

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^3.10.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.13 (latest) — 2015-08-18
- 1.0.12 — 2015-08-18
- 1.0.10 — 2015-08-18
- 1.0.7 — 2015-08-18
- 1.0.6 — 2015-08-18
- 1.0.5 — 2015-08-14
- 1.0.4 — 2015-08-14
- 1.0.3 — 2015-08-14
- 1.0.2 — 2015-08-14
- 1.0.1 — 2015-08-14
- 1.0.0 — 2015-08-14

## README

# percento.js #

**`Percento.js`** is designed to be [Handlebars](http://handlebarsjs.com/) for JSON.

It is a node module that allows you to template JSON objects with _mixins_ that can be resolved with a _context_ by this package. _Mixins_ are defined using delimiters, the default ones are `%...%`.

For example, given the following JSON object _(template)_
```json
{
  "Jason": [
    "%actor%",
    "%murderer%",
    "%singer%"
  ]
}
```
And a _context_ of
```js
{
  singer: "Mraz",
  actor: "Statham",
  murderer: "Voorhees"
}
```
**`Percento.js`** will produce the following
```json
{
  "Jason": [
    "Statham",
    "Voorhees",
    "Mraz"
  ]
}
```

## Installation & Usage ##

To install this package to your project, run
```
npm install --save percento
```
Then require it and use it with
```js
var percento = require('percento');

percento().resolve(json, ctx);
```
Percento _returns_ the modified string, so you will have to assign it to a variable.

## Options ##

#### Custom Delimiteres ####

**`Percento.js`** can be configured with a custom delimiter, or pair of opening and closing delimiters to define your own matchers.
```js
percento({delimiter: '$$'}).resolve(template, ctx);
percento({delimiter: {first: '{{', last: '}}'}}).resolve(template, ctx);
```

#### Chaining Resolutions ####

You can chain resolutions calls together in the following manner
```js
percento().chain().resolve(template, ctx1).resolve(ctx2).resolve(ctx3).value();
```
Chaining passes the resolved template through to each subsequent `resolve` call. You must call `.value()` at the end to return the current resolved template.

#### Nested Properties ####

_Templates_ can access nested properties of a _context_, for example this JSON object
```json
{
  "name": "%people[0].name%",
  "surname": "%people[1].surname%"
}
```
Can access the properties in this _context_
```js
{
    people:[
        { name: "Juliet", surname: "Capulet" },
        { name: "Romeo", surname: "Montague" }
    ]
}

```
To pick the values `"Juliet"` and `"Montague"`.
This uses [`lodash`'s deep get](https://lodash.com/docs#get) method, so the _accessor string_ in the _template_ should behave the same way.

#### Self Resolution ####

Calling `percento` with a single argument will try to resolve all mixins using _itself_ as the context, for example
```js
var template = {
    name: "william",
    surname: "shakespeare",
    fullname: "%name% %surname%"
}

var result = percento().resolve(template);

result.fullname; // william shakespeare
```

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