# pinch

> String.replace but for JavaScript objects (and JSON).

Latest version **0.1.3** (published 2012-03-19) · 0 weekly downloads

## Install

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

## 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.3 |
| Published | 2012-03-19 |
| First published | 2012-03-05 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.6.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | František Hába |
| Maintainers | baggz |
| Keywords | JSON, JSON replace, object replace, string replace, match, regex, replace, replacement, pattern, substr, String.replace |

## Links

- npm: https://www.npmjs.com/package/pinch
- Repository: https://github.com/Baggz/Pinch
- Issues: https://github.com/Baggz/Pinch/issues
- npm.io page: https://npm.io/package/pinch

## 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

- 0.1.3 (latest) — 2012-03-19
- 0.1.2 — 2012-03-17
- 0.1.1 — 2012-03-06
- 0.1.0 — 2012-03-05

## README

# Pinch

[![Build Status](https://secure.travis-ci.org/Baggz/Pinch.png?branch=master)](http://travis-ci.org/Baggz/Pinch)

Pinch is a small JavaScript utility which is able to **replace any data in a JavaScript object** (or JSON). You just need to provide a key *(for instance `users[0].name`)* in a *dot notation* or a *square bracket notation* and a replacement. The replacement can be a *string* or a *function* to be called for each match.

### Example

```javascript
var data = {
  users: [
    {
      name: 'John'
    },
    {
      name: 'Kenneth',
    },
    {
      name: 'Brent'
    }
  ]
};

// This step is optional. Pinch accepts JSON as well
// as a regular JavaScript object.
data = JSON.stringify(data);

pinch(data, 'users[0].name', 'Juan');
```

To be honest, in the first example Pinch is overkill. Pinch is very useful for **replacing multiple keys**, *e.g. converting some values to a number*. See the following example.

```javascript
var data = {
  user: {
    id: '123'
  },
  request: {
    id: '456'
  },
  book: {
    id: '789'
  }
};

// Converts all ids to a number
pinch(data, /id/, function(path, key, value) {
  return parseInt(value);
});
```

<a name="Contents"></a>
### Contents

<ul>
  <li><a href="#Download">Download</a>
  <li><a href="#Usage">Usage</a>
  <li><a href="#Documentation">Documentation</a>
  <li><a href="#Compatibility">Compatibility</a>
  <li><a href="#Tests">Tests</a>
  <li><a href="#Contributors">Contributors</a>
  <li><a href="#License">License</a>
</ul>

<a name="Download"></a>
## Download [&uarr;](#Contents)

To install **Pinch**, use [NPM](http://npmjs.org/).

```
$ npm install pinch
```

Releases are available for download from GitHub.

| **Version** | **Description** | **Size** | **Action** |
|:------------|:----------------|:---------|:-----------|
| `pinch.js` | *uncompressed, with comments* | 5.34 KB (1.68 KB gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.js) |
| `pinch.min.js` | *compressed, without comments* | 1.85 KB (852 bytes gzipped) | [Download](https://raw.github.com/Baggz/Pinch/master/dist/latest.min.js) |

<a name="Usage"></a>
## Usage [&uarr;](#Contents)

### Browser

```
<script src="./path/to/pinch.js"></script>
```

### Node.js, RingoJS, Narwhal

```javascript
var pinch = require('pinch');
```

### RequireJS

```javascript
// Configuration options, the path should not include the .js extension
require.config({
  paths: {
    "pinch": "path/to/pinch"
  }
});

// Load Pinch
require(['pinch'], function(pinch) {
  // Do something...
});
```

<a name="Documentation"></a>
## Documentation [&uarr;](#Contents)

### pinch(instance, pattern, replacement[, callback])

**Parameters**

* `instance` An instance could be a string as well as a regular JavaScript object
* `pattern` A pattern could be a regular expression or a string
* `replacement` A replacement could be a string or a function
* `callback`

**Notation**

Here are just a few examples of a dot notation and a square bracket notation.

* `users[0]`
* `users[0].books[0].title`
* `users[0]["full name"]`
* `user.name.en`

*Examples*

```javascript
var instance = {
  name: 'František'
};

pinch(instance, 'name', 'Juan'); // => { name: 'Juan' }
```

```javascript
var instance = [
  {
    user: {
      name: 'Edwin'
    }
  },
  {
    user: {
      name: 'Teresa'
    }
  }
];

pinch(instance, '[0].user.name', 'Juan'); =>  [{ user: { name: 'Juan' } }, ... ]
```

```javascript
var instance = [
  {
    user: {
      id: '123'
    },
    request: {
      id: '456'
    }
  }
];

pinch(instance, /id/, function(path, key, value) {
  return parseInt(value);
});
```

<a name="Compatibility"></a>
## Compatibility [&uarr;](#Contents)

### Node.js

From version **0.6.0**.

### Browsers

**Desktop**

| **Browser** | **Supported** |
|:------------|:-----------:|
| Google Chrome | ✔ |
| Safari | n/a |
| Firefox | n/a |
| Opera | n/a |
| Internet Explorer | n/a |

*Testing in progress...*

<a name="Tests"></a>
## Tests [&uarr;](#Contents)

```
$ npm test
```

<a name="Contributors"></a>
## Contributors [&uarr;](#Contents)

The following are the major contributors of Amanda (in random order).

* **František Hába** ([@Baggz](https://github.com/Baggz))

<a name="License"></a>
## License [&uarr;](#Contents)

(The MIT License)

Copyright (c) 2011 František Hába &lt;hello@frantisekhaba.com&gt;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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