Skip to content

Commit

Permalink
Merge pull request #10 from crudh/ignore-array-merger
Browse files Browse the repository at this point in the history
Add ignoreArrayMerger
  • Loading branch information
crudh committed Nov 25, 2015
2 parents 07a90ba + ee60fa4 commit 36d94bc
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 7 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ result === immutableObject
```
This can be useful for change detection, like in React's `shouldComponentUpdate`.


### ignoreArrayMerger
This is a simple merger that instead of replacing an array with another keeps the original one. Example:

```javascript
var immutable = require("seamless-immutable");
var mergers = reuqire("seamless-immutable-mergers");

var immutableObject = immutable({
title: "one",
items: [1, 2]
});

var otherObject = {
title: "two",
items: [3, 4]
};

var result = immutableObject.merge(otherObject, {merger: mergers.ignoreArrayMerger});
```

The result will be:
```javascript
{
title: "two",
items: [1, 2]
}
```


### updatingByIdArrayMerger
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:

Expand Down Expand Up @@ -155,6 +185,9 @@ This merger will check both arrays and only do anything if both of them has an o
It can also be used with the `deep` configuration to do this recursively.

## Releases
### 4.0.0
Added *ignoreArrayMerger*, updated to *[seamless-immutable](https://github.com/rtfeldman/seamless-immutable)* 4.0.2 and bumped the major version to be in sync.

### 3.0.2
Fixed a bug in *updatingByIdArrayMerger* where a merge with an empty array would wipe the content of the current array.

Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"name": "seamless-immutable-mergers",
"version": "3.0.2",
"version": "4.0.0",
"description": "A collection of merger functions for seamless-immutable.",
"main": "seamless-immutable-mergers.js",
"dependencies": {
"seamless-immutable": "~3.0.0"
},
"browserDependencies": {
"seamless-immutable": "~3.0.0"
"seamless-immutable": "~4.0.2"
},
"devDependencies": {
"chai": "~1.9.2",
Expand Down
11 changes: 9 additions & 2 deletions seamless-immutable-mergers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
return current;
}

function ignoreArrayMerger(current, other) {
if (!(current instanceof Array) || !(other instanceof Array)) return;

return current;
}

function updatingByIdArrayMerger(current, other, config) {
if (!(current instanceof Array) || !(other instanceof Array)) return;
if (current.length === 0) return;
Expand Down Expand Up @@ -56,8 +62,9 @@
// Export the library
var immutableMergers = {
concatArrayMerger: concatArrayMerger,
updatingByIdArrayMerger: updatingByIdArrayMerger,
equalityArrayMerger: equalityArrayMerger
equalityArrayMerger: equalityArrayMerger,
ignoreArrayMerger: ignoreArrayMerger,
updatingByIdArrayMerger: updatingByIdArrayMerger
};

Object.freeze(immutableMergers);
Expand Down
104 changes: 104 additions & 0 deletions test/IgnoreArrayMerger.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"use strict";
var assert = require("chai").assert;
var immutable = require("seamless-immutable");
var merger = require("../seamless-immutable-mergers").ignoreArrayMerger;

describe("IgnoreArrayMerger", function() {
var config = {
deep: true,
merger: merger
};

it("merges like a normal merge with everything except arrays", function() {
var current = immutable({
number: 1,
string: "One",
date: new Date(),
object: {
id: "object1"
}
});

var update = {
number: 2,
string: "Two",
date: new Date(),
object: {
id: "object2"
}
};

var resultNormal = current.merge(update);
var resultMerger = current.merge(update, config);

assert.deepEqual(resultNormal, resultMerger);
});

it("merges like a normal merge when there aren't two arrays to merge", function() {
var current = immutable({
arrayOne: [1, 3, 5]
});

var update = {
arrayTwo: [2, 4]
};

var resultNormal = current.merge(update);
var resultMerger = current.merge(update, config);

assert.deepEqual(resultNormal, resultMerger);
});

it("merges arrays by keeping the current", function() {
var current = immutable({
arrayOne: [1, 3, 5],
arrayTwo: ["a", "c", "e"],
arrayThree: [1, 3, 5],
arrayFour: []
});

var update = {
arrayOne: [2, 4],
arrayTwo: ["b", "d", "f"],
arrayThree: [],
arrayFour: [2, 4]
};

var result = current.merge(update, config);

assert.equal(result.arrayOne, current.arrayOne);
assert.equal(result.arrayTwo, current.arrayTwo);
assert.equal(result.arrayThree, current.arrayThree);
assert.equal(result.arrayFour, current.arrayFour);
});

it("merges arrays deep by concatenating them togheter", function() {
var current = immutable({
array: [1],
sub: {
arrayOne: [1, 3, 5],
arrayTwo: ["a", "c", "e"],
arrayThree: [1, 3, 5],
arrayFour: []
}
});

var update = {
array: [2],
sub: {
arrayOne: [2, 4],
arrayTwo: ["b", "d", "f"],
arrayThree: [],
arrayFour: [2, 4]
}
};

var result = current.merge(update, config);

assert.equal(result.array, current.array);
assert.equal(result.sub.arrayOne, current.sub.arrayOne);
assert.equal(result.sub.arrayTwo, current.sub.arrayTwo);
assert.equal(result.sub.arrayThree, current.sub.arrayThree);
assert.equal(result.sub.arrayFour, current.sub.arrayFour);
});
});

0 comments on commit 36d94bc

Please sign in to comment.