Skip to content

Commit

Permalink
Expose isEquals escape hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodny committed Jul 10, 2017
1 parent cee08c9 commit 18b6b5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 6 additions & 1 deletion index.js
Expand Up @@ -33,6 +33,7 @@ function newContext() {
update.extend = function(directive, fn) {
commands[directive] = fn;
};
update.isEquals = function(a, b) { return a === b; };

return update;

Expand All @@ -58,10 +59,14 @@ function newContext() {
var index, key;
getAllKeys(spec).forEach(function(key) {
if (hasOwnProperty.call(commands, key)) {
var objectWasNextObject = object === nextObject;
nextObject = commands[key](spec[key], nextObject, spec, object);
if (objectWasNextObject && update.isEquals(nextObject, object)) {
nextObject = object;
}
} else {
var nextValueForKey = update(object[key], spec[key]);
if (nextValueForKey !== nextObject[key]) {
if (!update.isEquals(nextValueForKey, nextObject[key])) {
if (nextObject === object) {
nextObject = copy(object);
}
Expand Down
23 changes: 19 additions & 4 deletions test.js
Expand Up @@ -324,11 +324,13 @@ describe('update', function() {


describe('update', function() {

var myUpdate;
beforeEach(function() {
myUpdate = update.newContext();
});

describe('can extend functionality', function() {
var myUpdate;
beforeEach(function() {
myUpdate = update.newContext();
});

it('allows adding new directives', function() {
myUpdate.extend('$addtax', function(tax, original) {
Expand Down Expand Up @@ -399,4 +401,17 @@ describe('update', function() {
expect(update.bind(null, obj, {$merge: {a: 'b'}})).toNotThrow()
});

it('supports an escape hatch for isEquals', function() {
myUpdate.isEquals = function(a, b) {
return JSON.stringify(a) === JSON.stringify(b);
}
var a = {b: {c: {d: [4, 5]}}};
var b = myUpdate(a, {b: {c: {d: {$set: [4, 5]}}}});
var c = myUpdate(a, {b: {$set: {c: {d: [4, 5]}}}});
var d = myUpdate(a, {$set: {b: {c: {d: [4, 5]}}}});
expect(a).toBe(b)
expect(a).toBe(c)
expect(a).toBe(d)
})

});

0 comments on commit 18b6b5d

Please sign in to comment.