Skip to content

Commit

Permalink
Merge pull request #116 from evgenykochetkov/add-update-and-updateIn
Browse files Browse the repository at this point in the history
Add update and updateIn functions
  • Loading branch information
Richard Feldman committed Apr 14, 2016
2 parents a11878c + 17e51ff commit 7e4cb33
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 3 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,34 @@ Immutable({type: {main: "parrot", sub: "Norwegian Blue"}, status: "alive"}).setI
// returns Immutable({type: {main: "parrot", sub: "Norwegian Ridgeback"}, status: "alive"})
```

### update

Returns an Immutable Object with a single property updated using the provided updater function.

```javascript
function inc (x) { return x + 1 }
Immutable({foo: 1}).update("foo", inc)
// returns Immutable({foo: 2})
```

All additional arguments will be passed to the updater function.

```javascript
function add (x, y) { return x + y }
Immutable({foo: 1}).update("foo", add, 10)
// returns Immutable({foo: 11})
```

### updateIn

Like [update](#update), but accepts a nested path to the property.

```javascript
function add (x, y) { return x + y }
Immutable({foo: {bar: 1}}).updateIn(["foo", "bar"], add, 10)
// returns Immutable({foo: {bar: 11}})
```

### without

```javascript
Expand Down
26 changes: 26 additions & 0 deletions seamless-immutable.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
addPropertyTo(array, "asMutable", asMutableArray);
addPropertyTo(array, "set", arraySet);
addPropertyTo(array, "setIn", arraySetIn);
addPropertyTo(array, "update", update);
addPropertyTo(array, "updateIn", updateIn);

for(var i = 0, length = array.length; i < length; i++) {
array[i] = Immutable(array[i]);
Expand Down Expand Up @@ -430,6 +432,28 @@
return makeImmutableObject(mutable, this);
}

function update(property, updater) {
var restArgs = Array.prototype.slice.call(arguments, 2);
var initialVal = this[property];
return this.set(property, updater.apply(initialVal, [initialVal].concat(restArgs)));
}

function getInPath(obj, path) {
/*jshint eqnull:true */
for (var i = 0, l = path.length; obj != null && i < l; i++) {
obj = obj[path[i]];
}

return (i && i == l) ? obj : undefined;
}

function updateIn(path, updater) {
var restArgs = Array.prototype.slice.call(arguments, 2);
var initialVal = getInPath(this, path);

return this.setIn(path, updater.apply(initialVal, [initialVal].concat(restArgs)));
}

function asMutableObject(opts) {
var result = this.instantiateEmptyObject(), key;

Expand Down Expand Up @@ -467,6 +491,8 @@
addPropertyTo(obj, "instantiateEmptyObject", instantiateEmptyObject);
addPropertyTo(obj, "set", objectSet);
addPropertyTo(obj, "setIn", objectSetIn);
addPropertyTo(obj, "update", update);
addPropertyTo(obj, "updateIn", updateIn);

return makeImmutable(obj, mutatingObjectMethods);
}
Expand Down
2 changes: 1 addition & 1 deletion seamless-immutable.development.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e4cb33

Please sign in to comment.