From c3946e1087b4557883fffe2433ebd89781103ba0 Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Thu, 9 Apr 2015 22:11:02 +0200 Subject: [PATCH] Bump 0.4.4 --- bower.json | 2 +- build/baobab.min.js | 4 ++-- index.js | 2 +- package.json | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bower.json b/bower.json index 02b31c0..a55acc9 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "baobab", "main": "build/baobab.min.js", - "version": "0.4.3", + "version": "0.4.4", "homepage": "https://github.com/Yomguithereal/baobab", "author": { "name": "Guillaume Plique", diff --git a/build/baobab.min.js b/build/baobab.min.js index f42aeff..34eda33 100644 --- a/build/baobab.min.js +++ b/build/baobab.min.js @@ -1,2 +1,2 @@ -/* baobab.js - Version: 0.4.3 - Author: Yomguithereal (Guillaume Plique) */ -!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Baobab=t()}}(function(){var t;return function e(t,r,n){function o(s,a){if(!r[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);var h=new Error("Cannot find module '"+s+"'");throw h.code="MODULE_NOT_FOUND",h}var c=r[s]={exports:{}};t[s][0].call(c.exports,function(e){var r=t[s][1][e];return o(r?r:e)},c,c.exports,e,t,r,n)}return r[s].exports}for(var i="function"==typeof require&&require,s=0;se;e++)n.push(t[e].handler);return n}var o={once:"boolean",scope:"object"},i=function(){this._enabled=!0,this._children=[],this._handlers={},this._handlersAll=[]};i.prototype.on=function(t,e,r){var n,s,a,u,h,c;if("function"==typeof e){for(h="string"==typeof t?[t]:t,n=0,s=h.length;n!==s;n+=1)if(u=h[n]){this._handlers[u]||(this._handlers[u]=[]),c={handler:e};for(a in r||{}){if(!o[a])throw new Error('The option "'+a+'" is not recognized by Emmett.');c[a]=r[a]}this._handlers[u].push(c)}}else if(t&&"object"==typeof t&&!Array.isArray(t))for(u in t)i.prototype.on.call(this,u,t[u],e);else{if("function"!=typeof t)throw new Error("Wrong arguments.");c={handler:t};for(a in r||{}){if(!o[a])throw new Error('The option "'+a+'" is not recognized by Emmett.');c[a]=r[a]}this._handlersAll.push(c)}return this},i.prototype.once=function(t,e,r){if("function"==typeof e)r=r||{},r.once=!0,this.on(t,e,r);else{if((!t||"object"!=typeof t||Array.isArray(t))&&"function"!=typeof t)throw new Error("Wrong arguments.");e=e||{},e.once=!0,this.on(t,e)}return this},i.prototype.off=function(t,e){var r,n,o,i,s,a,u,h="string"==typeof t?[t]:t;if(1===arguments.length&&"function"==typeof h){e=arguments[0];for(s in this._handlers){for(a=[],r=0,n=this._handlers[s].length;r!==n;r+=1)this._handlers[s][r].handler!==e&&a.push(this._handlers[s][r]);this._handlers[s]=a}for(a=[],r=0,n=this._handlersAll.length;r!==n;r+=1)this._handlersAll[r].handler!==e&&a.push(this._handlersAll[r]);this._handlersAll=a}else if(2===arguments.length)for(r=0,n=h.length;r!==n;r+=1){if(u=h[r],this._handlers[u]){for(a=[],o=0,i=this._handlers[u].length;o!==i;o+=1)this._handlers[u][o].handler!==e&&a.push(this._handlers[u][o]);this._handlers[u]=a}this._handlers[u]&&0===this._handlers[u].length&&delete this._handlers[u]}return this},i.prototype.unbindAll=function(){var t;this._handlersAll=[];for(t in this._handlers)delete this._handlers[t];return this},i.prototype.emit=function(t,e){var r,n,o,i,s,a,u,h,c,l,f="string"==typeof t?[t]:t;if(!this._enabled)return this;for(e=void 0===e?{}:e,r=0,n=f.length;r!==n;r+=1)if(l=f[r],c=(this._handlers[l]||[]).concat(this._handlersAll),c.length){for(u={type:l,data:e||{},target:this},a=[],o=0,i=c.length;o!==i;o+=1)(this._handlers[l]&&this._handlers[l].indexOf(c[o])>=0||this._handlersAll.indexOf(c[o])>=0)&&(c[o].handler.call("scope"in c[o]?c[o].scope:this,u),c[o].once&&a.push(c[o]));for(s=0;sr;r++)if(t._children[r]===e){t._children.splice(r,1);break}}),this._children.push(e),e},i.prototype.listeners=function(t){var r,n,o,i=[];if(t)for(i=e(this._handlers[t]),n=0,o=this._children.length;o>n;n++)i=i.concat(this._children[n].listeners(t));else{i=e(this._handlersAll);for(r in this._handlers)i=i.concat(e(this._handlers[r]));for(n=0,o=this._children.length;o>n;n++)i=i.concat(this._children[n].listeners())}return i},i.prototype.kill=function(){if(this.emit("emmett:kill"),this.unbindAll(),this._handlers=null,this._handlersAll=null,this._enabled=!1,this._children)for(var t=0,e=this._children.length;e>t;t++)this._children[t].kill();this._children=null},i.prototype.disable=function(){return this._enabled=!1,this},i.prototype.enable=function(){return this._enabled=!0,this},i.version="2.1.2","undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=i),n.Emitter=i):"function"==typeof t&&t.amd?t("emmett",[],function(){return i}):this.Emitter=i}).call(this)},{}],4:[function(e,r,n){!function(){"use strict";function e(t){function e(t,o){var s,a,u,h,c,l,f,p,d=!1,y=!1,g=r.get(t);if("string"===r.get(o)){for(s=o.replace(/^[\?\!]/,"").split(/\|/),u=s.length,a=0;u>a;a++)if(i.indexOf(s[a])<0&&!(s[a]in n))throw new Error("Invalid type.");if(o.match(/^\?/)&&(d=!0),o.replace(/^\?/,"").match(/^\!/)&&(y=!0),y&&d)throw new Error("Invalid type.");for(a in s)if(n[s[a]]&&("function"==typeof n[s[a]].type?n[s[a]].type.call(r,t)===!0:!e(t,n[s[a]].type)))return y?(c=new Error,c.message='Expected a "'+o+'" but found a "'+s[a]+'".',c.expected=o,c.type=s[a],c.value=t,c):null;return null===t||void 0===t?y||d?null:(c=new Error,c.message='Expected a "'+o+'" but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c):(f=~s.indexOf("*"),p=~s.indexOf(g),y&&(f||p)?(c=new Error,c.message='Expected a "'+o+'" but found a "'+(p?g:"*")+'".',c.type=p?g:"*",c.expected=o,c.value=t,c):y||f||p?null:(c=new Error,c.message='Expected a "'+o+'" but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c))}if("object"===r.get(o)){if("object"!==g)return c=new Error,c.message='Expected an object but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c;for(h in o)if(l=e(t[h],o[h]))return c=l,c.path=c.path?[h].concat(c.path):[h],c;for(h in t)if(void 0===o[h])return c=new Error,c.message='Unexpected key "'+h+'".',c.type=g,c.value=t,c;return null}if("array"===r.get(o)){if(1!==o.length)throw new Error("Invalid type.");if("array"!==g)return c=new Error,c.message='Expected an array but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c;for(u=t.length,a=0;u>a;a++)if(l=e(t[a],o[0]))return c=l,c.path=c.path?[a].concat(c.path):[a],c;return null}throw new Error("Invalid type.")}var r=this,n={};if(this.add=function(t,e){var r,o,s,a,u,h;if(1===arguments.length){if("object"!==this.get(t))throw new Error("If types.add is called with one argument, this one has to be an object.");r=t,a=r.id,h=r.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or two arguments.");if("string"!=typeof t||!t)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");a=t,h=e}if("string"!==this.get(a)||0===a.length)throw new Error("A type requires an string id.");if(void 0!==n[a]&&"proto"!==n[a])throw new Error('The type "'+a+'" already exists.');if(~i.indexOf(a))throw new Error('"'+a+'" is a reserved type name.');n[a]=1,s=(r||{}).proto||[],s=Array.isArray(s)?s:[s],u={};for(o in s)void 0===n[s[o]]&&(n[s[o]]=1,u[s[o]]=1);if("function"!==this.get(h)&&!this.isValid(h))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(n[a]=void 0===r?{id:a,type:h}:{},void 0!==r)for(o in r)n[a][o]=r[o];for(o in u)o!==a&&delete n[o];return this},this.has=function(t){return!!n[t]},this.get=function(t){return null===t||void 0===t?String(t):o[Object.prototype.toString.call(t)]||"object"},this.check=function(t,r,n){var o=e(t,r);if(n&&o)throw o;return!o},this.isValid=function(t){var e,r,o;if("string"===this.get(t)){e=t.replace(/^[\?\!]/,"").split(/\|/);for(o in e)if(i.indexOf(e[o])<0&&!(e[o]in n))return!1;return!0}if("object"===this.get(t)){for(r in t)if(!this.isValid(t[r]))return!1;return!0}return"array"===this.get(t)&&1===t.length?this.isValid(t[0]):!1},this.add("type",function(t){return this.isValid(t)}.bind(this)),this.add("primitive",function(t){return!t||!(t instanceof Object||"object"==typeof t)}),t=t||{},"object"!==this.get(t))throw Error("Invalid argument.");for(var s in t)this.add(s,t[s])}var o={},i=["*"];!function(){var t,e,r=["Arguments","Boolean","Number","String","Function","Array","Date","RegExp","Object"];for(t in r)e=r[t],i.push(e.toLowerCase()),o["[object "+e+"]"]=e.toLowerCase()}();var s=e;e.call(s),Object.defineProperty(s,"version",{value:"0.3.1"}),"undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=s),n.types=s):"function"==typeof t&&t.amd?t("typology",[],function(){return s}):this.types=s}(this)},{}],5:[function(t,e){function r(t){return t+"$"+(new Date).getTime()+(""+Math.random()).replace("0.","")}function n(t,e){if(arguments.length<1&&(t={}),!(this instanceof n))return new n(t,e);if(!f.Object(t)&&!f.Array(t))throw Error("Baobab: invalid data.");if(i.call(this),this.options=a.shallowMerge(l,e),this._cloner=this.options.cloningFunction||a.deepClone,this._transaction={},this._future=void 0,this._history=[],this._cursors={},this.typology=this.options.typology?this.options.typology instanceof s?this.options.typology:new s(this.options.typology):new s,this.validate=this.options.validate||null,this.validate)try{this.typology.check(t,this.validate,!0)}catch(r){throw r.message="/"+r.path.join("/")+": "+r.message,r}this.data=this._cloner(t),this.mixin=c.baobab(this)}var o=t("./cursor.js"),i=t("emmett"),s=t("typology"),a=t("./helpers.js"),u=t("./update.js"),h=t("./merge.js"),c=t("./mixins.js"),l=t("../defaults.js"),f=t("./type.js");a.inherits(n,i),n.prototype._archive=function(){if(!(this.options.maxHistory<=0)){var t={data:this._cloner(this.data)};return this._history.length===this.options.maxHistory&&this._history.pop(),this._history.unshift(t),t}},n.prototype.commit=function(t){var e;if(t)this.data=t.data,e=t.log;else{this.options.shiftReferences&&(this.data=a.shallowClone(this.data));var r=this._archive();e=u(this.data,this._transaction,this.options),r&&(r.log=e)}if(this.validate){var n,o,i=[],s=e.length;for(o=0;s>o;o++)if(n=a.getIn(this.validate,e[o]))try{this.typology.check(this.get(e[o]),n,!0)}catch(h){h.path=e[o].concat(h.path||[]),i.push(h)}i.length&&this.emit("invalid",{errors:i})}return this._transaction={},this._future&&(this._future=clearTimeout(this._future)),this.emit("update",{log:e}),this},n.prototype.select=function(t){if(!t)throw Error("Baobab.select: invalid path.");if(arguments.length>1&&(t=a.arrayOf(arguments)),!f.Path(t))throw Error("Baobab.select: invalid path.");t=f.Array(t)?t:[t];var e,n=f.ComplexPath(t);if(n&&(e=a.solvePath(this.data,t)),this.options.cursorSingletons){var i=t.map(function(t){return f.Function(t)?r("fn"):f.Object(t)?r("ob"):t}).join("λ");if(this._cursors[i])return this._cursors[i];var s=new o(this,t,e,i);return this._cursors[i]=s,s}return new o(this,t)},n.prototype.root=function(){return this.select([])},n.prototype.reference=function(t){if(arguments.length>1&&(t=a.arrayOf(arguments)),!f.Path(t))throw Error("Baobab.get: invalid path.");return a.getIn(this.data,f.String(t)||f.Number(t)?[t]:t)},n.prototype.get=function(){var t=this.reference.apply(this,arguments);return this.options.clone?this._cloner(t):t},n.prototype.clone=function(){return this._cloner(this.reference.apply(this,arguments))},n.prototype.set=function(t,e){if(arguments.length<2)throw Error("Baobab.set: expects a key and a value.");var r={};if(f.Array(t)){var n=a.solvePath(this.data,t);if(!n)throw Error("Baobab.set: could not solve dynamic path.");r=a.pathObject(n,{$set:e})}else r[t]={$set:e};return this.update(r)},n.prototype.unset=function(t){if(!t&&0!==t)throw Error("Baobab.unset: expects a valid key to unset.");var e={};return e[t]={$unset:!0},this.update(e)},n.prototype.update=function(t){var e=this;if(!f.Object(t))throw Error("Baobab.update: wrong specification.");return this._transaction=h(t,this._transaction),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout(e.commit.bind(e,null),0)),this):this.commit():this},n.prototype.hasHistory=function(){return!!this._history.length},n.prototype.getHistory=function(){return this._history},n.prototype.undo=function(){if(!this.hasHistory())throw Error("Baobab.undo: no history recorded, cannot undo.");var t=this._history.shift();this.commit(t)},n.prototype.release=function(){delete this.data,delete this._transaction,delete this._history;for(var t in this._cursors)this._cursors[t].release();delete this._cursors,this.kill()},n.prototype.toJSON=function(){return this.reference()},e.exports=n},{"../defaults.js":2,"./cursor.js":7,"./helpers.js":8,"./merge.js":9,"./mixins.js":10,"./type.js":11,"./update.js":12,emmett:3,typology:4}],6:[function(t,e){function r(t,e){e.on("update",t.cursorListener),t.tree.off("update",t.treeListener),t.tree.on("update",t.treeListener)}function n(t){var e=this;if(arguments.length<2)throw Error("baobab.Combination: not enough arguments.");var n=arguments[1],o=s.arrayOf(arguments).slice(2);if(n instanceof Array&&(o=n.slice(1),n=n[0]),!a.Cursor(n))throw Error("baobab.Combination: argument should be a cursor.");if("or"!==t&&"and"!==t)throw Error("baobab.Combination: invalid operator.");i.call(this),this.cursors=[n],this.operators=[],this.tree=n.tree,this.updates=new Array(this.cursors.length),this.cursorListener=function(){e.updates[e.cursors.indexOf(this)]=!0},this.treeListener=function(){var t,r,n=e.updates[0];for(t=1,r=e.cursors.length;r>t;t++)n="or"===e.operators[t-1]?n||e.updates[t]:n&&e.updates[t];n&&e.emit("update"),e.updates=new Array(e.cursors.length)},this.bound=!1;var u=this.on,h=this.once,c=function(){e.bound||(e.bound=!0,e.cursors.forEach(function(t){r(e,t)}))};this.on=function(){return c(),u.apply(this,arguments)},this.once=function(){return c(),h.apply(this,arguments)},o.forEach(function(e){this[t](e)},this)}function o(t){n.prototype[t]=function(e){if(!a.Cursor(e))throw this.release(),Error("baobab.Combination."+t+": argument should be a cursor.");if(~this.cursors.indexOf(e))throw this.release(),Error("baobab.Combination."+t+": cursor already in combination.");return this.cursors.push(e),this.operators.push(t),this.updates.length++,this.bound&&r(this,e),this}}var i=t("emmett"),s=t("./helpers.js"),a=t("./type.js");s.inherits(n,i),o("or"),o("and"),n.prototype.release=function(){this.cursors.forEach(function(t){t.off("update",this.cursorListener)},this),this.tree.off("update",this.treeListener),this.cursors=null,this.operators=null,this.tree=null,this.updates=null,this.kill()},e.exports=n},{"./helpers.js":8,"./type.js":11,emmett:3}],7:[function(t,e){function r(t,e,r,o){var a=this;n.call(this),e=e||[],this.tree=t,this.path=e,this.hash=o,this.relevant=void 0!==this.reference(),this.complexPath=!!r,this.solvedPath=this.complexPath?r:this.path,this.updateHandler=function(t){var e,r,n,o,i,u,h=t.data.log,c=!1;if(a.complexPath&&(a.solvedPath=s.solvePath(a.tree.data,a.path)),!a.path.length)return a.emit("update");t:for(i=0,n=h.length;n>i;i++)for(e=h[i],u=0,o=e.length;o>u&&(r=e[u],r===""+a.solvedPath[u]);u++)if(u+1===o||u+1===a.solvedPath.length){c=!0;break t}var l=void 0!==a.reference();a.relevant?l&&c?a.emit("update"):l||(a.emit("irrelevant"),a.relevant=!1):l&&c&&(a.emit("relevant"),a.emit("update"),a.relevant=!0)},this.mixin=i.cursor(this);var u=!1,h=this.on,c=this.once,l=function(){u||(u=!0,a.tree.on("update",a.updateHandler))};this.on=function(){return l(),h.apply(this,arguments)},this.once=function(){return l(),c.apply(this,arguments)}}var n=t("emmett"),o=t("./combination.js"),i=t("./mixins.js"),s=t("./helpers.js"),a=t("./type.js");s.inherits(r,n),r.prototype.isRoot=function(){return!this.path.length},r.prototype.isLeaf=function(){return a.Primitive(this.reference())},r.prototype.isBranch=function(){return!this.isLeaf()&&!this.isRoot()},r.prototype.root=function(){return this.tree.root()},r.prototype.select=function(t){if(arguments.length>1&&(t=s.arrayOf(arguments)),!a.Path(t))throw Error("baobab.Cursor.select: invalid path.");return this.tree.select(this.path.concat(t))},r.prototype.up=function(){return this.solvedPath&&this.solvedPath.length?this.tree.select(this.path.slice(0,-1)):null},r.prototype.left=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.left: cannot go left on a non-list type.");return t?this.tree.select(this.solvedPath.slice(0,-1).concat(t-1)):null},r.prototype.leftmost=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.leftmost: cannot go left on a non-list type.");return this.tree.select(this.solvedPath.slice(0,-1).concat(0))},r.prototype.right=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.right: cannot go right on a non-list type.");return t+1===this.up().reference().length?null:this.tree.select(this.solvedPath.slice(0,-1).concat(t+1))},r.prototype.rightmost=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.right: cannot go right on a non-list type.");var e=this.up().reference();return this.tree.select(this.solvedPath.slice(0,-1).concat(e.length-1))},r.prototype.down=function(){+this.solvedPath[this.solvedPath.length-1];return this.reference()instanceof Array?this.tree.select(this.solvedPath.concat(0)):null},r.prototype.get=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.get(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.reference=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.reference(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.clone=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.clone(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.set=function(t,e){if(arguments.length<2)throw Error("baobab.Cursor.set: expecting at least key/value.");var r=this.reference();if("object"!=typeof r)throw Error("baobab.Cursor.set: trying to set key to a non-object.");var n={};if(a.Array(t)){var o=s.solvePath(r,t);if(!o)throw Error("baobab.Cursor.set: could not solve dynamic path.");n=s.pathObject(o,{$set:e})}else n[t]={$set:e};return this.update(n)},r.prototype.edit=function(t){return this.update({$set:t})},r.prototype.unset=function(t){if(!t&&0!==t)throw Error("baobab.Cursor.unset: expects a valid key to unset.");if("object"!=typeof this.reference())throw Error("baobab.Cursor.set: trying to set key to a non-object.");var e={};return e[t]={$unset:!0},this.update(e)},r.prototype.remove=function(){if(this.isRoot())throw Error("baobab.Cursor.remove: cannot remove root node.");return this.update({$unset:!0})},r.prototype.apply=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.apply: argument is not a function.");return this.update({$apply:t})},r.prototype.chain=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.chain: argument is not a function.");return this.update({$chain:t})},r.prototype.push=function(t){if(!(this.reference()instanceof Array))throw Error("baobab.Cursor.push: trying to push to non-array value.");return this.update(arguments.length>1?{$push:s.arrayOf(arguments)}:{$push:t})},r.prototype.unshift=function(t){if(!(this.reference()instanceof Array))throw Error("baobab.Cursor.push: trying to push to non-array value.");return this.update(arguments.length>1?{$unshift:s.arrayOf(arguments)}:{$unshift:t})},r.prototype.merge=function(t){if(!a.Object(t))throw Error("baobab.Cursor.merge: trying to merge a non-object.");if(!a.Object(this.reference()))throw Error("baobab.Cursor.merge: trying to merge into a non-object.");this.update({$merge:t})},r.prototype.update=function(t){return this.tree.update(s.pathObject(this.solvedPath,t)),this},r.prototype.or=function(t){return new o("or",this,t)},r.prototype.and=function(t){return new o("and",this,t)},r.prototype.release=function(){this.tree.off("update",this.updateHandler),this.hash&&delete this.tree._cursors[this.hash],delete this.tree,delete this.path,delete this.solvedPath,this.kill()},r.prototype.toJSON=function(){return this.reference()},a.Cursor=function(t){return t instanceof r},e.exports=r},{"./combination.js":6,"./helpers.js":8,"./mixins.js":10,"./type.js":11,emmett:3}],8:[function(t,e){(function(r){function n(t){return Array.prototype.slice.call(t)}function o(t,e){var r,n={};for(r in t)n[r]=t[r];for(r in e)n[r]=e[r];return n}function i(t){var e=t.source,r="";return t.global&&(r+="g"),t.multiline&&(r+="m"),t.ignoreCase&&(r+="i"),t.sticky&&(r+="y"),t.unicode&&(r+="u"),new RegExp(e,r)}function s(t,e){if(!e||"object"!=typeof e||e instanceof Error||"ArrayBuffer"in r&&e instanceof ArrayBuffer)return e;if(b.Array(e)){if(t){var n,o,s=[];for(n=0,o=e.length;o>n;n++)s.push(v(e[n]));return s}return e.slice(0)}if(b.Date(e))return new Date(e.getTime());if(e instanceof RegExp)return i(e);if(b.Object(e)){var a,u={};e.constructor&&e.constructor!==Object&&(u=Object.create(e.constructor.prototype));for(a in e)e.hasOwnProperty(a)&&(u[a]=t?v(e[a]):e[a]);return u}return e}function a(t,e){return function(r){return e(t(r))}}function u(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return t[r]}function h(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return r;return-1}function c(t,e){var r,n=!0;if(!t)return!1;for(r in e)if(b.Object(e[r]))n=n&&c(t[r],e[r]);else if(b.Array(e[r]))n=n&&!!~e[r].indexOf(t[r]);else if(t[r]!==e[r])return!1;return n}function l(t,e){return u(t,function(t){return c(t,e)})}function f(t,e){return h(t,function(t){return c(t,e)})}function p(t,e){e=e||[];var r,n,o=t;for(r=0,n=e.length;n>r;r++){if(!o)return;if("function"==typeof e[r]){if(!b.Array(o))return;o=u(o,e[r])}else if("object"==typeof e[r]){if(!b.Array(o))return;o=l(o,e[r])}else o=o[e[r]]}return o}function d(t,e){var r,n,o,i=[],s=t;for(n=0,o=e.length;o>n;n++){if(!s)return null;if("function"==typeof e[n]){if(!b.Array(s))return;r=h(s,e[n]),i.push(r),s=s[r]}else if("object"==typeof e[n]){if(!b.Array(s))return;r=f(s,e[n]),i.push(r),s=s[r]}else i.push(e[n]),s=s[e[n]]||{}}return i}function y(t,e){var r,n=t.length,o={},i=o;for(n||(o=e),r=0;n>r;r++)i[t[r]]=r+1===n?e:{},i=i[t[r]];return o}function g(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}var b=t("./type.js"),m=s.bind(null,!1),v=s.bind(null,!0);e.exports={arrayOf:n,deepClone:v,shallowClone:m,shallowMerge:o,compose:a,getIn:p,inherits:g,pathObject:y,solvePath:d}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./type.js":11}],9:[function(t,e){function r(t,e){return e in(t||{})}function n(t,e,n){return r(t,n)&&r(e,n)}function o(){var t,e,r,a,u={},h=arguments.length;for(r=h-1;r>=0;r--){if(arguments[r].$unset)delete u.$set,delete u.$apply,delete u.$merge,u.$unset=arguments[r].$unset;else{if(arguments[r].$set){delete u.$apply,delete u.$merge,delete u.$unset,u.$set=arguments[r].$set;continue}if(arguments[r].$merge){delete u.$set,delete u.$apply,delete u.$unset,u.$merge=arguments[r].$merge;continue}if(arguments[r].$apply){delete u.$set,delete u.$merge,delete u.$unset,u.$apply=arguments[r].$apply;continue}if(arguments[r].$chain){delete u.$set,delete u.$merge,delete u.$unset,u.$apply=u.$apply?i.compose(u.$apply,arguments[r].$chain):arguments[r].$chain;continue}}for(a in arguments[r])t=u[a],e=arguments[r][a],t&&s.Object(e)?n(t,e,"$push")?t.$push=s.Array(t.$push)?t.$push.concat(e.$push):[t.$push].concat(e.$push):n(t,e,"$unshift")?t.$unshift=s.Array(e.$unshift)?e.$unshift.concat(t.$unshift):[e.$unshift].concat(t.$unshift):u[a]=o(e,t):u[a]=e}return u}var i=t("./helpers.js"),s=t("./type.js");e.exports=o},{"./helpers.js":8,"./type.js":11}],10:[function(t,e){var r=t("./combination.js"),n=t("./type.js");e.exports={baobab:function(t){return{mixins:[{getInitialState:function(){if(this.tree=t,!this.cursor&&!this.cursors)return{};if(this.cursor&&this.cursors)throw Error("baobab.mixin: you cannot have both `component.cursor` and `component.cursors`. Please make up your mind.");if(this.__type=null,this.__updateHandler=function(){this.setState(this.__getCursorData())}.bind(this),this.cursor){if(!n.MixinCursor(this.cursor))throw Error("baobab.mixin.cursor: invalid data (cursor, string, array or function).");n.Function(this.cursor)&&(this.cursor=this.cursor()),n.Cursor(this.cursor)||(this.cursor=t.select(this.cursor)),this.__getCursorData=function(){return{cursor:this.cursor.get()}}.bind(this),this.__type="single"}else if(this.cursors){if(!n.MixinCursors(this.cursors))throw Error("baobab.mixin.cursor: invalid data (object, array or function).");if(n.Function(this.cursors)&&(this.cursors=this.cursors()),n.Array(this.cursors))this.cursors=this.cursors.map(function(e){return n.Cursor(e)?e:t.select(e)}),this.__getCursorData=function(){return{cursors:this.cursors.map(function(t){return t.get()})}}.bind(this),this.__type="array";else{for(var e in this.cursors)n.Cursor(this.cursors[e])||(this.cursors[e]=t.select(this.cursors[e]));this.__getCursorData=function(){var t={};for(e in this.cursors)t[e]=this.cursors[e].get();return{cursors:t}}.bind(this),this.__type="object"}}return this.__getCursorData()},componentDidMount:function(){"single"===this.__type?(this.__combination=new r("or",[this.cursor]),this.__combination.on("update",this.__updateHandler)):"array"===this.__type?(this.__combination=new r("or",this.cursors),this.__combination.on("update",this.__updateHandler)):"object"===this.__type&&(this.__combination=new r("or",Object.keys(this.cursors).map(function(t){return this.cursors[t]},this)),this.__combination.on("update",this.__updateHandler))},componentWillUnmount:function(){this.__combination&&this.__combination.release()}}].concat(t.options.mixins)}},cursor:function(t){return{mixins:[{getInitialState:function(){return this.cursor=t,this.__updateHandler=function(){this.setState({cursor:this.cursor.get()})}.bind(this),{cursor:this.cursor.get()}},componentDidMount:function(){this.cursor.on("update",this.__updateHandler)},componentWillUnmount:function(){this.cursor.off("update",this.__updateHandler)}}].concat(t.tree.options.mixins)}}}},{"./combination.js":6,"./type.js":11}],11:[function(t,e){var r=function(t){return Array.isArray(t)?"array":"object"==typeof t&&null!==t?"object":"string"==typeof t?"string":"number"==typeof t?"number":"boolean"==typeof t?"boolean":"function"==typeof t?"function":null===t?"null":void 0===t?"undefined":t instanceof Date?"date":"invalid"};r.Array=function(t){return Array.isArray(t)},r.Object=function(t){return!Array.isArray(t)&&"object"==typeof t&&null!==t},r.String=function(t){return"string"==typeof t},r.Number=function(t){return"number"==typeof t},r.Boolean=function(t){return"boolean"==typeof t},r.Function=function(t){return"function"==typeof t},r.Primitive=function(t){return"string"==typeof t||"number"==typeof t||"boolean"==typeof t},r.Date=function(t){return t instanceof Date},r.Step=function(t){var e=r(t),n=["null","undefined","invalid","date"];return-1===n.indexOf(e)},r.Path=function(t){var e=["object","string","number","function","undefined"];if(!r.Array(t))return e.indexOf(r(t))>=0;for(var n=0;n=0||r.Cursor(t)},r.MixinCursors=function(t){var e=["array","object","function"];return e.indexOf(r(t))>=0},r.ComplexPath=function(t){for(var e=["object","function"],n=0;n=0)return!0;return!1},e.exports=r},{}],12:[function(t,e){function r(t,e){var r=new Error("baobab.update: "+e+" at path /"+t.toString());return r.path=t,r}function n(t,e,n){n=n||{shiftReferences:!1};var a={};return function u(t,e,h,c){h=h||[];var l,f,p,d,y=h.join("λ");for(p in e)if(s[p])switch(d=e[p],a[y]=!0,p){case"$push":if(!i.Array(t))throw r(h,"using command $push to a non array");i.Array(d)?t.push.apply(t,d):t.push(d);break;case"$unshift":if(!i.Array(t))throw r(h,"using command $unshift to a non array");i.Array(d)?t.unshift.apply(t,d):t.unshift(d)}else if(f=y?y+"λ"+p:p,"$unset"in(e[p]||{}))a[f]=!0,i.Array(t)?n.shiftReferences?c[h[h.length-1]]=t.slice(0,+p).concat(t.slice(+p+1)):t.splice(p,1):delete t[p];else if("$set"in(e[p]||{}))d=e[p].$set,a[f]=!0,t[p]=d;else if("$apply"in(e[p]||{})||"$chain"in(e[p]||{})){if(l=e[p].$apply||e[p].$chain,"function"!=typeof l)throw r(h.concat(p),"using command $apply with a non function");a[f]=!0,t[p]=l.call(null,t[p])}else if("$merge"in(e[p]||{})){if(d=e[p].$merge,!i.Object(t[p]))throw r(h.concat(p),"using command $merge on a non-object");a[f]=!0,t[p]=o.shallowMerge(t[p],d)}else if(n.shiftReferences&&("$push"in(e[p]||{})||"$unshift"in(e[p]||{}))){if("$push"in(e[p]||{})){if(d=e[p].$push,!i.Array(t[p]))throw r(h.concat(p),"using command $push to a non array");t[p]=t[p].concat(d)}if("$unshift"in(e[p]||{})){if(d=e[p].$unshift,!i.Array(t[p]))throw r(h.concat(p),"using command $unshift to a non array");t[p]=(d instanceof Array?d:[d]).concat(t[p])}a[f]=!0}else"undefined"==typeof t[p]&&(t[p]={}),n.shiftReferences&&(t[p]=o.shallowClone(t[p])),u(t[p],e[p],h.concat(p),t)}(t,e),Object.keys(a).map(function(t){return t.split("λ")})}var o=t("./helpers.js"),i=t("./type.js"),s={};["$set","$push","$unshift","$apply","$merge"].forEach(function(t){s[t]=!0}),e.exports=n},{"./helpers.js":8,"./type.js":11}]},{},[1])(1)}); \ No newline at end of file +/* baobab.js - Version: 0.4.4 - Author: Yomguithereal (Guillaume Plique) */ +!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.Baobab=t()}}(function(){var t;return function e(t,r,n){function o(s,a){if(!r[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(i)return i(s,!0);var h=new Error("Cannot find module '"+s+"'");throw h.code="MODULE_NOT_FOUND",h}var c=r[s]={exports:{}};t[s][0].call(c.exports,function(e){var r=t[s][1][e];return o(r?r:e)},c,c.exports,e,t,r,n)}return r[s].exports}for(var i="function"==typeof require&&require,s=0;se;e++)n.push(t[e].handler);return n}var o={once:"boolean",scope:"object"},i=function(){this._enabled=!0,this._children=[],this._handlers={},this._handlersAll=[]};i.prototype.on=function(t,e,r){var n,s,a,u,h,c;if("function"==typeof e){for(h="string"==typeof t?[t]:t,n=0,s=h.length;n!==s;n+=1)if(u=h[n]){this._handlers[u]||(this._handlers[u]=[]),c={handler:e};for(a in r||{}){if(!o[a])throw new Error('The option "'+a+'" is not recognized by Emmett.');c[a]=r[a]}this._handlers[u].push(c)}}else if(t&&"object"==typeof t&&!Array.isArray(t))for(u in t)i.prototype.on.call(this,u,t[u],e);else{if("function"!=typeof t)throw new Error("Wrong arguments.");c={handler:t};for(a in r||{}){if(!o[a])throw new Error('The option "'+a+'" is not recognized by Emmett.');c[a]=r[a]}this._handlersAll.push(c)}return this},i.prototype.once=function(t,e,r){if("function"==typeof e)r=r||{},r.once=!0,this.on(t,e,r);else{if((!t||"object"!=typeof t||Array.isArray(t))&&"function"!=typeof t)throw new Error("Wrong arguments.");e=e||{},e.once=!0,this.on(t,e)}return this},i.prototype.off=function(t,e){var r,n,o,i,s,a,u,h="string"==typeof t?[t]:t;if(1===arguments.length&&"function"==typeof h){e=arguments[0];for(s in this._handlers){for(a=[],r=0,n=this._handlers[s].length;r!==n;r+=1)this._handlers[s][r].handler!==e&&a.push(this._handlers[s][r]);this._handlers[s]=a}for(a=[],r=0,n=this._handlersAll.length;r!==n;r+=1)this._handlersAll[r].handler!==e&&a.push(this._handlersAll[r]);this._handlersAll=a}else if(2===arguments.length)for(r=0,n=h.length;r!==n;r+=1){if(u=h[r],this._handlers[u]){for(a=[],o=0,i=this._handlers[u].length;o!==i;o+=1)this._handlers[u][o].handler!==e&&a.push(this._handlers[u][o]);this._handlers[u]=a}this._handlers[u]&&0===this._handlers[u].length&&delete this._handlers[u]}return this},i.prototype.unbindAll=function(){var t;this._handlersAll=[];for(t in this._handlers)delete this._handlers[t];return this},i.prototype.emit=function(t,e){var r,n,o,i,s,a,u,h,c,l,f="string"==typeof t?[t]:t;if(!this._enabled)return this;for(e=void 0===e?{}:e,r=0,n=f.length;r!==n;r+=1)if(l=f[r],c=(this._handlers[l]||[]).concat(this._handlersAll),c.length){for(u={type:l,data:e||{},target:this},a=[],o=0,i=c.length;o!==i;o+=1)(this._handlers[l]&&this._handlers[l].indexOf(c[o])>=0||this._handlersAll.indexOf(c[o])>=0)&&(c[o].handler.call("scope"in c[o]?c[o].scope:this,u),c[o].once&&a.push(c[o]));for(s=0;sr;r++)if(t._children[r]===e){t._children.splice(r,1);break}}),this._children.push(e),e},i.prototype.listeners=function(t){var r,n,o,i=[];if(t)for(i=e(this._handlers[t]),n=0,o=this._children.length;o>n;n++)i=i.concat(this._children[n].listeners(t));else{i=e(this._handlersAll);for(r in this._handlers)i=i.concat(e(this._handlers[r]));for(n=0,o=this._children.length;o>n;n++)i=i.concat(this._children[n].listeners())}return i},i.prototype.kill=function(){if(this.emit("emmett:kill"),this.unbindAll(),this._handlers=null,this._handlersAll=null,this._enabled=!1,this._children)for(var t=0,e=this._children.length;e>t;t++)this._children[t].kill();this._children=null},i.prototype.disable=function(){return this._enabled=!1,this},i.prototype.enable=function(){return this._enabled=!0,this},i.version="2.1.2","undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=i),n.Emitter=i):"function"==typeof t&&t.amd?t("emmett",[],function(){return i}):this.Emitter=i}).call(this)},{}],4:[function(e,r,n){!function(){"use strict";function e(t){function e(t,o){var s,a,u,h,c,l,f,p,d=!1,y=!1,g=r.get(t);if("string"===r.get(o)){for(s=o.replace(/^[\?\!]/,"").split(/\|/),u=s.length,a=0;u>a;a++)if(i.indexOf(s[a])<0&&!(s[a]in n))throw new Error("Invalid type.");if(o.match(/^\?/)&&(d=!0),o.replace(/^\?/,"").match(/^\!/)&&(y=!0),y&&d)throw new Error("Invalid type.");for(a in s)if(n[s[a]]&&("function"==typeof n[s[a]].type?n[s[a]].type.call(r,t)===!0:!e(t,n[s[a]].type)))return y?(c=new Error,c.message='Expected a "'+o+'" but found a "'+s[a]+'".',c.expected=o,c.type=s[a],c.value=t,c):null;return null===t||void 0===t?y||d?null:(c=new Error,c.message='Expected a "'+o+'" but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c):(f=~s.indexOf("*"),p=~s.indexOf(g),y&&(f||p)?(c=new Error,c.message='Expected a "'+o+'" but found a "'+(p?g:"*")+'".',c.type=p?g:"*",c.expected=o,c.value=t,c):y||f||p?null:(c=new Error,c.message='Expected a "'+o+'" but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c))}if("object"===r.get(o)){if("object"!==g)return c=new Error,c.message='Expected an object but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c;for(h in o)if(l=e(t[h],o[h]))return c=l,c.path=c.path?[h].concat(c.path):[h],c;for(h in t)if(void 0===o[h])return c=new Error,c.message='Unexpected key "'+h+'".',c.type=g,c.value=t,c;return null}if("array"===r.get(o)){if(1!==o.length)throw new Error("Invalid type.");if("array"!==g)return c=new Error,c.message='Expected an array but found a "'+g+'".',c.expected=o,c.type=g,c.value=t,c;for(u=t.length,a=0;u>a;a++)if(l=e(t[a],o[0]))return c=l,c.path=c.path?[a].concat(c.path):[a],c;return null}throw new Error("Invalid type.")}var r=this,n={};if(this.add=function(t,e){var r,o,s,a,u,h;if(1===arguments.length){if("object"!==this.get(t))throw new Error("If types.add is called with one argument, this one has to be an object.");r=t,a=r.id,h=r.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or two arguments.");if("string"!=typeof t||!t)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");a=t,h=e}if("string"!==this.get(a)||0===a.length)throw new Error("A type requires an string id.");if(void 0!==n[a]&&"proto"!==n[a])throw new Error('The type "'+a+'" already exists.');if(~i.indexOf(a))throw new Error('"'+a+'" is a reserved type name.');n[a]=1,s=(r||{}).proto||[],s=Array.isArray(s)?s:[s],u={};for(o in s)void 0===n[s[o]]&&(n[s[o]]=1,u[s[o]]=1);if("function"!==this.get(h)&&!this.isValid(h))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(n[a]=void 0===r?{id:a,type:h}:{},void 0!==r)for(o in r)n[a][o]=r[o];for(o in u)o!==a&&delete n[o];return this},this.has=function(t){return!!n[t]},this.get=function(t){return null===t||void 0===t?String(t):o[Object.prototype.toString.call(t)]||"object"},this.check=function(t,r,n){var o=e(t,r);if(n&&o)throw o;return!o},this.isValid=function(t){var e,r,o;if("string"===this.get(t)){e=t.replace(/^[\?\!]/,"").split(/\|/);for(o in e)if(i.indexOf(e[o])<0&&!(e[o]in n))return!1;return!0}if("object"===this.get(t)){for(r in t)if(!this.isValid(t[r]))return!1;return!0}return"array"===this.get(t)&&1===t.length?this.isValid(t[0]):!1},this.add("type",function(t){return this.isValid(t)}.bind(this)),this.add("primitive",function(t){return!t||!(t instanceof Object||"object"==typeof t)}),t=t||{},"object"!==this.get(t))throw Error("Invalid argument.");for(var s in t)this.add(s,t[s])}var o={},i=["*"];!function(){var t,e,r=["Arguments","Boolean","Number","String","Function","Array","Date","RegExp","Object"];for(t in r)e=r[t],i.push(e.toLowerCase()),o["[object "+e+"]"]=e.toLowerCase()}();var s=e;e.call(s),Object.defineProperty(s,"version",{value:"0.3.1"}),"undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=s),n.types=s):"function"==typeof t&&t.amd?t("typology",[],function(){return s}):this.types=s}(this)},{}],5:[function(t,e){function r(t){return t+"$"+(new Date).getTime()+(""+Math.random()).replace("0.","")}function n(t,e){if(arguments.length<1&&(t={}),!(this instanceof n))return new n(t,e);if(!f.Object(t)&&!f.Array(t))throw Error("Baobab: invalid data.");if(i.call(this),this.options=a.shallowMerge(l,e),this._cloner=this.options.cloningFunction||a.deepClone,this._transaction={},this._future=void 0,this._history=[],this._cursors={},this.typology=this.options.typology?this.options.typology instanceof s?this.options.typology:new s(this.options.typology):new s,this.validate=this.options.validate||null,this.validate)try{this.typology.check(t,this.validate,!0)}catch(r){throw r.message="/"+r.path.join("/")+": "+r.message,r}this.data=this._cloner(t),this.mixin=c.baobab(this)}var o=t("./cursor.js"),i=t("emmett"),s=t("typology"),a=t("./helpers.js"),u=t("./update.js"),h=t("./merge.js"),c=t("./mixins.js"),l=t("../defaults.js"),f=t("./type.js");a.inherits(n,i),n.prototype._archive=function(){if(!(this.options.maxHistory<=0)){var t={data:this._cloner(this.data)};return this._history.length===this.options.maxHistory&&this._history.pop(),this._history.unshift(t),t}},n.prototype.commit=function(t){var e;if(t)this.data=t.data,e=t.log;else{this.options.shiftReferences&&(this.data=a.shallowClone(this.data));var r=this._archive();e=u(this.data,this._transaction,this.options),r&&(r.log=e)}if(this.validate){var n,o,i=[],s=e.length;for(o=0;s>o;o++)if(n=a.getIn(this.validate,e[o]))try{this.typology.check(this.get(e[o]),n,!0)}catch(h){h.path=e[o].concat(h.path||[]),i.push(h)}i.length&&this.emit("invalid",{errors:i})}return this._transaction={},this._future&&(this._future=clearTimeout(this._future)),this.emit("update",{log:e}),this},n.prototype.select=function(t){if(!t)throw Error("Baobab.select: invalid path.");if(arguments.length>1&&(t=a.arrayOf(arguments)),!f.Path(t))throw Error("Baobab.select: invalid path.");t=f.Array(t)?t:[t];var e,n=f.ComplexPath(t);if(n&&(e=a.solvePath(this.data,t)),this.options.cursorSingletons){var i=t.map(function(t){return f.Function(t)?r("fn"):f.Object(t)?r("ob"):t}).join("λ");if(this._cursors[i])return this._cursors[i];var s=new o(this,t,e,i);return this._cursors[i]=s,s}return new o(this,t)},n.prototype.root=function(){return this.select([])},n.prototype.reference=function(t){if(arguments.length>1&&(t=a.arrayOf(arguments)),!f.Path(t))throw Error("Baobab.get: invalid path.");return a.getIn(this.data,f.String(t)||f.Number(t)?[t]:t)},n.prototype.get=function(){var t=this.reference.apply(this,arguments);return this.options.clone?this._cloner(t):t},n.prototype.clone=function(){return this._cloner(this.reference.apply(this,arguments))},n.prototype.set=function(t,e){if(arguments.length<2)throw Error("Baobab.set: expects a key and a value.");var r={};if(f.Array(t)){var n=a.solvePath(this.data,t);if(!n)throw Error("Baobab.set: could not solve dynamic path.");r=a.pathObject(n,{$set:e})}else r[t]={$set:e};return this.update(r)},n.prototype.unset=function(t){if(!t&&0!==t)throw Error("Baobab.unset: expects a valid key to unset.");var e={};return e[t]={$unset:!0},this.update(e)},n.prototype.update=function(t){var e=this;if(!f.Object(t))throw Error("Baobab.update: wrong specification.");return this._transaction=h(t,this._transaction),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout(e.commit.bind(e,null),0)),this):this.commit():this},n.prototype.hasHistory=function(){return!!this._history.length},n.prototype.getHistory=function(){return this._history},n.prototype.undo=function(){if(!this.hasHistory())throw Error("Baobab.undo: no history recorded, cannot undo.");var t=this._history.shift();this.commit(t)},n.prototype.release=function(){delete this.data,delete this._transaction,delete this._history;for(var t in this._cursors)this._cursors[t].release();delete this._cursors,this.kill()},n.prototype.toJSON=function(){return this.reference()},e.exports=n},{"../defaults.js":2,"./cursor.js":7,"./helpers.js":8,"./merge.js":9,"./mixins.js":10,"./type.js":11,"./update.js":12,emmett:3,typology:4}],6:[function(t,e){function r(t,e){e.on("update",t.cursorListener),t.tree.off("update",t.treeListener),t.tree.on("update",t.treeListener)}function n(t){var e=this;if(arguments.length<2)throw Error("baobab.Combination: not enough arguments.");var n=arguments[1],o=s.arrayOf(arguments).slice(2);if(n instanceof Array&&(o=n.slice(1),n=n[0]),!a.Cursor(n))throw Error("baobab.Combination: argument should be a cursor.");if("or"!==t&&"and"!==t)throw Error("baobab.Combination: invalid operator.");i.call(this),this.cursors=[n],this.operators=[],this.tree=n.tree,this.updates=new Array(this.cursors.length),this.cursorListener=function(){e.updates[e.cursors.indexOf(this)]=!0},this.treeListener=function(){var t,r,n=e.updates[0];for(t=1,r=e.cursors.length;r>t;t++)n="or"===e.operators[t-1]?n||e.updates[t]:n&&e.updates[t];n&&e.emit("update"),e.updates=new Array(e.cursors.length)},this.bound=!1;var u=this.on,h=this.once,c=function(){e.bound||(e.bound=!0,e.cursors.forEach(function(t){r(e,t)}))};this.on=function(){return c(),u.apply(this,arguments)},this.once=function(){return c(),h.apply(this,arguments)},o.forEach(function(e){this[t](e)},this)}function o(t){n.prototype[t]=function(e){if(!a.Cursor(e))throw this.release(),Error("baobab.Combination."+t+": argument should be a cursor.");if(~this.cursors.indexOf(e))throw this.release(),Error("baobab.Combination."+t+": cursor already in combination.");return this.cursors.push(e),this.operators.push(t),this.updates.length++,this.bound&&r(this,e),this}}var i=t("emmett"),s=t("./helpers.js"),a=t("./type.js");s.inherits(n,i),o("or"),o("and"),n.prototype.release=function(){this.cursors.forEach(function(t){t.off("update",this.cursorListener)},this),this.tree.off("update",this.treeListener),this.cursors=null,this.operators=null,this.tree=null,this.updates=null,this.kill()},e.exports=n},{"./helpers.js":8,"./type.js":11,emmett:3}],7:[function(t,e){function r(t,e,r,o){var a=this;n.call(this),e=e||[],this.tree=t,this.path=e,this.hash=o,this.relevant=void 0!==this.reference(),this.complexPath=!!r,this.solvedPath=this.complexPath?r:this.path,this.updateHandler=function(t){var e,r,n,o,i,u,h=t.data.log,c=!1;if(a.complexPath&&(a.solvedPath=s.solvePath(a.tree.data,a.path)),!a.path.length)return a.emit("update");t:for(i=0,n=h.length;n>i;i++)for(e=h[i],u=0,o=e.length;o>u&&(r=e[u],r===""+a.solvedPath[u]);u++)if(u+1===o||u+1===a.solvedPath.length){c=!0;break t}var l=void 0!==a.reference();a.relevant?l&&c?a.emit("update"):l||(a.emit("irrelevant"),a.relevant=!1):l&&c&&(a.emit("relevant"),a.emit("update"),a.relevant=!0)},this.mixin=i.cursor(this);var u=!1,h=this.on,c=this.once,l=function(){u||(u=!0,a.tree.on("update",a.updateHandler))};this.on=function(){return l(),h.apply(this,arguments)},this.once=function(){return l(),c.apply(this,arguments)}}var n=t("emmett"),o=t("./combination.js"),i=t("./mixins.js"),s=t("./helpers.js"),a=t("./type.js");s.inherits(r,n),r.prototype.isRoot=function(){return!this.path.length},r.prototype.isLeaf=function(){return a.Primitive(this.reference())},r.prototype.isBranch=function(){return!this.isLeaf()&&!this.isRoot()},r.prototype.root=function(){return this.tree.root()},r.prototype.select=function(t){if(arguments.length>1&&(t=s.arrayOf(arguments)),!a.Path(t))throw Error("baobab.Cursor.select: invalid path.");return this.tree.select(this.path.concat(t))},r.prototype.up=function(){return this.solvedPath&&this.solvedPath.length?this.tree.select(this.path.slice(0,-1)):null},r.prototype.left=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.left: cannot go left on a non-list type.");return t?this.tree.select(this.solvedPath.slice(0,-1).concat(t-1)):null},r.prototype.leftmost=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.leftmost: cannot go left on a non-list type.");return this.tree.select(this.solvedPath.slice(0,-1).concat(0))},r.prototype.right=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.right: cannot go right on a non-list type.");return t+1===this.up().reference().length?null:this.tree.select(this.solvedPath.slice(0,-1).concat(t+1))},r.prototype.rightmost=function(){var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("baobab.Cursor.right: cannot go right on a non-list type.");var e=this.up().reference();return this.tree.select(this.solvedPath.slice(0,-1).concat(e.length-1))},r.prototype.down=function(){+this.solvedPath[this.solvedPath.length-1];return this.reference()instanceof Array?this.tree.select(this.solvedPath.concat(0)):null},r.prototype.get=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.get(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.reference=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.reference(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.clone=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.tree.clone(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.set=function(t,e){if(arguments.length<2)throw Error("baobab.Cursor.set: expecting at least key/value.");var r=this.reference();if("object"!=typeof r)throw Error("baobab.Cursor.set: trying to set key to a non-object.");var n={};if(a.Array(t)){var o=s.solvePath(r,t);if(!o)throw Error("baobab.Cursor.set: could not solve dynamic path.");n=s.pathObject(o,{$set:e})}else n[t]={$set:e};return this.update(n)},r.prototype.edit=function(t){return this.update({$set:t})},r.prototype.unset=function(t){if(!t&&0!==t)throw Error("baobab.Cursor.unset: expects a valid key to unset.");if("object"!=typeof this.reference())throw Error("baobab.Cursor.set: trying to set key to a non-object.");var e={};return e[t]={$unset:!0},this.update(e)},r.prototype.remove=function(){if(this.isRoot())throw Error("baobab.Cursor.remove: cannot remove root node.");return this.update({$unset:!0})},r.prototype.apply=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.apply: argument is not a function.");return this.update({$apply:t})},r.prototype.chain=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.chain: argument is not a function.");return this.update({$chain:t})},r.prototype.push=function(t){if(!(this.reference()instanceof Array))throw Error("baobab.Cursor.push: trying to push to non-array value.");return this.update(arguments.length>1?{$push:s.arrayOf(arguments)}:{$push:t})},r.prototype.unshift=function(t){if(!(this.reference()instanceof Array))throw Error("baobab.Cursor.push: trying to push to non-array value.");return this.update(arguments.length>1?{$unshift:s.arrayOf(arguments)}:{$unshift:t})},r.prototype.merge=function(t){if(!a.Object(t))throw Error("baobab.Cursor.merge: trying to merge a non-object.");if(!a.Object(this.reference()))throw Error("baobab.Cursor.merge: trying to merge into a non-object.");this.update({$merge:t})},r.prototype.update=function(t){return this.tree.update(s.pathObject(this.solvedPath,t)),this},r.prototype.or=function(t){return new o("or",this,t)},r.prototype.and=function(t){return new o("and",this,t)},r.prototype.release=function(){this.tree.off("update",this.updateHandler),this.hash&&delete this.tree._cursors[this.hash],delete this.tree,delete this.path,delete this.solvedPath,this.kill()},r.prototype.toJSON=function(){return this.reference()},a.Cursor=function(t){return t instanceof r},e.exports=r},{"./combination.js":6,"./helpers.js":8,"./mixins.js":10,"./type.js":11,emmett:3}],8:[function(t,e){(function(r){function n(t){return Array.prototype.slice.call(t)}function o(t,e){var r,n={};for(r in t)n[r]=t[r];for(r in e)n[r]=e[r];return n}function i(t){var e=t.source,r="";return t.global&&(r+="g"),t.multiline&&(r+="m"),t.ignoreCase&&(r+="i"),t.sticky&&(r+="y"),t.unicode&&(r+="u"),new RegExp(e,r)}function s(t,e){if(!e||"object"!=typeof e||e instanceof Error||"ArrayBuffer"in r&&e instanceof ArrayBuffer)return e;if(b.Array(e)){if(t){var n,o,s=[];for(n=0,o=e.length;o>n;n++)s.push(v(e[n]));return s}return e.slice(0)}if(b.Date(e))return new Date(e.getTime());if(e instanceof RegExp)return i(e);if(b.Object(e)){var a,u={};e.constructor&&e.constructor!==Object&&(u=Object.create(e.constructor.prototype));for(a in e)e.hasOwnProperty(a)&&(u[a]=t?v(e[a]):e[a]);return u}return e}function a(t,e){return function(r){return e(t(r))}}function u(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return t[r]}function h(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return r;return-1}function c(t,e){var r,n=!0;if(!t)return!1;for(r in e)if(b.Object(e[r]))n=n&&c(t[r],e[r]);else if(b.Array(e[r]))n=n&&!!~e[r].indexOf(t[r]);else if(t[r]!==e[r])return!1;return n}function l(t,e){return u(t,function(t){return c(t,e)})}function f(t,e){return h(t,function(t){return c(t,e)})}function p(t,e){e=e||[];var r,n,o=t;for(r=0,n=e.length;n>r;r++){if(!o)return;if("function"==typeof e[r]){if(!b.Array(o))return;o=u(o,e[r])}else if("object"==typeof e[r]){if(!b.Array(o))return;o=l(o,e[r])}else o=o[e[r]]}return o}function d(t,e){var r,n,o,i=[],s=t;for(n=0,o=e.length;o>n;n++){if(!s)return null;if("function"==typeof e[n]){if(!b.Array(s))return;r=h(s,e[n]),i.push(r),s=s[r]}else if("object"==typeof e[n]){if(!b.Array(s))return;r=f(s,e[n]),i.push(r),s=s[r]}else i.push(e[n]),s=s[e[n]]||{}}return i}function y(t,e){var r,n=t.length,o={},i=o;for(n||(o=e),r=0;n>r;r++)i[t[r]]=r+1===n?e:{},i=i[t[r]];return o}function g(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}var b=t("./type.js"),m=s.bind(null,!1),v=s.bind(null,!0);e.exports={arrayOf:n,deepClone:v,shallowClone:m,shallowMerge:o,compose:a,getIn:p,inherits:g,pathObject:y,solvePath:d}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./type.js":11}],9:[function(t,e){function r(t,e){return e in(t||{})}function n(t,e,n){return r(t,n)&&r(e,n)}function o(){var t,e,r,a,u={},h=arguments.length;for(r=h-1;r>=0;r--){if(arguments[r].$unset)delete u.$set,delete u.$apply,delete u.$merge,u.$unset=arguments[r].$unset;else{if(arguments[r].$set){delete u.$apply,delete u.$merge,delete u.$unset,u.$set=arguments[r].$set;continue}if(arguments[r].$merge){delete u.$set,delete u.$apply,delete u.$unset,u.$merge=arguments[r].$merge;continue}if(arguments[r].$apply){delete u.$set,delete u.$merge,delete u.$unset,u.$apply=arguments[r].$apply;continue}if(arguments[r].$chain){delete u.$set,delete u.$merge,delete u.$unset,u.$apply=u.$apply?i.compose(u.$apply,arguments[r].$chain):arguments[r].$chain;continue}}for(a in arguments[r])t=u[a],e=arguments[r][a],t&&s.Object(e)?n(t,e,"$push")?t.$push=s.Array(t.$push)?t.$push.concat(e.$push):[t.$push].concat(e.$push):n(t,e,"$unshift")?t.$unshift=s.Array(e.$unshift)?e.$unshift.concat(t.$unshift):[e.$unshift].concat(t.$unshift):u[a]=o(e,t):u[a]=e}return u}var i=t("./helpers.js"),s=t("./type.js");e.exports=o},{"./helpers.js":8,"./type.js":11}],10:[function(t,e){var r=t("./combination.js"),n=t("./type.js");e.exports={baobab:function(t){return{mixins:[{getInitialState:function(){if(this.tree=t,!this.cursor&&!this.cursors)return{};if(this.cursor&&this.cursors)throw Error("baobab.mixin: you cannot have both `component.cursor` and `component.cursors`. Please make up your mind.");if(this.__type=null,this.__updateHandler=function(){this.setState(this.__getCursorData())}.bind(this),this.cursor){if(!n.MixinCursor(this.cursor))throw Error("baobab.mixin.cursor: invalid data (cursor, string, array or function).");n.Function(this.cursor)&&(this.cursor=this.cursor()),n.Cursor(this.cursor)||(this.cursor=t.select(this.cursor)),this.__getCursorData=function(){return{cursor:this.cursor.get()}}.bind(this),this.__type="single"}else if(this.cursors){if(!n.MixinCursors(this.cursors))throw Error("baobab.mixin.cursor: invalid data (object, array or function).");if(n.Function(this.cursors)&&(this.cursors=this.cursors()),n.Array(this.cursors))this.cursors=this.cursors.map(function(e){return n.Cursor(e)?e:t.select(e)}),this.__getCursorData=function(){return{cursors:this.cursors.map(function(t){return t.get()})}}.bind(this),this.__type="array";else{for(var e in this.cursors)n.Cursor(this.cursors[e])||(this.cursors[e]=t.select(this.cursors[e]));this.__getCursorData=function(){var t={};for(e in this.cursors)t[e]=this.cursors[e].get();return{cursors:t}}.bind(this),this.__type="object"}}return this.__getCursorData()},componentDidMount:function(){"single"===this.__type?(this.__combination=new r("or",[this.cursor]),this.__combination.on("update",this.__updateHandler)):"array"===this.__type?(this.__combination=new r("or",this.cursors),this.__combination.on("update",this.__updateHandler)):"object"===this.__type&&(this.__combination=new r("or",Object.keys(this.cursors).map(function(t){return this.cursors[t]},this)),this.__combination.on("update",this.__updateHandler))},componentWillUnmount:function(){this.__combination&&this.__combination.release()}}].concat(t.options.mixins)}},cursor:function(t){return{mixins:[{getInitialState:function(){return this.cursor=t,this.__updateHandler=function(){this.setState({cursor:this.cursor.get()})}.bind(this),{cursor:this.cursor.get()}},componentDidMount:function(){this.cursor.on("update",this.__updateHandler)},componentWillUnmount:function(){this.cursor.off("update",this.__updateHandler)}}].concat(t.tree.options.mixins)}}}},{"./combination.js":6,"./type.js":11}],11:[function(t,e){var r=function(t){return Array.isArray(t)?"array":"object"==typeof t&&null!==t?"object":"string"==typeof t?"string":"number"==typeof t?"number":"boolean"==typeof t?"boolean":"function"==typeof t?"function":null===t?"null":void 0===t?"undefined":t instanceof Date?"date":"invalid"};r.Array=function(t){return Array.isArray(t)},r.Object=function(t){return!Array.isArray(t)&&"object"==typeof t&&null!==t},r.String=function(t){return"string"==typeof t},r.Number=function(t){return"number"==typeof t},r.Boolean=function(t){return"boolean"==typeof t},r.Function=function(t){return"function"==typeof t},r.Primitive=function(t){return"string"==typeof t||"number"==typeof t||"boolean"==typeof t},r.Date=function(t){return t instanceof Date},r.Step=function(t){var e=r(t),n=["null","undefined","invalid","date"];return-1===n.indexOf(e)},r.Path=function(t){var e=["object","string","number","function","undefined"];if(!r.Array(t))return e.indexOf(r(t))>=0;for(var n=0;n=0||r.Cursor(t)},r.MixinCursors=function(t){var e=["array","object","function"];return e.indexOf(r(t))>=0},r.ComplexPath=function(t){for(var e=["object","function"],n=0;n=0)return!0;return!1},e.exports=r},{}],12:[function(t,e){function r(t,e){var r=new Error("baobab.update: "+e+" at path /"+t.toString());return r.path=t,r}function n(t,e,n){n=n||{shiftReferences:!1};var a={};return function u(t,e,h,c){h=h||[];var l,f,p,d,y=h.join("λ");for(p in e)if(s[p])switch(d=e[p],a[y]=!0,p){case"$push":if(!i.Array(t))throw r(h,"using command $push to a non array");i.Array(d)?t.push.apply(t,d):t.push(d);break;case"$unshift":if(!i.Array(t))throw r(h,"using command $unshift to a non array");i.Array(d)?t.unshift.apply(t,d):t.unshift(d)}else if(f=y?y+"λ"+p:p,"$unset"in(e[p]||{}))a[f]=!0,i.Array(t)?n.shiftReferences?c[h[h.length-1]]=t.slice(0,+p).concat(t.slice(+p+1)):t.splice(p,1):delete t[p];else if("$set"in(e[p]||{}))d=e[p].$set,a[f]=!0,t[p]=d;else if("$apply"in(e[p]||{})||"$chain"in(e[p]||{})){if(l=e[p].$apply||e[p].$chain,"function"!=typeof l)throw r(h.concat(p),"using command $apply with a non function");a[f]=!0,t[p]=l.call(null,t[p])}else if("$merge"in(e[p]||{})){if(d=e[p].$merge,!i.Object(t[p]))throw r(h.concat(p),"using command $merge on a non-object");a[f]=!0,t[p]=o.shallowMerge(t[p],d)}else if(n.shiftReferences&&("$push"in(e[p]||{})||"$unshift"in(e[p]||{}))){if("$push"in(e[p]||{})){if(d=e[p].$push,!i.Array(t[p]))throw r(h.concat(p),"using command $push to a non array");t[p]=t[p].concat(d)}if("$unshift"in(e[p]||{})){if(d=e[p].$unshift,!i.Array(t[p]))throw r(h.concat(p),"using command $unshift to a non array");t[p]=(d instanceof Array?d:[d]).concat(t[p])}a[f]=!0}else"undefined"==typeof t[p]&&(t[p]={}),n.shiftReferences&&(t[p]=o.shallowClone(t[p])),u(t[p],e[p],h.concat(p),t)}(t,e),Object.keys(a).map(function(t){return t.split("λ")})}var o=t("./helpers.js"),i=t("./type.js"),s={};["$set","$push","$unshift","$apply","$merge"].forEach(function(t){s[t]=!0}),e.exports=n},{"./helpers.js":8,"./type.js":11}]},{},[1])(1)}); \ No newline at end of file diff --git a/index.js b/index.js index 0a6de10..c8eff74 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ var Baobab = require('./src/baobab.js'), // Non-writable version Object.defineProperty(Baobab, 'version', { - value: '0.4.3' + value: '0.4.4' }); // Exposing helpers diff --git a/package.json b/package.json index aad235a..1415ce0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "baobab", - "version": "0.4.3", + "version": "0.4.4", "description": "JavaScript data tree with cursors.", "main": "index.js", "dependencies": {