lazy-aggregator
Version:
Creates aggregation wrapper
81 lines (52 loc) • 2 kB
Markdown
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
Create lazy-initialized dictionary based on `source` dictionary handled with `fn`
**Parameters**
- `source` **[Object][1]<[string][2], any>** dictionary of configs for `fn` call (optional, default `{}`)
- `fn` **[function][3]** function which will be used to create elements in aggregation (optional, default `function(key,value){returnvalue}`)
**Examples**
```javascript
import Aggregation from 'lazy-aggregator';
const fn = (a) => a * 2;
const source = { el1: 1, el2: 2};
const aggregation = new Aggregation(source, fn);
aggregation['el1']; // 1
aggregation.el2; // 2
```
- Throws **[Error][4]** if `fn` is not a function
add element to aggregation
**Parameters**
- `key` **[String][2]** first argument to `fn`
- `value` **any?** data for `fn` call
**Examples**
```javascript
import Aggregation from 'lazy-aggregator';
import {add} from 'lazy-aggregator';
const fn = (a) => a * 2;
const source = { el1: 1, el2: 2};
const aggregation = new Aggregation({}, fn);
aggregation[add]('newEl', 2);
aggregation.newEl; // 4
```
Returns **[Object][1]** this
delete element from aggregation
**Parameters**
- `key` **[String][2]** key of aggregation member to delete
**Examples**
```javascript
import Aggregation from 'lazy-aggregator';
import {del} from 'lazy-aggregator';
const fn = (a) => a * 2;
const aggregation = new Aggregation({newEl: 3}, fn);
aggregation[del]('newEl');
aggregation.newEl; // undefined
```
Returns **[Object][1]** this
npm install --save lazy-aggregator
[]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error