Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update and updateIn functions #116

Merged
merged 3 commits into from
Apr 14, 2016
Merged
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
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