Skip to content

Commit

Permalink
Merge pull request #188 from dentrado/add-getIn
Browse files Browse the repository at this point in the history
Add getIn function
  • Loading branch information
Richard Feldman committed Feb 12, 2017
2 parents 5068f09 + b5231ca commit 2f8c108
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ Immutable.setIn(obj, ["type", "sub"], "Norwegian Ridgeback");

A second argument can be provided to perform a deep compare: `{deep: true}`.

### getIn

Returns the value at the given path. A default value can be provided as a second argument.

```javascript
var obj = Immutable({type: {main: "parrot", subtype: "Norwegian Blue"}, status: "alive"});
Immutable.getIn(obj, ["type", "subtype"]);
// returns "Norwegian Blue"
Immutable.getIn(obj, ["type", "class"], "Aves");
// returns "Aves"
```

### update

Returns an Immutable Object with a single property updated using the provided updater function.
Expand Down
8 changes: 8 additions & 0 deletions seamless-immutable.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function immutableInit(config) {
addPropertyTo(array, "setIn", arraySetIn);
addPropertyTo(array, "update", update);
addPropertyTo(array, "updateIn", updateIn);
addPropertyTo(array, "getIn", getIn);
}

for(var i = 0, length = array.length; i < length; i++) {
Expand Down Expand Up @@ -551,6 +552,11 @@ function immutableInit(config) {
return Immutable.setIn(this, path, updater.apply(initialVal, [initialVal].concat(restArgs)));
}

function getIn(path, defaultValue) {
var value = getInPath(this, path);
return value === undefined ? defaultValue : value;
}

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

Expand Down Expand Up @@ -587,6 +593,7 @@ function immutableInit(config) {
addPropertyTo(obj, "setIn", objectSetIn);
addPropertyTo(obj, "update", update);
addPropertyTo(obj, "updateIn", updateIn);
addPropertyTo(obj, "getIn", getIn);
}

return makeImmutable(obj, mutatingObjectMethods);
Expand Down Expand Up @@ -702,6 +709,7 @@ function immutableInit(config) {
Immutable.setIn = toStaticObjectOrArray(objectSetIn, arraySetIn);
Immutable.update = toStatic(update);
Immutable.updateIn = toStatic(updateIn);
Immutable.getIn = toStatic(getIn);
Immutable.flatMap = toStatic(flatMap);
Immutable.asObject = toStatic(asObject);
if (!globalConfig.use_static) {
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 2f8c108

Please sign in to comment.