UNPKG

lazy-aggregator

Version:
81 lines (52 loc) 2 kB
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> ## lazy-aggregator Create lazy-initialized dictionary based on `source` dictionary handled with `fn` **Parameters** - `source` **[Object][1]&lt;[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 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 ## del 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 ## Installation npm install --save lazy-aggregator [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object [2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function [4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error