Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/angus-c/obj-transform

Generates transform functions to apply to each property of an object
https://github.com/angus-c/obj-transform

Last synced: about 1 month ago
JSON representation

Generates transform functions to apply to each property of an object

Awesome Lists containing this project

README

        

## obj-transform

Generates transform functions to apply to each property of an object

### Usage

```js
// commonJS...
var transform = require('obj-transform');
// ...or es2015
import transform from 'obj-transform';

// an incrementing transformer
const increment = transform(x => x + 1);
// enlarge a rectangle
increment({height: 4, width: 3}); // {height: 5, width: 4}
// move south-east
increment({x: 0, y: 0}); // {x: 1, y: 1}
// looping transform
while (inbounds(ball)) {
increment(ball);
}

// ad-hoc transform
transform(x => x * x)({a: 1, b: 2, c: 3}); // {a: 1, b: 4, c: 9}

// a mixin transformer
var mixin = transform(function(value, key) {return this[key] || value});
mixin({a: 3, b: 9, c: 12}, {a: 1, c: 3}); // { a: 1, b: 9, c: 3 }
```

### Install

```
npm install obj-transform
```

### API

__Creating a transform function__
```js
transform(_callback_);
```

_callback_ is a function that can accept up to three arguments:

**value**
The value for each property in _baseObj_
**key** (optional)
The key for each property in _baseObj_
**baseObj** (optional)
The _baseObj_

__Using a transform function__
```js
transformFunction(_object_[, _thisValue_]);
```

### Tests

```
npm test
```