├── .travis.yml ├── .gitignore ├── bower.json ├── package.json ├── test ├── EqualityArrayMerger.spec.js ├── IgnoreArrayMerger.spec.js ├── ConcatArrayMerger.spec.js └── UpdatingByIdArrayMerger.spec.js ├── seamless-immutable-mergers.js ├── README.md └── LICENSE /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '5.0' 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | bower_components 3 | .idea 4 | *.iml 5 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "seamless-immutable-mergers", 3 | "version": "7.1.0", 4 | "description": "A collection of merger functions for seamless-immutable.", 5 | "main": "seamless-immutable-mergers.js", 6 | "dependencies": { 7 | "seamless-immutable": "^7.0.0" 8 | }, 9 | "moduleType": [ 10 | "amd", 11 | "globals", 12 | "node" 13 | ], 14 | "keywords": [ 15 | "seamless", 16 | "immutable", 17 | "merger" 18 | ], 19 | "authors": [ 20 | "Christian Rudh " 21 | ], 22 | "license": "Apache-2.0", 23 | "homepage": "https://github.com/crudh/seamless-immutable-mergers", 24 | "ignore": [ 25 | "**/.*", 26 | "node_modules", 27 | "bower_components", 28 | "test" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "seamless-immutable-mergers", 3 | "version": "7.0.0", 4 | "description": "A collection of merger functions for seamless-immutable.", 5 | "main": "seamless-immutable-mergers.js", 6 | "dependencies": { 7 | "seamless-immutable": "^7.0.0" 8 | }, 9 | "devDependencies": { 10 | "chai": "~3.5.0", 11 | "mocha": "~3.2.0" 12 | }, 13 | "scripts": { 14 | "test": "node_modules/mocha/bin/mocha test/*.spec.js" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/crudh/seamless-immutable-mergers.git" 19 | }, 20 | "keywords": [ 21 | "seamless", 22 | "immutable", 23 | "merger" 24 | ], 25 | "author": "Christian Rudh ", 26 | "license": "Apache-2.0", 27 | "bugs": { 28 | "url": "https://github.com/crudh/seamless-immutable-mergers/issues" 29 | }, 30 | "homepage": "https://github.com/crudh/seamless-immutable-mergers" 31 | } 32 | -------------------------------------------------------------------------------- /test/EqualityArrayMerger.spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var assert = require("chai").assert; 3 | var immutable = require("seamless-immutable"); 4 | var merger = require("../seamless-immutable-mergers").equalityArrayMerger; 5 | 6 | describe("EqualityArrayMerger", function() { 7 | var config = { 8 | merger: merger 9 | }; 10 | 11 | it("gives the same reference back if the arrays contains the same items", function() { 12 | var data = {a: [1, 2]}; 13 | var data2 = data; 14 | assert.equal(data, data2); 15 | 16 | var current = immutable(data); 17 | var resultWithoutMerger = current.merge(data2); 18 | assert.notEqual(current, resultWithoutMerger); 19 | 20 | var resultWithMerger = current.merge(data2, config); 21 | assert.equal(current, resultWithMerger); 22 | }); 23 | 24 | it("doesn't give the same reference back if the arrays contains different items", function() { 25 | var data = {a: [1, 2]} 26 | var data2 = {a: [2,1]}; 27 | assert.notEqual(data, data2); 28 | 29 | var current = immutable(data); 30 | var resultWithoutMerger = current.merge(data2); 31 | assert.notEqual(current, resultWithoutMerger); 32 | 33 | var resultWithMerger = current.merge(data2, config); 34 | assert.notEqual(current, resultWithMerger); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /seamless-immutable-mergers.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | (function (root, factory) { 3 | if (typeof define === 'function' && define.amd) { 4 | define(['seamless-immutable'], factory); 5 | } else if (typeof module === 'object' && module.exports) { 6 | module.exports = factory(require('seamless-immutable')); 7 | } else { 8 | root.returnExports = factory(root.b); 9 | } 10 | }(this, function (immutable) { 11 | function concatArrayMerger(current, other) { 12 | if (!(current instanceof Array) || !(other instanceof Array)) return; 13 | 14 | return current.concat(other); 15 | } 16 | 17 | function equalityArrayMerger(current, other) { 18 | if (!(current instanceof Array) || !(other instanceof Array)) return; 19 | if (current.length !== other.length) return; 20 | 21 | for (var i = 0; i < current.length; i++) { 22 | if (current[i] !== other[i]) return; 23 | } 24 | 25 | return current; 26 | } 27 | 28 | function ignoreArrayMerger(current, other) { 29 | if (!(current instanceof Array) || !(other instanceof Array)) return; 30 | 31 | return current; 32 | } 33 | 34 | function updatingByIdArrayMerger(current, other, config) { 35 | if (!(current instanceof Array) || !(other instanceof Array)) return; 36 | if (current.length === 0) return; 37 | if (other.length === 0) return current; 38 | 39 | var identifier = config.mergerObjectIdentifier; 40 | if (current[0] === null || !(typeof current[0] === "object") || !current[0][identifier]) return; 41 | if (other[0] === null || !(typeof other[0] === "object") || !other[0][identifier]) return; 42 | 43 | var currentMap = {}; 44 | for (var i = 0; i < current.length; i++) { 45 | currentMap[current[i][identifier]] = i; 46 | } 47 | 48 | var resultList = current.asMutable(); 49 | for (var j = 0; j < other.length; j++) { 50 | var matchingCurrentIndex = currentMap[other[j][identifier]]; 51 | 52 | if (matchingCurrentIndex === undefined) { 53 | var modifier = config.modifier; 54 | if (modifier !== 'push' && modifier !== 'unshift') 55 | modifier = 'push'; 56 | resultList[modifier](other[j]); 57 | } else { 58 | resultList[matchingCurrentIndex] = resultList[matchingCurrentIndex].merge(other[j], config); 59 | } 60 | } 61 | 62 | return immutable(resultList); 63 | } 64 | 65 | // Export the library 66 | var immutableMergers = { 67 | concatArrayMerger: concatArrayMerger, 68 | equalityArrayMerger: equalityArrayMerger, 69 | ignoreArrayMerger: ignoreArrayMerger, 70 | updatingByIdArrayMerger: updatingByIdArrayMerger 71 | }; 72 | 73 | Object.freeze(immutableMergers); 74 | 75 | return immutableMergers; 76 | })); 77 | -------------------------------------------------------------------------------- /test/IgnoreArrayMerger.spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var assert = require("chai").assert; 3 | var immutable = require("seamless-immutable"); 4 | var merger = require("../seamless-immutable-mergers").ignoreArrayMerger; 5 | 6 | describe("IgnoreArrayMerger", function() { 7 | var config = { 8 | deep: true, 9 | merger: merger 10 | }; 11 | 12 | it("merges like a normal merge with everything except arrays", function() { 13 | var current = immutable({ 14 | number: 1, 15 | string: "One", 16 | date: new Date(), 17 | object: { 18 | id: "object1" 19 | } 20 | }); 21 | 22 | var update = { 23 | number: 2, 24 | string: "Two", 25 | date: new Date(), 26 | object: { 27 | id: "object2" 28 | } 29 | }; 30 | 31 | var resultNormal = current.merge(update); 32 | var resultMerger = current.merge(update, config); 33 | 34 | assert.deepEqual(resultNormal, resultMerger); 35 | }); 36 | 37 | it("merges like a normal merge when there aren't two arrays to merge", function() { 38 | var current = immutable({ 39 | arrayOne: [1, 3, 5] 40 | }); 41 | 42 | var update = { 43 | arrayTwo: [2, 4] 44 | }; 45 | 46 | var resultNormal = current.merge(update); 47 | var resultMerger = current.merge(update, config); 48 | 49 | assert.deepEqual(resultNormal, resultMerger); 50 | }); 51 | 52 | it("merges arrays by keeping the current", function() { 53 | var current = immutable({ 54 | arrayOne: [1, 3, 5], 55 | arrayTwo: ["a", "c", "e"], 56 | arrayThree: [1, 3, 5], 57 | arrayFour: [] 58 | }); 59 | 60 | var update = { 61 | arrayOne: [2, 4], 62 | arrayTwo: ["b", "d", "f"], 63 | arrayThree: [], 64 | arrayFour: [2, 4] 65 | }; 66 | 67 | var result = current.merge(update, config); 68 | 69 | assert.equal(result.arrayOne, current.arrayOne); 70 | assert.equal(result.arrayTwo, current.arrayTwo); 71 | assert.equal(result.arrayThree, current.arrayThree); 72 | assert.equal(result.arrayFour, current.arrayFour); 73 | }); 74 | 75 | it("merges arrays deep by concatenating them togheter", function() { 76 | var current = immutable({ 77 | array: [1], 78 | sub: { 79 | arrayOne: [1, 3, 5], 80 | arrayTwo: ["a", "c", "e"], 81 | arrayThree: [1, 3, 5], 82 | arrayFour: [] 83 | } 84 | }); 85 | 86 | var update = { 87 | array: [2], 88 | sub: { 89 | arrayOne: [2, 4], 90 | arrayTwo: ["b", "d", "f"], 91 | arrayThree: [], 92 | arrayFour: [2, 4] 93 | } 94 | }; 95 | 96 | var result = current.merge(update, config); 97 | 98 | assert.equal(result.array, current.array); 99 | assert.equal(result.sub.arrayOne, current.sub.arrayOne); 100 | assert.equal(result.sub.arrayTwo, current.sub.arrayTwo); 101 | assert.equal(result.sub.arrayThree, current.sub.arrayThree); 102 | assert.equal(result.sub.arrayFour, current.sub.arrayFour); 103 | }); 104 | }); 105 | -------------------------------------------------------------------------------- /test/ConcatArrayMerger.spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var assert = require("chai").assert; 3 | var immutable = require("seamless-immutable"); 4 | var merger = require("../seamless-immutable-mergers").concatArrayMerger; 5 | 6 | describe("ConcatArrayMerger", function() { 7 | var config = { 8 | deep: true, 9 | merger: merger 10 | }; 11 | 12 | it("merges like a normal merge with everything except arrays", function() { 13 | var current = immutable({ 14 | number: 1, 15 | string: "One", 16 | date: new Date(), 17 | object: { 18 | id: "object1" 19 | } 20 | }); 21 | 22 | var update = { 23 | number: 2, 24 | string: "Two", 25 | date: new Date(), 26 | object: { 27 | id: "object2" 28 | } 29 | }; 30 | 31 | var resultNormal = current.merge(update); 32 | var resultMerger = current.merge(update, config); 33 | 34 | assert.deepEqual(resultNormal, resultMerger); 35 | }); 36 | 37 | it("merges like a normal merge when there aren't two arrays to merge", function() { 38 | var current = immutable({ 39 | arrayOne: [1, 3, 5] 40 | }); 41 | 42 | var update = { 43 | arrayTwo: [2, 4] 44 | }; 45 | 46 | var resultNormal = current.merge(update); 47 | var resultMerger = current.merge(update, config); 48 | 49 | assert.deepEqual(resultNormal, resultMerger); 50 | }); 51 | 52 | it("merges arrays by concatenating them togheter", function() { 53 | var current = immutable({ 54 | arrayOne: [1, 3, 5], 55 | arrayTwo: ["a", "c", "e"], 56 | arrayThree: [1, 3, 5], 57 | arrayFour: [] 58 | }); 59 | 60 | var update = { 61 | arrayOne: [2, 4], 62 | arrayTwo: ["b", "d", "f"], 63 | arrayThree: [], 64 | arrayFour: [2, 4] 65 | }; 66 | 67 | var result = current.merge(update, config); 68 | 69 | assert.sameMembers(result.arrayOne, [1, 3, 5, 2, 4]); 70 | assert.sameMembers(result.arrayTwo, ["a", "c", "e", "b", "d", "f"]); 71 | assert.sameMembers(result.arrayThree, [1, 3, 5]); 72 | assert.sameMembers(result.arrayFour, [2, 4]); 73 | }); 74 | 75 | it("merges arrays deep by concatenating them togheter", function() { 76 | var current = immutable({ 77 | array: [1], 78 | sub: { 79 | arrayOne: [1, 3, 5], 80 | arrayTwo: ["a", "c", "e"], 81 | arrayThree: [1, 3, 5], 82 | arrayFour: [] 83 | } 84 | }); 85 | 86 | var update = { 87 | array: [2], 88 | sub: { 89 | arrayOne: [2, 4], 90 | arrayTwo: ["b", "d", "f"], 91 | arrayThree: [], 92 | arrayFour: [2, 4] 93 | } 94 | }; 95 | 96 | var result = current.merge(update, config); 97 | 98 | assert.sameMembers(result.array, [1,2]); 99 | assert.sameMembers(result.sub.arrayOne, [1, 3, 5, 2, 4]); 100 | assert.sameMembers(result.sub.arrayTwo, ["a", "c", "e", "b", "d", "f"]); 101 | assert.sameMembers(result.sub.arrayThree, [1, 3, 5]); 102 | assert.sameMembers(result.sub.arrayFour, [2, 4]); 103 | }); 104 | }); 105 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | seamless-immutable-mergers 2 | ========================== 3 | This contains a set of custom mergers for the [seamless-immutable](https://github.com/rtfeldman/seamless-immutable) library. It's mainly a showcase for what can be done with custom mergers, but the mergers are hopefully useful on their own. 4 | 5 | [![Build Status](https://travis-ci.org/crudh/seamless-immutable-mergers.svg?branch=master)](https://travis-ci.org/crudh/seamless-immutable-mergers) 6 | 7 | ## Installation 8 | Any of the following: 9 | * `npm install seamless-immutable-mergers` 10 | * `bower install seamless-immutable-mergers` 11 | * download *seamless-immutable-mergers.js* 12 | 13 | Make sure that you have *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* loaded and in scope before using *seamless-immutable-mergers*. 14 | 15 | Note: If you are using *[requirejs](http://requirejs.org/)* or similar then make sure that *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* is exported as `seamless-immutable` so *seamless-immutable-mergers* can require the dependency. 16 | 17 | ## The merge API of seamless-immutable 18 | If you have an immutable object that you want to merge with another object then you do the following in *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)*: 19 | 20 | ```javascript 21 | var result = immutableObject.merge(otherObject); 22 | ``` 23 | 24 | All properties in `otherObject` will be added to `immutableObject` overwriting any duplicates. Properties in 25 | `immutableObject` that aren't in `otherObject` will be left as is. 26 | 27 | ### Deep merge 28 | You can also pass in configuration to the merge process. For example using the deep option: 29 | 30 | ```javascript 31 | var result = immutableObject.merge(otherObject, {deep: true}); 32 | ``` 33 | 34 | This will recursively merge all properties that are objects and exists in both the source and target instead of replacing them. 35 | 36 | ### Custom mergers 37 | You can also pass in a custom merger that overrides the normal merge process. For example: 38 | 39 | ```javascript 40 | var result = immutableObject.merge(otherObject, {merger: myCustomMerger}); 41 | ``` 42 | 43 | A merger is a function that takes the property for the current object, the property for the other object and optionally the config object. Example: 44 | 45 | ```javascript 46 | function myCustomMerger(current, other, config) { 47 | return undefined; 48 | } 49 | ``` 50 | 51 | If the merge returns `undefined` then the merge continues like normal. If the merger returns something else then that result is used instead. So checks can be added so a custom merger only operates on specific input, like arrays. 52 | 53 | ## Supplied mergers 54 | ### concatArrayMerger 55 | This is a simple merger that instead of replacing an array with another concats them together. Example: 56 | 57 | ```javascript 58 | var immutable = require("seamless-immutable"); 59 | var mergers = reuqire("seamless-immutable-mergers"); 60 | 61 | var immutableObject = immutable({ 62 | title: "one", 63 | items: [1, 2] 64 | }); 65 | 66 | var otherObject = { 67 | title: "two", 68 | items: [3, 4] 69 | }; 70 | 71 | var result = immutableObject.merge(otherObject, {merger: mergers.concatArrayMerger}); 72 | ``` 73 | 74 | The result will be: 75 | ```javascript 76 | { 77 | title: "two", 78 | items: [1, 2, 3, 4] 79 | } 80 | ``` 81 | 82 | ### equalityArrayMerger 83 | This is a merger that operates on arrays and compares the contents of the arrays. If both arrays contain the same elements (it checks each element using `===`) it will not replace the current array with the update and thus not flag that as a change. This means that if there are no other changes and the arrays contain the same elements the result of the merge will be the same object as original. Example: 84 | 85 | ```javascript 86 | var data = {a: [1, 2]}; 87 | var immutableObject = immutable(data); 88 | 89 | var data2 = {a: [1, 2]}; 90 | var result = immutableObject.merge(data2, {merger: mergers.equalityArrayMerger}); 91 | 92 | result === immutableObject 93 | // true 94 | ``` 95 | This can be useful for change detection, like in React's `shouldComponentUpdate`. 96 | 97 | 98 | ### ignoreArrayMerger 99 | This is a simple merger that instead of replacing an array with another keeps the original one. Example: 100 | 101 | ```javascript 102 | var immutable = require("seamless-immutable"); 103 | var mergers = reuqire("seamless-immutable-mergers"); 104 | 105 | var immutableObject = immutable({ 106 | title: "one", 107 | items: [1, 2] 108 | }); 109 | 110 | var otherObject = { 111 | title: "two", 112 | items: [3, 4] 113 | }; 114 | 115 | var result = immutableObject.merge(otherObject, {merger: mergers.ignoreArrayMerger}); 116 | ``` 117 | 118 | The result will be: 119 | ```javascript 120 | { 121 | title: "two", 122 | items: [1, 2] 123 | } 124 | ``` 125 | 126 | 127 | ### updatingByIdArrayMerger 128 | This is a merger that operates on arrays that contains objects with specified ids. It tries to merge each object in the target array with the object with the same id from the source array. Example: 129 | 130 | ```javascript 131 | var immutable = require("seamless-immutable"); 132 | var mergers = reuqire("seamless-immutable-mergers"); 133 | 134 | var immutableObject = immutable({ 135 | array: [ 136 | { 137 | id: 10, 138 | status: "ok", 139 | content: "text" 140 | } 141 | ] 142 | }); 143 | 144 | var otherObject = { 145 | array: [ 146 | { 147 | id: 10, 148 | status: "fail" 149 | }, 150 | { 151 | id: 11, 152 | status: "ok", 153 | content: "media" 154 | } 155 | ] 156 | }; 157 | 158 | var mergeConfig = { 159 | merger: mergers.updatingByIdArrayMerger, 160 | mergerObjectIdentifier: "id" 161 | }; 162 | var result = immutableObject.merge(otherObject, mergeConfig); 163 | ``` 164 | 165 | The result will be: 166 | ```javascript 167 | { 168 | array: [ 169 | { 170 | id: 10, 171 | status: "fail", 172 | content: "text" 173 | }, 174 | { 175 | id: 11, 176 | status: "ok", 177 | content: "media" 178 | } 179 | ] 180 | } 181 | ``` 182 | 183 | This merger requires that `mergerObjectIdentifier` is set in the config with the name of the property that identifies the object. 184 | It can be used to update and add to arrays using for example push from the server with only the updated data. 185 | 186 | This merger will check both arrays and only do anything if both of them has an object with the specified identifier at position 0. It will then assume that the rest of the arrays only contains such objects. 187 | 188 | It can also be used with the following options: 189 | 190 | ##### `deep?: boolean` 191 | 192 | Default is `false`. Control whether the merger should or not to do this recursively. 193 | 194 | ##### `modifier?: 'push' | 'unshift'` 195 | Default is `push`. Manages the way the new data is added to the array (first or last position - respectively). 196 | 197 | ## Releases 198 | ### 7.1.0 199 | *updatingByIdArrayMerger* received a config option for how new items are added to the array (thanks to @daviscabral) 200 | 201 | ### 7.0.0 202 | Updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 7.0.0 and bumped the major version to be in sync. 203 | 204 | ### 6.0.0 205 | Updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 6.0.0 and bumped the major version to be in sync. 206 | 207 | ### 5.0.1 208 | Updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 5.2.0. 209 | 210 | ### 5.0.0 211 | Updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 5.0.0 and bumped the major version to be in sync. Also updated chai and mocha test dependencies to the latest versions. 212 | 213 | ### 4.0.0 214 | Added *ignoreArrayMerger*, updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 4.0.2 and bumped the major version to be in sync. 215 | 216 | ### 3.0.2 217 | Fixed a bug in *updatingByIdArrayMerger* where a merge with an empty array would wipe the content of the current array. 218 | 219 | ### 3.0.0 220 | Updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 3.0.0 and bumped the major version to be in sync. 221 | 222 | ### 2.2.0 223 | Started using the UMD pattern so the library will be easy to consume as a global or with requirejs (and not only node/browserify). 224 | 225 | ### 2.1.0 226 | Added new merger: *equalityArrayMerger*. 227 | 228 | ### 2.0.0 229 | Initial release. 230 | -------------------------------------------------------------------------------- /test/UpdatingByIdArrayMerger.spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var assert = require("chai").assert; 3 | var immutable = require("seamless-immutable"); 4 | var merger = require("../seamless-immutable-mergers").updatingByIdArrayMerger; 5 | 6 | describe("UpdatingByIdArrayMerger", function() { 7 | var config = { 8 | deep: true, 9 | merger: merger, 10 | mergerObjectIdentifier: "id" 11 | }; 12 | 13 | function compareTestObjects(obj1, obj2) { 14 | assert.equal(obj1.id, obj2.id); 15 | assert.equal(obj1.data, obj2.data); 16 | } 17 | 18 | it("merges like a normal merge with everything except arrays", function() { 19 | var current = immutable({ 20 | number: 1, 21 | string: "One", 22 | date: new Date(), 23 | object: { 24 | id: "object1" 25 | } 26 | }); 27 | 28 | var update = { 29 | number: 2, 30 | string: "Two", 31 | date: new Date(), 32 | object: { 33 | id: "object2" 34 | } 35 | }; 36 | 37 | var resultNormal = current.merge(update); 38 | var resultMerger = current.merge(update, config); 39 | 40 | assert.deepEqual(resultNormal, resultMerger); 41 | }); 42 | 43 | it("merges like a normal merge when there aren't two arrays to merge", function() { 44 | var current = immutable({ 45 | arrayOne: [1, 3, 5] 46 | }); 47 | 48 | var update = { 49 | arrayTwo: [2, 4] 50 | }; 51 | 52 | var resultNormal = current.merge(update); 53 | var resultMerger = current.merge(update, config); 54 | 55 | assert.deepEqual(resultNormal, resultMerger); 56 | }); 57 | 58 | it("merges like a normal merge when there aren't two arrays with objects that contains the specified id", function() { 59 | var current = immutable({ 60 | arrayOne: [1, 3, 5], 61 | arrayTwo: [], 62 | arrayThree: [{data: 1}], 63 | arrayFour: [{id: 1, data: 2}], 64 | arrayFive: [] 65 | }); 66 | 67 | var update = { 68 | arrayOne: [2, 4], 69 | arrayTwo: [5, 5], 70 | arrayThree: [{data: 2}, {data: 4}], 71 | arrayFour: [{id: 1, data: 3}, {id: 2, data: 4}], 72 | arrayFive: [{id: 1, data: 2}] 73 | }; 74 | 75 | var result = current.merge(update, config); 76 | 77 | assert.sameMembers(result.arrayOne, [2, 4]); 78 | assert.sameMembers(result.arrayTwo, [5, 5]); 79 | 80 | assert.equal(result.arrayThree.length, 2); 81 | compareTestObjects(result.arrayThree[0], update.arrayThree[0]); 82 | compareTestObjects(result.arrayThree[1], update.arrayThree[1]); 83 | 84 | assert.equal(result.arrayFour.length, 2); 85 | compareTestObjects(result.arrayFour[0], update.arrayFour[0]); 86 | compareTestObjects(result.arrayFour[1], update.arrayFour[1]); 87 | 88 | assert.equal(result.arrayFive.length, 1); 89 | compareTestObjects(result.arrayFive[0], update.arrayFive[0]); 90 | }); 91 | 92 | it("correctly merges objects in arrays", function() { 93 | var current = immutable({ 94 | array: [ 95 | { 96 | id: 10, 97 | status: "ok", 98 | content: "text" 99 | } 100 | ] 101 | }); 102 | 103 | var update = { 104 | array: [ 105 | { 106 | id: 10, 107 | status: "fail" 108 | }, 109 | { 110 | id: 11, 111 | status: "ok", 112 | content: "media" 113 | } 114 | ] 115 | }; 116 | 117 | var result = current.merge(update, config); 118 | assert.equal(result.array.length, 2); 119 | 120 | var firstObject = result.array[0]; 121 | assert.equal(firstObject.id, 10); 122 | assert.equal(firstObject.status, "fail"); 123 | assert.equal(firstObject.content, "text"); 124 | 125 | var secondObject = result.array[1]; 126 | assert.equal(secondObject.id, 11); 127 | assert.equal(secondObject.status, "ok"); 128 | assert.equal(secondObject.content, "media"); 129 | }); 130 | 131 | it("deeply merges arrays in merged objects", function() { 132 | var current = immutable({ 133 | array: [ 134 | { 135 | id: 10, 136 | status: "ok", 137 | content: "text", 138 | items: [ 139 | { 140 | id: 100, 141 | status: "ok", 142 | content: "text" 143 | } 144 | ] 145 | } 146 | ] 147 | }); 148 | 149 | var update = { 150 | array: [ 151 | { 152 | id: 10, 153 | items: [ 154 | { 155 | id: 101, 156 | status: "ok", 157 | content: "media" 158 | }, 159 | { 160 | id: 100, 161 | status: "fail" 162 | } 163 | ] 164 | } 165 | ] 166 | }; 167 | 168 | var result = current.merge(update, config); 169 | assert.equal(result.array.length, 1); 170 | 171 | var resultObject = result.array[0]; 172 | assert.equal(resultObject.id, 10); 173 | assert.equal(resultObject.status, "ok"); 174 | assert.equal(resultObject.content, "text"); 175 | 176 | var items = resultObject.items; 177 | assert.equal(items.length, 2); 178 | 179 | assert.equal(items[0].id, 100); 180 | assert.equal(items[0].status, "fail"); 181 | assert.equal(items[0].content, "text"); 182 | 183 | assert.equal(items[1].id, 101); 184 | assert.equal(items[1].status, "ok"); 185 | assert.equal(items[1].content, "media"); 186 | }); 187 | 188 | it("doesn't empty an array when the push contains an empty array", function() { 189 | var current = immutable({ 190 | array: [ 191 | { 192 | id: 10, 193 | status: "ok", 194 | content: "text", 195 | items: [ 196 | { 197 | id: 100, 198 | status: "ok", 199 | content: "text" 200 | } 201 | ] 202 | } 203 | ] 204 | }); 205 | 206 | var update = { 207 | array: [] 208 | }; 209 | 210 | var result = current.merge(update, config); 211 | assert.equal(result.array.length, 1); 212 | }); 213 | 214 | it("doesn't deeply empty an array when the push contains an empty array", function() { 215 | var current = immutable({ 216 | array: [ 217 | { 218 | id: 10, 219 | status: "ok", 220 | content: "text", 221 | items: [ 222 | { 223 | id: 100, 224 | status: "ok", 225 | content: "text" 226 | } 227 | ] 228 | } 229 | ] 230 | }); 231 | 232 | var update = { 233 | array: [ 234 | { 235 | id: 10, 236 | items: [] 237 | } 238 | ] 239 | }; 240 | 241 | var result = current.merge(update, config); 242 | assert.equal(result.array.length, 1); 243 | 244 | var resultObject = result.array[0]; 245 | var items = resultObject.items; 246 | assert.equal(items.length, 1); 247 | }); 248 | 249 | it("adds new items to the end when no modifier is provided", function() { 250 | var current = immutable({ 251 | array: [ 252 | { 253 | id: '6a230f52-a757-11e8-8ed2-3c15c2de1bfa', 254 | name: 'Categories' 255 | }, 256 | { 257 | id: '73c3d1a4-a757-11e8-9634-3c15c2de1bfa', 258 | name: 'Tags' 259 | } 260 | ] 261 | }); 262 | 263 | var update = { 264 | array: [ 265 | { 266 | id: 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa', 267 | name: 'Posts' 268 | } 269 | ] 270 | }; 271 | 272 | var result = current.merge(update, config); 273 | 274 | assert.equal(result.array[0].id, '6a230f52-a757-11e8-8ed2-3c15c2de1bfa'); 275 | assert.equal(result.array[1].id, '73c3d1a4-a757-11e8-9634-3c15c2de1bfa'); 276 | assert.equal(result.array[2].id, 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa'); 277 | }); 278 | 279 | it("adds new items to the beginning when unshift modifier is provided", function() { 280 | var current = immutable({ 281 | array: [ 282 | { 283 | id: '6a230f52-a757-11e8-8ed2-3c15c2de1bfa', 284 | name: 'Categories' 285 | }, 286 | { 287 | id: '73c3d1a4-a757-11e8-9634-3c15c2de1bfa', 288 | name: 'Tags' 289 | } 290 | ] 291 | }); 292 | 293 | var update = { 294 | array: [ 295 | { 296 | id: 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa', 297 | name: 'Posts' 298 | } 299 | ] 300 | }; 301 | 302 | var result = current.merge(update, Object.assign({ modifier: 'unshift' }, config)); 303 | 304 | assert.equal(result.array[0].id, 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa'); 305 | assert.equal(result.array[1].id, '6a230f52-a757-11e8-8ed2-3c15c2de1bfa'); 306 | assert.equal(result.array[2].id, '73c3d1a4-a757-11e8-9634-3c15c2de1bfa'); 307 | }); 308 | 309 | it("adds new items to the end when an invalid modifier is provided", function() { 310 | var current = immutable({ 311 | array: [ 312 | { 313 | id: '6a230f52-a757-11e8-8ed2-3c15c2de1bfa', 314 | name: 'Categories' 315 | }, 316 | { 317 | id: '73c3d1a4-a757-11e8-9634-3c15c2de1bfa', 318 | name: 'Tags' 319 | } 320 | ] 321 | }); 322 | 323 | var update = { 324 | array: [ 325 | { 326 | id: 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa', 327 | name: 'Posts' 328 | } 329 | ] 330 | }; 331 | 332 | var result = current.merge(update, Object.assign({ modifier: 'invalid' }, config)); 333 | 334 | assert.equal(result.array[0].id, '6a230f52-a757-11e8-8ed2-3c15c2de1bfa'); 335 | assert.equal(result.array[1].id, '73c3d1a4-a757-11e8-9634-3c15c2de1bfa'); 336 | assert.equal(result.array[2].id, 'a9e79932-a757-11e8-b3e7-3c15c2de1bfa'); 337 | }); 338 | }); 339 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------