Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/addons/__tests__/update-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ describe('update', function() {
it('should support set', function() {
expect(update({a: 'b'}, {$set: {c: 'd'}})).toEqual({c: 'd'});
});

it('should keep reference equality when possible with set', function() {
var original = {a: 1};
expect(update(original, {a: {$set: 1}})).toBe(original);
});

it('should support apply', function() {
expect(update(2, {$apply: (x) => x * 2})).toEqual(4);
Expand All @@ -78,6 +83,14 @@ describe('update', function() {
'function; got 123.'
);
});

it('should keep reference equality when possible with apply', function() {
var original = {a: {b: {}}};
function identity(val) {
return val;
}
expect(update(original, {a: {$apply: identity}})).toBe(original);
});

it('should support deep updates', function() {
expect(update({a: 'b', c: {d: 'e'}}, {c: {d: {$set: 'f'}}})).toEqual({
Expand Down
45 changes: 34 additions & 11 deletions src/addons/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,26 @@ function update(value, spec) {
COMMAND_SET
);

return spec[COMMAND_SET];
// always return original if possible, to keep reference equality
return spec[COMMAND_SET] === value ? value : spec[COMMAND_SET];
}

var nextValue = shallowCopy(value);
// not created unless needed
var nextValue;
var updatedProp;

/* get a clone of value, create if not present */
function getNextValue() {
if (!nextValue) {
nextValue = shallowCopy(value);
}
return nextValue;
}

/* get newest available of nextValue or value */
function getCurrentValue() {
return nextValue ? nextValue : value;
}

if (hasOwnProperty.call(spec, COMMAND_MERGE)) {
var mergeObj = spec[COMMAND_MERGE];
Expand All @@ -97,25 +113,25 @@ function update(value, spec) {
mergeObj
);
invariant(
nextValue && typeof nextValue === 'object',
getCurrentValue() && typeof getCurrentValue() === 'object',
'update(): %s expects a target of type \'object\'; got %s',
COMMAND_MERGE,
nextValue
getCurrentValue()
);
assign(nextValue, spec[COMMAND_MERGE]);
assign(getNextValue(), spec[COMMAND_MERGE]);
}

if (hasOwnProperty.call(spec, COMMAND_PUSH)) {
invariantArrayCase(value, spec, COMMAND_PUSH);
spec[COMMAND_PUSH].forEach(function(item) {
nextValue.push(item);
getNextValue().push(item);
});
}

if (hasOwnProperty.call(spec, COMMAND_UNSHIFT)) {
invariantArrayCase(value, spec, COMMAND_UNSHIFT);
spec[COMMAND_UNSHIFT].forEach(function(item) {
nextValue.unshift(item);
getNextValue().unshift(item);
});
}

Expand All @@ -141,7 +157,7 @@ function update(value, spec) {
COMMAND_SPLICE,
spec[COMMAND_SPLICE]
);
nextValue.splice.apply(nextValue, args);
getNextValue().splice.apply(getNextValue(), args);
});
}

Expand All @@ -152,16 +168,23 @@ function update(value, spec) {
COMMAND_APPLY,
spec[COMMAND_APPLY]
);
nextValue = spec[COMMAND_APPLY](nextValue);

updatedProp = spec[COMMAND_APPLY](getCurrentValue());
if (updatedProp !== getCurrentValue()) {
nextValue = updatedProp;
}
}

for (var k in spec) {
if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
nextValue[k] = update(value[k], spec[k]);
updatedProp = update(value[k], spec[k]);
if (updatedProp !== value[k]) {
getNextValue()[k] = updatedProp;
}
}
}

return nextValue;
return getCurrentValue();
}

module.exports = update;