diff --git a/README.md b/README.md index 7af5b1d..e9f54e6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/seamless-immutable.development.js b/seamless-immutable.development.js index 84e6f50..c68c571 100644 --- a/seamless-immutable.development.js +++ b/seamless-immutable.development.js @@ -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++) { @@ -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; @@ -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); @@ -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) { diff --git a/seamless-immutable.development.min.js b/seamless-immutable.development.min.js index 0d771f9..fd4585d 100644 --- a/seamless-immutable.development.min.js +++ b/seamless-immutable.development.min.js @@ -1 +1 @@ -/* (c) 2016, Richard Feldman, github.com/rtfeldman/seamless-immutable/blob/master/LICENSE */!function(){"use strict";function a(b){function c(a){return"object"==typeof a&&!Array.isArray(a)&&null!==a}function d(a){var b=Object.getPrototypeOf(a);return b?Object.create(b):{}}function e(a,b,c){Object.defineProperty(a,b,{enumerable:!1,configurable:!1,writable:!1,value:c})}function f(a,b){e(a,b,function(){throw new k("The "+b+" method cannot be invoked on an Immutable data structure.")})}function g(a){e(a,R,!0)}function h(a){return"object"!=typeof a||(null===a||Boolean(Object.getOwnPropertyDescriptor(a,R)))}function i(a,b){return a===b||a!==a&&b!==b}function j(a){return!(null===a||"object"!=typeof a||Array.isArray(a)||a instanceof Date)}function k(a){this.name="MyError",this.message=a,this.stack=(new Error).stack}function l(a,b){g(a);for(var c in b)b.hasOwnProperty(c)&&f(a,b[c]);return Object.freeze(a),a}function m(a,b){var c=a[b];e(a,b,function(){return K(c.apply(a,arguments))})}function n(a,b,c){var d=c&&c.deep;if(a in this&&(d&&this[a]!==b&&j(b)&&j(this[a])&&(b=K.merge(this[a],b,{deep:!0,mode:"replace"})),i(this[a],b)))return this;var e=u.call(this);return e[a]=K(b),p(e)}function o(a,b,c){var d=a[0];if(1===a.length)return n.call(this,d,b,c);var e,f=a.slice(1),g=this[d];if("object"==typeof g&&null!==g)e=K.setIn(g,f,b);else{var h=f[0];e=""!==h&&isFinite(h)?o.call(X,f,b):A.call(Y,f,b)}if(d in this&&g===e)return this;var i=u.call(this);return i[d]=e,p(i)}function p(a){for(var b in V)if(V.hasOwnProperty(b)){var c=V[b];m(a,c)}Q.use_static||(e(a,"flatMap",s),e(a,"asObject",v),e(a,"asMutable",u),e(a,"set",n),e(a,"setIn",o),e(a,"update",C),e(a,"updateIn",E));for(var d=0,f=a.length;d