diff --git a/bower.json b/bower.json index 4980254..14df65f 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "baobab", "main": "build/baobab.min.js", - "version": "0.3.2", + "version": "0.4.0", "homepage": "https://github.com/Yomguithereal/baobab", "author": { "name": "Guillaume Plique", diff --git a/build/baobab.min.js b/build/baobab.min.js index 248e169..b18c65f 100644 --- a/build/baobab.min.js +++ b/build/baobab.min.js @@ -1,2 +1,2 @@ -/* baobab.js - Version: 0.3.2 - 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 h="function"==typeof require&&require;if(!a&&h)return h(s,!0);if(i)return i(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}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,h,u,c;if("function"==typeof e){for(u="string"==typeof t?[t]:t,n=0,s=u.length;n!==s;n+=1)if(h=u[n]){this._handlers[h]||(this._handlers[h]=[]),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[h].push(c)}}else if(t&&"object"==typeof t&&!Array.isArray(t))for(h in t)i.prototype.on.call(this,h,t[h],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,h,u="string"==typeof t?[t]:t;if(1===arguments.length&&"function"==typeof u){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=u.length;r!==n;r+=1){if(h=u[r],this._handlers[h]){for(a=[],o=0,i=this._handlers[h].length;o!==i;o+=1)this._handlers[h][o].handler!==e&&a.push(this._handlers[h][o]);this._handlers[h]=a}this._handlers[h]&&0===this._handlers[h].length&&delete this._handlers[h]}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,h,u,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(h={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,h),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,h,u,c,l,f,p,d=!1,y=!1,g=r.get(t);if("string"===r.get(o)){for(s=o.replace(/^[\?\!]/,"").split(/\|/),h=s.length,a=0;h>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(u in o)if(l=e(t[u],o[u]))return c=l,c.path=c.path?[u].concat(c.path):[u],c;for(u in t)if(void 0===o[u])return c=new Error,c.message='Unexpected key "'+u+'".',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(h=t.length,a=0;h>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,h,u;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,u=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,u=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],h={};for(o in s)void 0===n[s[o]]&&(n[s[o]]=1,h[s[o]]=1);if("function"!==this.get(u)&&!this.isValid(u))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:u}:{},void 0!==r)for(o in r)n[a][o]=r[o];for(o in h)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"),h=t("./update.js"),u=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=h(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(u){u.path=e[o].concat(u.path||[]),i.push(u)}i.length&&this.emit("invalid",{errors:i})}return this.emit("update",{log:e}),this._transaction={},this._future&&(this._future=clearTimeout(this._future)),this},n.prototype.select=function(t){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.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={};return r[t]={$set:e},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=u(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(){this.kill(),delete this.data,delete this._transaction,delete this._history;for(var t in this._cursors)this._cursors[t].release();delete this._cursors},n.prototype.toJSON=function(){return this.reference()},e.exports=n},{"../defaults.js":1,"./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)}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.root=n.root,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.root.on("update",this.treeListener),r(this,n),o.forEach(function(e){this[t](e)},this)}function o(t){n.prototype[t]=function(e){if(!a.Cursor(e))throw Error("baobab.Combination."+t+": argument should be a cursor.");if(~this.cursors.indexOf(e))throw Error("baobab.Combination."+t+": cursor already in combination.");return this.cursors.push(e),this.operators.push(t),this.updates.length++,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.kill(),this.cursors.forEach(function(t){t.off("update",this.cursorListener)},this),this.root.off("update",this.treeListener),this.cursors=null,this.operators=null,this.root=null,this.updates=null},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.root=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,h,u=t.data.log,c=!1;if(a.complexPath&&(a.solvedPath=s.solvePath(a.root.data,a.path)),this._handlers.update.length||this._handlersAll.length){if(!a.path.length)return a.emit("update");t:for(i=0,n=u.length;n>i;i++)for(e=u[i],h=0,o=e.length;o>h&&(r=e[h],r===""+a.solvedPath[h]);h++)if(h+1===o||h+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.root.on("update",this.updateHandler),this.mixin=i.cursor(this)}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.select=function(t){if(arguments.length>1&&(t=s.arrayOf(arguments)),!a.Path(t))throw Error("baobab.Cursor.select: invalid path.");return this.root.select(this.path.concat(t))},r.prototype.up=function(){return this.solvedPath&&this.solvedPath.length?this.root.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.root.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.root.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.root.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.root.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.root.select(this.solvedPath.concat(0)):null},r.prototype.get=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.get(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.reference=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.reference(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.clone=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.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={};return r[t]={$set:e},this.update(r)},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.");var e={};return e[t]={$unset:!0},this.update(e)},r.prototype.remove=function(){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.thread=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.thread: argument is not a function.");return this.update({$thread: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.root.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.root.off("update",this.updateHandler),this.hash&&delete this.root._cursors[this.hash],this.root=null,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(t){return Array.prototype.slice.call(t)}function n(t,e){var r,n={};for(r in t)n[r]=t[r];for(r in e)n[r]=e[r];return n}function o(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 i(t,e){if(!e||"object"!=typeof e||e instanceof Error||e instanceof ArrayBuffer)return e;if(g.Array(e)){if(t){var r,n,i=[];for(r=0,n=e.length;n>r;r++)i.push(b(e[r]));return i}return e.slice(0)}if(g.Date(e))return new Date(e.getTime());if(e instanceof RegExp)return o(e);if(g.Object(e)){var s,a={};e.constructor&&e.constructor!==Object&&(a=Object.create(e.constructor.prototype));for(s in e)e.hasOwnProperty(s)&&(a[s]=t?b(e[s]):e[s]);return a}return e}function s(t,e){return function(r){return e(t(r))}}function a(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 u(t,e){var r,n=!0;for(r in e)if(g.Object(e[r]))n=n&&u(t[r]);else if(g.Array(e[r]))n=n&&!!~e[r].indexOf(t[r]);else if(t[r]!==e[r])return!1;return n}function c(t,e){return a(t,function(t){return u(t,e)})}function l(t,e){return h(t,function(t){return u(t,e)})}function f(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(!g.Array(o))return;o=a(o,e[r])}else if("object"==typeof e[r]){if(!g.Array(o))return;o=c(o,e[r])}else o=o[e[r]]}return o}function p(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(!g.Array(s))return;r=h(s,e[n]),i.push(r),s=s[r]}else if("object"==typeof e[n]){if(!g.Array(s))return;r=l(s,e[n]),i.push(r),s=s[r]}else i.push(e[n]),s=s[e[n]]}return i}function d(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 y(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}var g=t("./type.js"),m=i.bind(null,!1),b=i.bind(null,!0);e.exports={arrayOf:r,deepClone:b,shallowClone:m,shallowMerge:n,compose:s,getIn:f,inherits:y,pathObject:d,solvePath:p}},{"./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,h={},u=arguments.length;for(r=u-1;r>=0;r--){if(arguments[r].$unset)delete h.$set,delete h.$apply,delete h.$merge,h.$unset=arguments[r].$unset;else{if(arguments[r].$set){delete h.$apply,delete h.$merge,delete h.$unset,h.$set=arguments[r].$set;continue}if(arguments[r].$merge){delete h.$set,delete h.$apply,delete h.$unset,h.$merge=arguments[r].$merge;continue}if(arguments[r].$apply){delete h.$set,delete h.$merge,delete h.$unset,h.$apply=arguments[r].$apply;continue}if(arguments[r].$chain){delete h.$set,delete h.$merge,delete h.$unset,h.$apply=h.$apply?i.compose(h.$apply,arguments[r].$chain):arguments[r].$chain;continue}}for(a in arguments[r])t=h[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):h[a]=o(e,t):h[a]=e}return h}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 or array).");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(-1===["object","array"].indexOf(n(this.cursors)))throw Error("baobab.mixin.cursor: invalid data (object or array).");if(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.root.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.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||{};var a={};return function h(t,e,u){u=u||[];var c,l,f,p,d=u.join("λ");for(f in e)if(s[f])switch(p=e[f],a[d]=!0,f){case"$push":if(!i.Array(t))throw r(u,"using command $push to a non array");i.Array(p)?t.push.apply(t,p):t.push(p);break;case"$unshift":if(!i.Array(t))throw r(u,"using command $unshift to a non array");i.Array(p)?t.unshift.apply(t,p):t.unshift(p)}else if(l=d?d+"λ"+f:f,"$unset"in(e[f]||{}))a[l]=!0,delete t[f];else if("$set"in(e[f]||{}))p=e[f].$set,a[l]=!0,t[f]=p;else if("$apply"in(e[f]||{})){if(c=e[f].$apply,"function"!=typeof c)throw r(u.concat(f),"using command $apply with a non function");a[l]=!0,t[f]=c.call(null,t[f])}else if("$merge"in(e[f]||{})){if(p=e[f].$merge,!i.Object(t[f]))throw r(u.concat(f),"using command $merge on a non-object");a[l]=!0,t[f]=o.shallowMerge(t[f],p)}else if(n.shiftReferences&&("$push"in(e[f]||{})||"$unshift"in(e[f]||{}))){if("$push"in(e[f]||{})){if(p=e[f].$push,!i.Array(t[f]))throw r(u.concat(f),"using command $push to a non array");t[f]=t[f].concat(p)}if("$unshift"in(e[f]||{})){if(p=e[f].$unshift,!i.Array(t[f]))throw r(u.concat(f),"using command $unshift to a non array");t[f]=(p instanceof Array?p:[p]).concat(t[f])}a[l]=!0}else"undefined"==typeof t[f]&&(t[f]={}),n.shiftReferences&&(t[f]=o.shallowClone(t[f])),h(t[f],e[f],u.concat(f))}(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}]},{},[2])(2)}); \ No newline at end of file +/* baobab.js - Version: 0.4.0 - 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;sr;r++)if(t._children[r]===e){t._children.splice(r,1);break}}),this._children.push(e),e},o.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},o.prototype.disable=function(){return this._enabled=!1,this},o.prototype.enable=function(){return this._enabled=!0,this},o.version="2.1.1","undefined"!=typeof n?("undefined"!=typeof r&&r.exports&&(n=r.exports=o),n.Emitter=o):"function"==typeof t&&t.amd?t("emmett",[],function(){return o}):this.Emitter=o}).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.emit("update",{log:e}),this._transaction={},this._future&&(this._future=clearTimeout(this._future)),this},n.prototype.select=function(t){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.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={};return r[t]={$set:e},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(){this.kill(),delete this.data,delete this._transaction,delete this._history;for(var t in this._cursors)this._cursors[t].release();delete this._cursors},n.prototype.toJSON=function(){return this.reference()},e.exports=n},{"../defaults.js":1,"./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)}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.root=n.root,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.root.on("update",this.treeListener),r(this,n),o.forEach(function(e){this[t](e)},this)}function o(t){n.prototype[t]=function(e){if(!a.Cursor(e))throw Error("baobab.Combination."+t+": argument should be a cursor.");if(~this.cursors.indexOf(e))throw Error("baobab.Combination."+t+": cursor already in combination.");return this.cursors.push(e),this.operators.push(t),this.updates.length++,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.kill(),this.cursors.forEach(function(t){t.off("update",this.cursorListener)},this),this.root.off("update",this.treeListener),this.cursors=null,this.operators=null,this.root=null,this.updates=null},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.root=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.root.data,a.path)),this._handlers.update.length||this._handlersAll.length){if(!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.root.on("update",this.updateHandler),this.mixin=i.cursor(this)}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.select=function(t){if(arguments.length>1&&(t=s.arrayOf(arguments)),!a.Path(t))throw Error("baobab.Cursor.select: invalid path.");return this.root.select(this.path.concat(t))},r.prototype.up=function(){return this.solvedPath&&this.solvedPath.length?this.root.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.root.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.root.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.root.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.root.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.root.select(this.solvedPath.concat(0)):null},r.prototype.get=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.get(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.reference=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.reference(a.Step(t)?this.solvedPath.concat(t):this.solvedPath)},r.prototype.clone=function(t){return arguments.length>1&&(t=s.arrayOf(arguments)),this.root.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={};return r[t]={$set:e},this.update(r)},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.");var e={};return e[t]={$unset:!0},this.update(e)},r.prototype.remove=function(){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.thread=function(t){if("function"!=typeof t)throw Error("baobab.Cursor.thread: argument is not a function.");return this.update({$thread: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.root.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.root.off("update",this.updateHandler),this.hash&&delete this.root._cursors[this.hash],this.root=null,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(t){return Array.prototype.slice.call(t)}function n(t,e){var r,n={};for(r in t)n[r]=t[r];for(r in e)n[r]=e[r];return n}function o(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 i(t,e){if(!e||"object"!=typeof e||e instanceof Error||e instanceof ArrayBuffer)return e;if(g.Array(e)){if(t){var r,n,i=[];for(r=0,n=e.length;n>r;r++)i.push(b(e[r]));return i}return e.slice(0)}if(g.Date(e))return new Date(e.getTime());if(e instanceof RegExp)return o(e);if(g.Object(e)){var s,a={};e.constructor&&e.constructor!==Object&&(a=Object.create(e.constructor.prototype));for(s in e)e.hasOwnProperty(s)&&(a[s]=t?b(e[s]):e[s]);return a}return e}function s(t,e){return function(r){return e(t(r))}}function a(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return t[r]}function u(t,e){var r,n;for(r=0,n=t.length;n>r;r++)if(e(t[r]))return r;return-1}function h(t,e){var r,n=!0;for(r in e)if(g.Object(e[r]))n=n&&h(t[r]);else if(g.Array(e[r]))n=n&&!!~e[r].indexOf(t[r]);else if(t[r]!==e[r])return!1;return n}function c(t,e){return a(t,function(t){return h(t,e)})}function l(t,e){return u(t,function(t){return h(t,e)})}function f(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(!g.Array(o))return;o=a(o,e[r])}else if("object"==typeof e[r]){if(!g.Array(o))return;o=c(o,e[r])}else o=o[e[r]]}return o}function p(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(!g.Array(s))return;r=u(s,e[n]),i.push(r),s=s[r]}else if("object"==typeof e[n]){if(!g.Array(s))return;r=l(s,e[n]),i.push(r),s=s[r]}else i.push(e[n]),s=s[e[n]]}return i}function d(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 y(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}var g=t("./type.js"),m=i.bind(null,!1),b=i.bind(null,!0);e.exports={arrayOf:r,deepClone:b,shallowClone:m,shallowMerge:n,compose:s,getIn:f,inherits:y,pathObject:d,solvePath:p}},{"./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 or array).");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(-1===["object","array"].indexOf(n(this.cursors)))throw Error("baobab.mixin.cursor: invalid data (object or array).");if(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.root.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.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||{};var a={};return function u(t,e,h){h=h||[];var c,l,f,p,d=h.join("λ");for(f in e)if(s[f])switch(p=e[f],a[d]=!0,f){case"$push":if(!i.Array(t))throw r(h,"using command $push to a non array");i.Array(p)?t.push.apply(t,p):t.push(p);break;case"$unshift":if(!i.Array(t))throw r(h,"using command $unshift to a non array");i.Array(p)?t.unshift.apply(t,p):t.unshift(p)}else if(l=d?d+"λ"+f:f,"$unset"in(e[f]||{}))a[l]=!0,delete t[f];else if("$set"in(e[f]||{}))p=e[f].$set,a[l]=!0,t[f]=p;else if("$apply"in(e[f]||{})){if(c=e[f].$apply,"function"!=typeof c)throw r(h.concat(f),"using command $apply with a non function");a[l]=!0,t[f]=c.call(null,t[f])}else if("$merge"in(e[f]||{})){if(p=e[f].$merge,!i.Object(t[f]))throw r(h.concat(f),"using command $merge on a non-object");a[l]=!0,t[f]=o.shallowMerge(t[f],p)}else if(n.shiftReferences&&("$push"in(e[f]||{})||"$unshift"in(e[f]||{}))){if("$push"in(e[f]||{})){if(p=e[f].$push,!i.Array(t[f]))throw r(h.concat(f),"using command $push to a non array");t[f]=t[f].concat(p)}if("$unshift"in(e[f]||{})){if(p=e[f].$unshift,!i.Array(t[f]))throw r(h.concat(f),"using command $unshift to a non array");t[f]=(p instanceof Array?p:[p]).concat(t[f])}a[l]=!0}else"undefined"==typeof t[f]&&(t[f]={}),n.shiftReferences&&(t[f]=o.shallowClone(t[f])),u(t[f],e[f],h.concat(f))}(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}]},{},[2])(2)}); \ No newline at end of file diff --git a/index.js b/index.js index 31934d2..5527570 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.3.2' + value: '0.4.0' }); // Exposing helpers diff --git a/package.json b/package.json index 7de5538..68196b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "baobab", - "version": "0.3.2", + "version": "0.4.0", "description": "JavaScript data tree with cursors.", "main": "index.js", "dependencies": {