|
3 | 3 |
|
4 | 4 | Mixes/merges/extends your object in multiple ways. |
5 | 5 |
|
| 6 | +Unlike underscore/lodash utility methods this module allows you to: |
| 7 | +* mix or deep merge objects' prototype chain. Regular mixin/extend/assign implementations can't do that. |
| 8 | +* mix or deep merge unique properties only. I.e. no data will be overwritten if a property already exists. |
| 9 | +* filter each individual property by target value, source value, and key. See API. |
| 10 | +* transform each value by target value, source value, and key. See API. |
| 11 | + |
6 | 12 | ## Install |
7 | 13 | ```sh |
8 | 14 | $ npm install supermixer |
@@ -42,7 +48,7 @@ functionMixer({}, { a: "x" }, { b: function(){} }); |
42 | 48 |
|
43 | 49 | ### Mixin functions including prototype chain. |
44 | 50 | ```js |
45 | | -// assigns functions only, but traverse through the protorype chain |
| 51 | +// assigns functions only, but traverse through the prototype chain |
46 | 52 | var chainFunctionMixer = mixer({ |
47 | 53 | filter: function (val) { return typeof val === 'function' ; }, |
48 | 54 | chain: true |
@@ -97,5 +103,29 @@ mergeChainData(new EventEmitter()); |
97 | 103 | // { hello: "world" } |
98 | 104 | ``` |
99 | 105 |
|
| 106 | +## API |
| 107 | + |
| 108 | +### supermixer(opts = {}) |
| 109 | +The `opts`: |
| 110 | +``` |
| 111 | + * @param {Object} opts |
| 112 | + * @param {Function} opts.filter Function which filters value and key. |
| 113 | + * @param {Function} opts.transform Function which transforms each value. |
| 114 | + * @param {Boolean} opts.chain Loop through prototype properties too. |
| 115 | + * @param {Boolean} opts.deep Deep looping through the nested properties. |
| 116 | + * @param {Boolean} opts.noOverwrite Do not overwrite any existing data (aka first one wins). |
| 117 | +``` |
| 118 | + |
| 119 | +Usage: |
| 120 | +```js |
| 121 | +mixer({ |
| 122 | + filter(sourceValue, targetValue, key) { return key[0] !== '_'; }, // do not copy "private" values |
| 123 | + transform(resultValue, targetValue, key) { console.log(key); return resultValue; }, // log each key which gets set |
| 124 | + chain: true, |
| 125 | + deep: true, |
| 126 | + noOverwrite: true |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
100 | 130 | ## Want to contribute? |
101 | 131 | This project is Open Open Source. This means whoever submits an accepted PR will receive write permissions to the project. |
0 commit comments