diff --git a/bower.json b/bower.json index dd9351b..5c81fc7 100644 --- a/bower.json +++ b/bower.json @@ -25,7 +25,7 @@ "type": "git", "url": "git://github.com/rhalff/dot-object.git" }, - "version": "0.11.0", + "version": "1.2.0", "homepage": "https://github.com/rhalff/dot-object", "moduleType": [ "amd", diff --git a/dist/dot-object.js b/dist/dot-object.js index b81ce51..d2ad37d 100644 --- a/dist/dot-object.js +++ b/dist/dot-object.js @@ -86,7 +86,7 @@ this._fill(a, obj[k], v, mod); } else { - if (obj[k] === Object(obj[k]) && Object.keys(obj[k]).length) { + if (!this.override && obj[k] === Object(obj[k]) && Object.keys(obj[k]).length) { throw new Error('Trying to redefine non-empty obj[\'' + k + '\']'); } @@ -131,6 +131,8 @@ obj[k] = _process(obj[k], mod); } }); + + return obj; }; /** @@ -145,6 +147,8 @@ } else if (this.override) { obj[path] = _process(v, mod); } + + return obj; }; /** @@ -425,6 +429,17 @@ DotObject.del = DotObject.remove = wrap('remove'); DotObject.dot = wrap('dot'); + ['override', 'overwrite'].forEach(function (prop) { + Object.defineProperty(DotObject, prop, { + get: function () { + return dotDefault.override; + }, + set: function (val) { + dotDefault.override = !! val; + } + }); + }); + DotObject._process = _process; diff --git a/dist/dot-object.min.js b/dist/dot-object.min.js index aead44d..4eec0dc 100644 --- a/dist/dot-object.min.js +++ b/dist/dot-object.min.js @@ -1 +1 @@ -!function(t,e){"use strict";function r(t,e){var r,i;if("function"==typeof e)i=e(t),void 0!==i&&(t=i);else if(Array.isArray(e))for(r=0;r=0&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(e)}function s(t,e){return this instanceof s?("undefined"==typeof t&&(t="."),"undefined"==typeof e&&(e=!1),this.seperator=t,this.override=e,void(this.cleanup=[])):new s(t,e)}function p(t){return function(){return c[t].apply(c,arguments)}}function f(t){return"[object Object]"===Object.prototype.toString.call(t)}var c=new s(".",!1);s.prototype._fill=function(t,e,i,o){var s=t.shift();if(t.length>0){if(e[s]=e[s]||(1===t.length&&n(t[0])?[]:{}),e[s]!==Object(e[s])){if(!this.override)throw new Error("Trying to redefine `"+s+"` which is a "+typeof e[s]);e[s]={}}this._fill(t,e[s],i,o)}else{if(e[s]===Object(e[s])&&Object.keys(e[s]).length)throw new Error("Trying to redefine non-empty obj['"+s+"']");e[s]=r(i,o)}},s.prototype.object=function(t,e){var i=this;Object.keys(t).forEach(function(n){var s=void 0===e?null:e[n],p=o(n,i.seperator).join(i.seperator);-1!==p.indexOf(i.seperator)?(i._fill(p.split(i.seperator),t,t[n],s),delete t[n]):i.override&&(t[n]=r(t[n],s))})},s.prototype.str=function(t,e,i,n){-1!==t.indexOf(this.seperator)?this._fill(t.split(this.seperator),i,e,n):this.override&&(i[t]=r(e,n))},s.prototype.pick=function(t,e,r){var n,s,p,f,c;for(s=o(t,this.seperator),n=0;n=0&&(t=t.replace(/\[/g,".").replace(/]/g,"")),t.split(e)}function s(t,e){return this instanceof s?("undefined"==typeof t&&(t="."),"undefined"==typeof e&&(e=!1),this.seperator=t,this.override=e,void(this.cleanup=[])):new s(t,e)}function p(t){return function(){return c[t].apply(c,arguments)}}function f(t){return"[object Object]"===Object.prototype.toString.call(t)}var c=new s(".",!1);s.prototype._fill=function(t,e,i,o){var s=t.shift();if(t.length>0){if(e[s]=e[s]||(1===t.length&&n(t[0])?[]:{}),e[s]!==Object(e[s])){if(!this.override)throw new Error("Trying to redefine `"+s+"` which is a "+typeof e[s]);e[s]={}}this._fill(t,e[s],i,o)}else{if(!this.override&&e[s]===Object(e[s])&&Object.keys(e[s]).length)throw new Error("Trying to redefine non-empty obj['"+s+"']");e[s]=r(i,o)}},s.prototype.object=function(t,e){var i=this;return Object.keys(t).forEach(function(n){var s=void 0===e?null:e[n],p=o(n,i.seperator).join(i.seperator);-1!==p.indexOf(i.seperator)?(i._fill(p.split(i.seperator),t,t[n],s),delete t[n]):i.override&&(t[n]=r(t[n],s))}),t},s.prototype.str=function(t,e,i,n){return-1!==t.indexOf(this.seperator)?this._fill(t.split(this.seperator),i,e,n):this.override&&(i[t]=r(e,n)),i},s.prototype.pick=function(t,e,r){var n,s,p,f,c;for(s=o(t,this.seperator),n=0;n