From 53946e74bf6e68c6ab191025404abb414d502f9d Mon Sep 17 00:00:00 2001 From: Martin Forsgren Date: Tue, 27 Dec 2016 16:29:10 +0100 Subject: [PATCH 1/3] Add getIn function --- seamless-immutable.development.js | 7 ++++ seamless-immutable.development.min.js | 2 +- seamless-immutable.production.min.js | 2 +- src/seamless-immutable.js | 7 ++++ test/ImmutableArray.spec.js | 2 + test/ImmutableArray/test-getIn.js | 34 +++++++++++++++++ test/ImmutableObject.spec.js | 2 + test/ImmutableObject/test-getIn.js | 53 +++++++++++++++++++++++++++ 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 test/ImmutableArray/test-getIn.js create mode 100644 test/ImmutableObject/test-getIn.js diff --git a/seamless-immutable.development.js b/seamless-immutable.development.js index d17773e..97ff523 100644 --- a/seamless-immutable.development.js +++ b/seamless-immutable.development.js @@ -204,6 +204,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++) { @@ -552,6 +553,10 @@ function immutableInit(config) { return Immutable.setIn(this, path, updater.apply(initialVal, [initialVal].concat(restArgs))); } + function getIn(path) { + return getInPath(this, path); + } + function asMutableObject(opts) { var result = instantiateEmptyObject(this), key; @@ -588,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); @@ -703,6 +709,7 @@ function immutableInit(config) { Immutable.setIn = toStaticObjectOrArray(objectSetIn, arraySetIn); Immutable.update = toStatic(update); Immutable.updateIn = toStatic(updateIn); + Immutable.getIn = getInPath; 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 627deb9..bad1cb9 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)):!0}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){var b=new Error(a);return b.__proto__=k,b}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;f>d;d++)a[d]=K(a[d]);return l(a,U)}function q(a){return Q.use_static||e(a,"asMutable",r),l(a,W)}function r(){return new Date(this.getTime())}function s(a){if(0===arguments.length)return this;var b,c=[],d=this.length;for(b=0;d>b;b++){var e=a(this[b],b,this);Array.isArray(e)?c.push.apply(c,e):c.push(e)}return p(c)}function t(a){if("undefined"==typeof a&&0===arguments.length)return this;if("function"!=typeof a){var b=Array.isArray(a)?a.slice():Array.prototype.slice.call(arguments);b.forEach(function(a,b,c){"number"==typeof a&&(c[b]=a.toString())}),a=function(a,c){return-1!==b.indexOf(c)}}var c=d(this);for(var e in this)this.hasOwnProperty(e)&&a(this[e],e)===!1&&(c[e]=this[e]);return H(c)}function u(a){var b,c,d=[];if(a&&a.deep)for(b=0,c=this.length;c>b;b++)d.push(w(this[b]));else for(b=0,c=this.length;c>b;b++)d.push(this[b]);return d}function v(a){"function"!=typeof a&&(a=function(a){return a});var b,c={},d=this.length;for(b=0;d>b;b++){var e=a(this[b],b,this),f=e[0],g=e[1];c[f]=g}return H(c)}function w(a){return!a||"object"!=typeof a||!Object.getOwnPropertyDescriptor(a,R)||a instanceof Date?a:K.asMutable(a,{deep:!0})}function x(a,b){for(var c in a)Object.getOwnPropertyDescriptor(a,c)&&(b[c]=a[c]);return b}function y(a,b){function c(a,c,e){var g=K(c[e]),h=m&&m(a[e],g,b),l=a[e];if(void 0!==f||void 0!==h||!a.hasOwnProperty(e)||!i(g,l)){var n;n=h?h:k&&j(l)&&j(g)?K.merge(l,g,b):g,i(l,n)&&a.hasOwnProperty(e)||(void 0===f&&(f=x(a,d(a))),f[e]=n)}}function e(a,b){for(var c in a)b.hasOwnProperty(c)||(void 0===f&&(f=x(a,d(a))),delete f[c])}if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#merge can only be invoked with objects or arrays, not "+JSON.stringify(a));var f,g,h=Array.isArray(a),k=b&&b.deep,l=b&&b.mode||"merge",m=b&&b.merger;if(h)for(var n=0,o=a.length;o>n;n++){var p=a[n];for(g in p)p.hasOwnProperty(g)&&c(void 0!==f?f:this,p,g)}else{for(g in a)Object.getOwnPropertyDescriptor(a,g)&&c(this,a,g);"replace"===l&&e(this,a)}return void 0===f?this:H(f)}function z(a,b){var c=b&&b.deep;if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#replace can only be invoked with objects or arrays, not "+JSON.stringify(a));return K.merge(this,a,{deep:c,mode:"replace"})}function A(a,b,c){if(!(a instanceof Array)||0===a.length)throw new TypeError('The first argument to Immutable#setIn must be an array containing at least one "key" string.');var e=a[0];if(1===a.length)return B.call(this,e,b,c);var f,g=a.slice(1),h=this[e];if(f=this.hasOwnProperty(e)&&"object"==typeof h&&null!==h?K.setIn(h,g,b):A.call(Y,g,b),this.hasOwnProperty(e)&&h===f)return this;var i=x(this,d(this));return i[e]=f,H(i)}function B(a,b,c){var e=c&&c.deep;if(this.hasOwnProperty(a)&&(e&&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 f=x(this,d(this));return f[a]=K(b),H(f)}function C(a,b){var c=Array.prototype.slice.call(arguments,2),d=this[a];return K.set(this,a,b.apply(d,[d].concat(c)))}function D(a,b){for(var c=0,d=b.length;null!=a&&d>c;c++)a=a[b[c]];return c&&c==d?a:void 0}function E(a,b){var c=Array.prototype.slice.call(arguments,2),d=D(this,a);return K.setIn(this,a,b.apply(d,[d].concat(c)))}function F(a){var b,c=d(this);if(a&&a.deep)for(b in this)this.hasOwnProperty(b)&&(c[b]=w(this[b]));else for(b in this)this.hasOwnProperty(b)&&(c[b]=this[b]);return c}function G(){return{}}function H(a){return Q.use_static||(e(a,"merge",y),e(a,"replace",z),e(a,"without",t),e(a,"asMutable",F),e(a,"set",B),e(a,"setIn",A),e(a,"update",C),e(a,"updateIn",E)),l(a,S)}function I(a){return"object"==typeof a&&null!==a&&(a.$$typeof===P||a.$$typeof===O)}function J(a){return"undefined"!=typeof File&&a instanceof File}function K(a,b,c){if(h(a)||I(a)||J(a))return a;if(Array.isArray(a))return p(a.slice());if(a instanceof Date)return q(new Date(a.getTime()));var d=b&&b.prototype,e=d&&d!==Object.prototype?function(){return Object.create(d)}:G,f=e();if(null==c&&(c=64),0>=c)throw new k("Attempt to construct Immutable from a deeply nested object was detected. Have you tried to wrap an object with circular references (e.g. React element)? See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");c-=1;for(var g in a)Object.getOwnPropertyDescriptor(a,g)&&(f[g]=K(a[g],void 0,c));return H(f)}function L(a){function b(){var b=[].slice.call(arguments),c=b.shift();return a.apply(c,b)}return b}function M(a,b){function c(){var c=[].slice.call(arguments),d=c.shift();return Array.isArray(d)?b.apply(d,c):a.apply(d,c)}return c}function N(a,b,c){function d(){var d=[].slice.call(arguments),e=d.shift();return Array.isArray(e)?b.apply(e,d):e instanceof Date?c.apply(e,d):a.apply(e,d)}return d}var O="function"==typeof Symbol&&Symbol["for"]&&Symbol["for"]("react.element"),P=60103,Q={use_static:!1};c(b)&&void 0!==b.use_static&&(Q.use_static=Boolean(b.use_static));var R="__immutable_invariants_hold",S=["setPrototypeOf"],T=["keys"],U=S.concat(["push","pop","sort","splice","shift","unshift","reverse"]),V=T.concat(["map","filter","slice","concat","reduce","reduceRight"]),W=S.concat(["setDate","setFullYear","setHours","setMilliseconds","setMinutes","setMonth","setSeconds","setTime","setUTCDate","setUTCFullYear","setUTCHours","setUTCMilliseconds","setUTCMinutes","setUTCMonth","setUTCSeconds","setYear"]);k.prototype=Error.prototype;var X=K([]),Y=K({});return K.from=K,K.isImmutable=h,K.ImmutableError=k,K.merge=L(y),K.replace=L(z),K.without=L(t),K.asMutable=N(F,u,r),K.set=M(B,n),K.setIn=M(A,o),K.update=L(C),K.updateIn=L(E),K.flatMap=L(s),K.asObject=L(v),Q.use_static||(K["static"]=a({use_static:!0})),Object.freeze(K),K}var b=a();"function"==typeof define&&define.amd?define(function(){return b}):"object"==typeof module?module.exports=b:"object"==typeof exports?exports.Immutable=b:"object"==typeof window?window.Immutable=b:"object"==typeof global&&(global.Immutable=b)}(); \ No newline at end of file +/* (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,S,!0)}function h(a){return"object"!=typeof a||(null===a||Boolean(Object.getOwnPropertyDescriptor(a,S)))}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){var b=new Error(a);return b.__proto__=k,b}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 L(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=L.merge(this[a],b,{deep:!0,mode:"replace"})),i(this[a],b)))return this;var e=u.call(this);return e[a]=L(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=L.setIn(g,f,b);else{var h=f[0];e=""!==h&&isFinite(h)?o.call(Y,f,b):A.call(Z,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 W)if(W.hasOwnProperty(b)){var c=W[b];m(a,c)}R.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),e(a,"getIn",F));for(var d=0,f=a.length;dd;d++)a[d]=J(a[d]);return k(a,T)}function p(a){return P.use_static||e(a,"asMutable",q),k(a,V)}function q(){return new Date(this.getTime())}function r(a){if(0===arguments.length)return this;var b,c=[],d=this.length;for(b=0;d>b;b++){var e=a(this[b],b,this);Array.isArray(e)?c.push.apply(c,e):c.push(e)}return o(c)}function s(a){if("undefined"==typeof a&&0===arguments.length)return this;if("function"!=typeof a){var b=Array.isArray(a)?a.slice():Array.prototype.slice.call(arguments);b.forEach(function(a,b,c){"number"==typeof a&&(c[b]=a.toString())}),a=function(a,c){return-1!==b.indexOf(c)}}var c=d(this);for(var e in this)this.hasOwnProperty(e)&&a(this[e],e)===!1&&(c[e]=this[e]);return G(c)}function t(a){var b,c,d=[];if(a&&a.deep)for(b=0,c=this.length;c>b;b++)d.push(v(this[b]));else for(b=0,c=this.length;c>b;b++)d.push(this[b]);return d}function u(a){"function"!=typeof a&&(a=function(a){return a});var b,c={},d=this.length;for(b=0;d>b;b++){var e=a(this[b],b,this),f=e[0],g=e[1];c[f]=g}return G(c)}function v(a){return!a||"object"!=typeof a||!Object.getOwnPropertyDescriptor(a,Q)||a instanceof Date?a:J.asMutable(a,{deep:!0})}function w(a,b){for(var c in a)Object.getOwnPropertyDescriptor(a,c)&&(b[c]=a[c]);return b}function x(a,b){function c(a,c,e){var g=J(c[e]),j=m&&m(a[e],g,b),l=a[e];if(void 0!==f||void 0!==j||!a.hasOwnProperty(e)||!h(g,l)){var n;n=j?j:k&&i(l)&&i(g)?J.merge(l,g,b):g,h(l,n)&&a.hasOwnProperty(e)||(void 0===f&&(f=w(a,d(a))),f[e]=n)}}function e(a,b){for(var c in a)b.hasOwnProperty(c)||(void 0===f&&(f=w(a,d(a))),delete f[c])}if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#merge can only be invoked with objects or arrays, not "+JSON.stringify(a));var f,g,j=Array.isArray(a),k=b&&b.deep,l=b&&b.mode||"merge",m=b&&b.merger;if(j)for(var n=0,o=a.length;o>n;n++){var p=a[n];for(g in p)p.hasOwnProperty(g)&&c(void 0!==f?f:this,p,g)}else{for(g in a)Object.getOwnPropertyDescriptor(a,g)&&c(this,a,g);"replace"===l&&e(this,a)}return void 0===f?this:G(f)}function y(a,b){var c=b&&b.deep;if(0===arguments.length)return this;if(null===a||"object"!=typeof a)throw new TypeError("Immutable#replace can only be invoked with objects or arrays, not "+JSON.stringify(a));return J.merge(this,a,{deep:c,mode:"replace"})}function z(a,b,c){if(!(a instanceof Array)||0===a.length)throw new TypeError('The first argument to Immutable#setIn must be an array containing at least one "key" string.');var e=a[0];if(1===a.length)return A.call(this,e,b,c);var f,g=a.slice(1),h=this[e];if(f=this.hasOwnProperty(e)&&"object"==typeof h&&null!==h?J.setIn(h,g,b):z.call(X,g,b),this.hasOwnProperty(e)&&h===f)return this;var i=w(this,d(this));return i[e]=f,G(i)}function A(a,b,c){var e=c&&c.deep;if(this.hasOwnProperty(a)&&(e&&this[a]!==b&&i(b)&&i(this[a])&&(b=J.merge(this[a],b,{deep:!0,mode:"replace"})),h(this[a],b)))return this;var f=w(this,d(this));return f[a]=J(b),G(f)}function B(a,b){var c=Array.prototype.slice.call(arguments,2),d=this[a];return J.set(this,a,b.apply(d,[d].concat(c)))}function C(a,b){for(var c=0,d=b.length;null!=a&&d>c;c++)a=a[b[c]];return c&&c==d?a:void 0}function D(a,b){var c=Array.prototype.slice.call(arguments,2),d=C(this,a);return J.setIn(this,a,b.apply(d,[d].concat(c)))}function E(a){var b,c=d(this);if(a&&a.deep)for(b in this)this.hasOwnProperty(b)&&(c[b]=v(this[b]));else for(b in this)this.hasOwnProperty(b)&&(c[b]=this[b]);return c}function F(){return{}}function G(a){return P.use_static||(e(a,"merge",x),e(a,"replace",y),e(a,"without",s),e(a,"asMutable",E),e(a,"set",A),e(a,"setIn",z),e(a,"update",B),e(a,"updateIn",D)),k(a,R)}function H(a){return"object"==typeof a&&null!==a&&(a.$$typeof===O||a.$$typeof===N)}function I(a){return"undefined"!=typeof File&&a instanceof File}function J(a,b,c){if(g(a)||H(a)||I(a))return a;if(Array.isArray(a))return o(a.slice());if(a instanceof Date)return p(new Date(a.getTime()));var d=b&&b.prototype,e=d&&d!==Object.prototype?function(){return Object.create(d)}:F,f=e();for(var h in a)Object.getOwnPropertyDescriptor(a,h)&&(f[h]=J(a[h],void 0,c));return G(f)}function K(a){function b(){var b=[].slice.call(arguments),c=b.shift();return a.apply(c,b)}return b}function L(a,b){function c(){var c=[].slice.call(arguments),d=c.shift();return Array.isArray(d)?b.apply(d,c):a.apply(d,c)}return c}function M(a,b,c){function d(){var d=[].slice.call(arguments),e=d.shift();return Array.isArray(e)?b.apply(e,d):e instanceof Date?c.apply(e,d):a.apply(e,d)}return d}var N="function"==typeof Symbol&&Symbol["for"]&&Symbol["for"]("react.element"),O=60103,P={use_static:!1};c(b)&&void 0!==b.use_static&&(P.use_static=Boolean(b.use_static));var Q="__immutable_invariants_hold",R=["setPrototypeOf"],S=["keys"],T=R.concat(["push","pop","sort","splice","shift","unshift","reverse"]),U=S.concat(["map","filter","slice","concat","reduce","reduceRight"]),V=R.concat(["setDate","setFullYear","setHours","setMilliseconds","setMinutes","setMonth","setSeconds","setTime","setUTCDate","setUTCFullYear","setUTCHours","setUTCMilliseconds","setUTCMinutes","setUTCMonth","setUTCSeconds","setYear"]);j.prototype=Error.prototype;var W=J([]),X=J({});return J.from=J,J.isImmutable=g,J.ImmutableError=j,J.merge=K(x),J.replace=K(y),J.without=K(s),J.asMutable=M(E,t,q),J.set=L(A,m),J.setIn=L(z,n),J.update=K(B),J.updateIn=K(D),J.flatMap=K(r),J.asObject=K(u),P.use_static||(J["static"]=a({use_static:!0})),Object.freeze(J),J}var b=a();"function"==typeof define&&define.amd?define(function(){return b}):"object"==typeof module?module.exports=b:"object"==typeof exports?exports.Immutable=b:"object"==typeof window?window.Immutable=b:"object"==typeof global&&(global.Immutable=b)}(); \ No newline at end of file +/* (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){e(a,R,!0)}function g(a){return"object"!=typeof a||(null===a||Boolean(Object.getOwnPropertyDescriptor(a,R)))}function h(a,b){return a===b||a!==a&&b!==b}function i(a){return!(null===a||"object"!=typeof a||Array.isArray(a)||a instanceof Date)}function j(a){var b=new Error(a);return b.__proto__=j,b}function k(a,b){f(a);return a}function l(a,b){var c=a[b];e(a,b,function(){return K(c.apply(a,arguments))})}function m(a,b,c){var d=c&&c.deep;if(a in this&&(d&&this[a]!==b&&i(b)&&i(this[a])&&(b=K.merge(this[a],b,{deep:!0,mode:"replace"})),h(this[a],b)))return this;var e=t.call(this);return e[a]=K(b),o(e)}function n(a,b,c){var d=a[0];if(1===a.length)return m.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)?n.call(X,f,b):z.call(Y,f,b)}if(d in this&&g===e)return this;var i=t.call(this);return i[d]=e,o(i)}function o(a){for(var b in V)if(V.hasOwnProperty(b)){var c=V[b];l(a,c)}Q.use_static||(e(a,"flatMap",r),e(a,"asObject",u),e(a,"asMutable",t),e(a,"set",m),e(a,"setIn",n),e(a,"update",B),e(a,"updateIn",D),e(a,"getIn",E));for(var d=0,f=a.length;d Date: Tue, 27 Dec 2016 16:42:37 +0100 Subject: [PATCH 2/3] Add support for giving a default value as second parameter --- seamless-immutable.development.js | 7 ++++--- seamless-immutable.development.min.js | 2 +- seamless-immutable.production.min.js | 2 +- src/seamless-immutable.js | 7 ++++--- test/ImmutableArray/test-getIn.js | 11 +++++++++++ test/ImmutableObject/test-getIn.js | 11 +++++++++++ 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/seamless-immutable.development.js b/seamless-immutable.development.js index 97ff523..e71ac02 100644 --- a/seamless-immutable.development.js +++ b/seamless-immutable.development.js @@ -553,8 +553,9 @@ function immutableInit(config) { return Immutable.setIn(this, path, updater.apply(initialVal, [initialVal].concat(restArgs))); } - function getIn(path) { - return getInPath(this, path); + function getIn(path, defaultValue) { + var value = getInPath(this, path); + return value === undefined ? defaultValue : value; } function asMutableObject(opts) { @@ -709,7 +710,7 @@ function immutableInit(config) { Immutable.setIn = toStaticObjectOrArray(objectSetIn, arraySetIn); Immutable.update = toStatic(update); Immutable.updateIn = toStatic(updateIn); - Immutable.getIn = getInPath; + 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 bad1cb9..951b360 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,S,!0)}function h(a){return"object"!=typeof a||(null===a||Boolean(Object.getOwnPropertyDescriptor(a,S)))}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){var b=new Error(a);return b.__proto__=k,b}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 L(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=L.merge(this[a],b,{deep:!0,mode:"replace"})),i(this[a],b)))return this;var e=u.call(this);return e[a]=L(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=L.setIn(g,f,b);else{var h=f[0];e=""!==h&&isFinite(h)?o.call(Y,f,b):A.call(Z,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 W)if(W.hasOwnProperty(b)){var c=W[b];m(a,c)}R.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),e(a,"getIn",F));for(var d=0,f=a.length;d Date: Tue, 27 Dec 2016 16:56:53 +0100 Subject: [PATCH 3/3] Add example to README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index eaedd0c..03a952f 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,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.