# json-sharp

> Process operations on pure JSON objects

Latest version **0.1.4** (published 2017-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-sharp
pnpm add json-sharp
yarn add json-sharp
bun add json-sharp
```

## 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.1.4 |
| Published | 2017-06-28 |
| First published | 2015-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.8.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Globo.com |
| Maintainers | globocom, metal, ricobl |

## Links

- npm: https://www.npmjs.com/package/json-sharp
- Repository: https://github.com/globocom/json-sharp
- Homepage: https://github.com/globocom/json-sharp#readme
- Issues: https://github.com/globocom/json-sharp/issues
- npm.io page: https://npm.io/package/json-sharp

## Dependencies (2)

- [JSONPath](https://npm.io/package/JSONPath.md) ^0.10.0
- [deepmerge](https://npm.io/package/deepmerge.md) ^0.2.10

## Recent versions

- 0.1.4 (latest) — 2017-06-28
- 0.1.3 — 2015-10-09
- 0.1.2 — 2015-10-09
- 0.1.1 — 2015-10-09
- 0.1.0 — 2015-10-08

## README

[![Build Status](https://travis-ci.org/globocom/json-sharp.png?branch=master)](https://travis-ci.org/globocom/json-sharp)

json-sharp
==========

Process operations on pure JSON objects.


## How it works

`JSONSharp.process` clones an object and processes operations returning a
modified object.

Operations are simple objects with a single property representing its name.

The operation name should be preceded by the `#` (sharp) symbol to avoid
conflicts with real data.

The property value is processed by the operation logic using a given context.


## Motivation

Some systems need slightly different configuration between environments and
contexts. This technique allows to have a good degree of reuse with a simple
format.


## Example

Given the following object and context:

```js
var config = {
    '#merge': [
        {debug: true, url: 'http://localhost'},
        {
            '#switch': {
                '#property': 'env',
                '#case': {
                    dev: {
                        url: 'http://dev.com/'
                    },
                    prod: {
                        url: 'http://prod.com/',
                        debug: false
                    }
                }
            }
        }
    ]
};

var context = {
    env: 'dev'
};

var devConfig = require('JSONSharp').process(config, context);
```

Results in the following `devConfig` object:

```js
{
    debug: true, // Debug flag inherited from merging with the defaults
    url: 'http://dev.com/' // Url is replaced
}
```


## Operations

### `#merge`

The `#merge` operation takes a list of objects and deeply merges its properties
using the [deepmerge](https://www.npmjs.com/package/deepmerge) library.

Examples:

```js
JSONSharp.process({'#merge': [{a: 'a'}, {b: 'b']}, {});
// ==> {a: 'a', b: 'b'}
```


### `#switch`

The `#switch` operation works much like the [switch](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/switch) Javascript statement, with the exception that it doesn't
use a `break` statement.

It takes an object with the following properties:

* `#property`: the property name or `JSONPath` to be matched for results
* `#case`: an object mapping `#property` values to desired results
* `#case.#default`: the value will be used if no matching value is found

Examples:

```js
var switchObj = {
    '#switch': {
        '#property': 'name',
        '#case': {a: 'Prop A', '#default': 'not found'}
    }
};

JSONSharp.process(switchObj, {});
// ==> "not found"

JSONSharp.process(switchObj, {name: 'a'});
// ==> "Prop A"

JSONSharp.process(switchObj, {name: '$.a'});
// ==> "Prop A"
```


## Property resolution

A `#property` starting with `$.` will be resolved using the
[JSONPath](https://www.npmjs.com/package/JSONPath) library, otherwise simple
property access will be used.

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