|
16 | 16 |
|
17 | 17 | $ component install qualiancy/tea-merge
|
18 | 18 |
|
| 19 | +## Usage |
| 20 | + |
| 21 | +### merge (destination, source, ...) |
| 22 | + |
| 23 | +* **@param** _{Array|Object}_ destination |
| 24 | +* **@param** _{Array|Object}_ sources ... |
| 25 | +* **@return** _{Object}_ destination merge |
| 26 | + |
| 27 | +For each source, shallow merge its key/values to the |
| 28 | +destination. Sources are read in order, meaning the same |
| 29 | +key in a later source will overwrite the key's value set |
| 30 | +earlier. |
| 31 | + |
| 32 | +Also, this tool only supports objects and arrays. Furthermore, |
| 33 | +the destination and all sources must be of the same type. |
| 34 | + |
| 35 | +```js |
| 36 | +var merge = require('tea-merge'); |
| 37 | + |
| 38 | +// sample objects |
| 39 | +var a = { hello: 'universe', arr: [ { a: 'a' } ] } |
| 40 | + , b = { speak: 'loudly', arr: [ { b: 'b' }, { c: 'c' } }; |
| 41 | + |
| 42 | +merge(a, b); |
| 43 | +a.should.deep.equal({ |
| 44 | + hello: 'universe' |
| 45 | + , speak: 'loudly' |
| 46 | + , arr: [ |
| 47 | + { a: 'a', b: 'b' } |
| 48 | + , { c: 'c' } |
| 49 | + ] |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +When merging objects, it is expected that if the |
| 54 | +key from the source already exists in the destination, |
| 55 | +the existing value in the source supports the same type of |
| 56 | +iteration as in the destination. If they cannot, a |
| 57 | +`Incompatible merge scenario.` error will be thrown. |
| 58 | + |
| 59 | +##### Rules |
| 60 | + |
| 61 | +- Non-iterable values can be replaced with other |
| 62 | +non-iterable values: strings, numbers, etc. |
| 63 | +- Iterable values cannot replace non-iterable |
| 64 | +values; objects can't replace string, arrays, can't |
| 65 | +replace numbers, etc. |
| 66 | +- Non-iterable values cannot replace iterable |
| 67 | +values; numbers can't replace objects, strings can't |
| 68 | +replace arrays, etc. |
| 69 | + |
| 70 | + |
19 | 71 | ## License
|
20 | 72 |
|
21 | 73 | (The MIT License)
|
|
0 commit comments