/* * Baobab * * Homepage: https://github.com/Yomguithereal/baobab * Version: 2.6.1 * Author: Yomguithereal (Guillaume Plique) * License: MIT */ !function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).Baobab=t()}}((function(){return function t(e,r,n){function i(o,s){if(!r[o]){if(!e[o]){var h="function"==typeof require&&require;if(!s&&h)return h(o,!0);if(a)return a(o,!0);var l=new Error("Cannot find module '"+o+"'");throw l.code="MODULE_NOT_FOUND",l}var u=r[o]={exports:{}};e[o][0].call(u.exports,(function(t){return i(e[o][1][t]||t)}),u,u.exports,t,e,r,n)}return r[o].exports}for(var a="function"==typeof require&&require,o=0;o1?t[e]=a(t[e],{once:!0}):t.push({once:!0}),this.on.apply(this,t)},h.prototype.off=function(t,e){var r,n,i,a;if(1===arguments.length&&"function"==typeof t){e=arguments[0];var h=Object.keys(this._handlers).concat(Object.getOwnPropertySymbols(this._handlers));for(r=0;r1&&(r.data=e),a.fn.call("scope"in a?a.scope:this,r),a.once&&d.push(a);for(l=d.length-1;l>=0;l--){var p=(n=d[l].type?this._handlers[d[l].type]:d[l].pattern?this._handlersComplex:this._handlersAll).indexOf(d[l]);-1!==p&&n.splice(p,1)}}return this},h.prototype.kill=function(){this.unbindAll(),this._handlers=null,this._handlersAll=null,this._handlersComplex=null,this._enabled=!1,this.unbindAll=this.on=this.once=this.off=this.emit=this.listeners=Function.prototype},h.prototype.disable=function(){return this._enabled=!1,this},h.prototype.enable=function(){return this._enabled=!0,this},h.version="3.2.0",e.exports=h},{}],2:[function(t,e,r){"use strict";r.__esModule=!0,r.helpers=r.default=r.VERSION=r.dynamic=r.monkey=void 0;var n=c(t("emmett")),i=c(t("./cursor"));r.Cursor=i.default;var a=t("./monkey");r.MonkeyDefinition=a.MonkeyDefinition,r.Monkey=a.Monkey;var o=c(t("./watcher")),s=c(t("./type"));r.type=s.default;var h=c(t("./update")),l=function(t){if(t&&t.__esModule)return t;if(null===t||"object"!=typeof t&&"function"!=typeof t)return{default:t};var e=u();if(e&&e.has(t))return e.get(t);var r={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var i in t)if(Object.prototype.hasOwnProperty.call(t,i)){var a=n?Object.getOwnPropertyDescriptor(t,i):null;a&&(a.get||a.set)?Object.defineProperty(r,i,a):r[i]=t[i]}r.default=t,e&&e.set(t,r);return r}(t("./helpers"));function u(){if("function"!=typeof WeakMap)return null;var t=new WeakMap;return u=function(){return t},t}function c(t){return t&&t.__esModule?t:{default:t}}function f(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}r.helpers=l;var d=l.arrayFrom,p=l.coercePath,y=l.deepFreeze,v=l.getIn,g=l.makeError,m=l.deepClone,b=l.deepMerge,_=l.shallowClone,k=l.shallowMerge,w=l.hashPath,P={autoCommit:!0,asynchronous:!0,immutable:!0,lazyMonkeys:!0,monkeyBusiness:!0,persistent:!0,pure:!0,validate:null,validationBehavior:"rollback"},j=function(t){var e,r;function n(e,r){var n;if(n=t.call(this)||this,arguments.length<1&&(e={}),!s.default.object(e)&&!s.default.array(e))throw g("Baobab: invalid data.",{data:e});n.options=k({},P,r),n.options.persistent||(n.options.immutable=!1,n.options.pure=!1),n._identity="[object Baobab]",n._cursors={},n._future=null,n._transaction=[],n._affectedPathsIndex={},n._monkeys={},n._previousData=null,n._data=e,n.root=new i.default(f(n),[],"λ"),delete n.root.release,n.options.immutable&&y(n._data);var a=function(t){n[t]=function(){var e=this.root[t].apply(this.root,arguments);return e instanceof i.default?this:e}};["apply","clone","concat","deepClone","deepMerge","exists","get","push","merge","pop","project","serialize","set","shift","splice","unset","unshift"].forEach(a),n.options.monkeyBusiness&&n._refreshMonkeys();var o=n.validate();if(o)throw Error("Baobab: invalid data.",{error:o});return n}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,e.__proto__=r;var l=n.prototype;return l._refreshMonkeys=function(t,e,r){var n=this,i=function t(e,r){if(void 0===r&&(r=[]),e instanceof a.Monkey)return e.release(),void(0,h.default)(n._monkeys,r,{type:"unset"},{immutable:!1,persistent:!1,pure:!1});if(s.default.object(e))for(var i in e)t(e[i],r.concat(i))},o=function t(e,r){if(void 0===r&&(r=[]),e instanceof a.MonkeyDefinition||e instanceof a.Monkey){var i=new a.Monkey(n,r,e instanceof a.Monkey?e.definition:e);(0,h.default)(n._monkeys,r,{type:"set",value:i},{immutable:!1,persistent:!1,pure:!1})}else if(s.default.object(e))for(var o in e)t(e[o],r.concat(o))};if(arguments.length){var l=v(this._monkeys,e).data;l&&i(l,e),"unset"!==r&&o(t,e)}else o(this._data);return this},l.validate=function(t){var e=this.options,r=e.validate,n=e.validationBehavior;if("function"!=typeof r)return null;var i=r.call(this,this._previousData,this._data,t||[[]]);return i instanceof Error?("rollback"===n&&(this._data=this._previousData,this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data),this.emit("invalid",{error:i}),i):null},l.select=function(t){if(t=t||[],arguments.length>1&&(t=d(arguments)),!s.default.path(t))throw g("Baobab.select: invalid path.",{path:t});t=[].concat(t);var e=w(t),r=this._cursors[e];return r||(r=new i.default(this,t,e),this._cursors[e]=r),this.emit("select",{path:t,cursor:r}),r},l.update=function(t,e){var r=this;if(t=p(t),!s.default.operationType(e.type))throw g('Baobab.update: unknown operation type "'+e.type+'".',{operation:e});var n=v(this._data,t),i=n.solvedPath,a=n.exists;if(!i)throw g("Baobab.update: could not solve the given path.",{path:i});var o=s.default.monkeyPath(this._monkeys,i);if(o&&i.length>o.length)throw g("Baobab.update: attempting to update a read-only path.",{path:i});if("unset"!==e.type||a){var l=e;if(/merge/i.test(e.type)){var u=v(this._monkeys,i).data;if(s.default.object(u)){l=_(l);var c=v(this._data,i).data;/deep/i.test(l.type)?l.value=b({},b({},c,m(u)),l.value):l.value=k({},b({},c,m(u)),l.value)}}this._transaction.length||(this._previousData=this._data);var f=(0,h.default)(this._data,i,l,this.options),d=f.data,y=f.node;if(!("data"in f))return y;var P=i.concat("push"===e.type?y.length-1:[]),j=w(P);return this._data=d,this._affectedPathsIndex[j]=!0,this._transaction.push(k({},e,{path:P})),this.options.monkeyBusiness&&this._refreshMonkeys(y,i,e.type),this.emit("write",{path:P}),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout((function(){return r.commit()}),0)),y):(this.commit(),y):y}},l.commit=function(){if(!this._transaction.length)return this;this._future&&(this._future=clearTimeout(this._future));var t=Object.keys(this._affectedPathsIndex).map((function(t){return"λ"!==t?t.split("λ").slice(1):[]}));if(this.validate(t))return this;var e=this._transaction,r=this._previousData;return this._affectedPathsIndex={},this._transaction=[],this._previousData=this._data,this.emit("update",{paths:t,currentData:this._data,transaction:e,previousData:r}),this},l.getMonkey=function(t){t=p(t);var e=v(this._monkeys,[].concat(t)).data;return e instanceof a.Monkey?e:null},l.watch=function(t){return new o.default(this,t)},l.release=function(){var t;for(t in this.emit("release"),delete this.root,delete this._data,delete this._previousData,delete this._transaction,delete this._affectedPathsIndex,delete this._monkeys,this._cursors)this._cursors[t].release();delete this._cursors,this.kill()},l.toJSON=function(){return this.serialize()},l.toString=function(){return this._identity},n}(n.default);j.monkey=function(){for(var t=arguments.length,e=new Array(t),r=0;r1&&(t=(0,o.arrayFrom)(arguments)),this.tree.select(this.path.concat(t))},s.up=function(){return this.isRoot()?null:this.tree.select(this.path.slice(0,-1))},s.down=function(){if(l("down",this.solvedPath),!(this._get().data instanceof Array))throw Error("Baobab.Cursor.down: cannot go down on a non-list type.");return this.tree.select(this.solvedPath.concat(0))},s.left=function(){l("left",this.solvedPath);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},s.right=function(){l("right",this.solvedPath);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()._get().data.length?null:this.tree.select(this.solvedPath.slice(0,-1).concat(t+1))},s.leftmost=function(){l("leftmost",this.solvedPath);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))},s.rightmost=function(){l("rightmost",this.solvedPath);var t=+this.solvedPath[this.solvedPath.length-1];if(isNaN(t))throw Error("Baobab.Cursor.rightmost: cannot go right on a non-list type.");var e=this.up()._get().data;return this.tree.select(this.solvedPath.slice(0,-1).concat(e.length-1))},s.map=function(t,e){l("map",this.solvedPath);var r=this._get().data,n=arguments.length;if(!a.default.array(r))throw Error("baobab.Cursor.map: cannot map a non-list type.");return r.map((function(i,a){return t.call(n>1?e:this,this.select(a),a,r)}),this)},s._get=function(t){if(void 0===t&&(t=[]),!a.default.path(t))throw(0,o.makeError)("Baobab.Cursor.getters: invalid path.",{path:t});return this.solvedPath?(0,o.getIn)(this.tree._data,this.solvedPath.concat(t)):{data:void 0,solvedPath:null,exists:!1}},s.exists=function(t){return t=(0,o.coercePath)(t),arguments.length>1&&(t=(0,o.arrayFrom)(arguments)),this._get(t).exists},s.get=function(t){t=(0,o.coercePath)(t),arguments.length>1&&(t=(0,o.arrayFrom)(arguments));var e=this._get(t),r=e.data,n=e.solvedPath;return this.tree.emit("get",{data:r,solvedPath:n,path:this.path.concat(t)}),r},s.clone=function(){var t=this.get.apply(this,arguments);return(0,o.shallowClone)(t)},s.deepClone=function(){var t=this.get.apply(this,arguments);return(0,o.deepClone)(t)},s.serialize=function(t){if(t=(0,o.coercePath)(t),arguments.length>1&&(t=(0,o.arrayFrom)(arguments)),!a.default.path(t))throw(0,o.makeError)("Baobab.Cursor.getters: invalid path.",{path:t});if(this.solvedPath){var e=this.solvedPath.concat(t),r=(0,o.deepClone)((0,o.getIn)(this.tree._data,e).data),n=(0,o.getIn)(this.tree._monkeys,e).data,s=function t(e,r){if(a.default.object(r)&&a.default.object(e))for(var n in r)r[n]instanceof i.Monkey?delete e[n]:t(e[n],r[n])};return s(r,n),r}},s.project=function(t){if(a.default.object(t)){var e={};for(var r in t)e[r]=this.get(t[r]);return e}if(a.default.array(t)){for(var n=[],i=0,s=t.length;i2)throw(0,o.makeError)("Baobab.Cursor."+t+": too many arguments.");if(1!==arguments.length||c[t]||(n=r,r=[]),r=(0,o.coercePath)(r),!a.default.path(r))throw(0,o.makeError)("Baobab.Cursor."+t+": invalid path.",{path:r});if(e&&!e(n))throw(0,o.makeError)("Baobab.Cursor."+t+": invalid value.",{path:r,value:n});if(!this.solvedPath)throw(0,o.makeError)("Baobab.Cursor."+t+": the dynamic path of the cursor cannot be solved.",{path:this.path});var i=this.solvedPath.concat(r);return this.tree.update(i,{type:t,value:n})}}f("set"),f("unset"),f("apply",a.default.function),f("push"),f("concat",a.default.array),f("unshift"),f("pop"),f("shift"),f("splice",a.default.splicer),f("merge",a.default.object),f("deepMerge",a.default.object)},{"./helpers":4,"./monkey":5,"./type":6,emmett:1}],4:[function(t,e,r){(function(e){"use strict";r.__esModule=!0,r.arrayFrom=function(t){return h(t)},r.before=function(t,e){return function(){t.apply(null,arguments),e.apply(null,arguments)}},r.coercePath=function(t){return t||0===t||""===t?t:[]},r.getIn=function(t,e){if(!e)return g;var r,n,i,o=[],h=!0,l=t;for(n=0,i=e.length;n3?n-3:0),o=3;o=0?t.slice(0,e).concat(i).concat(t.slice(e+r)):t.slice(0,t.length+e).concat(i).concat(t.slice(t.length+e+r))},r.uniqid=r.deepMerge=r.shallowMerge=r.deepFreeze=r.freeze=r.deepClone=r.shallowClone=r.Archive=void 0;var n,i=t("./monkey"),a=(n=t("./type"))&&n.__esModule?n:{default:n};var o={}.hasOwnProperty;function s(t,e){var r,n;for(r=0,n=t.length;rthis.size&&(this.records.length=this.size),this},e.clear=function(){return this.records=[],this},e.back=function(t){var e=this.records[t-1];return e&&(this.records=this.records.slice(t)),e},t}();function u(t,r){if(!r||"object"!=typeof r||r instanceof Error||r instanceof i.MonkeyDefinition||r instanceof i.Monkey||"ArrayBuffer"in e&&r instanceof ArrayBuffer)return r;if(a.default.array(r)){if(t){for(var n=new Array(r.length),o=0,s=r.length;o1?e-1:0),n=1;n1&&isNaN(+t[1]))&&a(t[0],["number","function","object"]))};var o=["string","number","function","object"];i.path=function(t){return!(!t&&0!==t&&""!==t)&&[].concat(t).every((function(t){return a(t,o)}))},i.dynamicPath=function(t){return t.some((function(t){return i.function(t)||i.object(t)}))},i.monkeyPath=function(t,e){var r,i,a=[],o=t;for(r=0,i=e.length;r0&&v.push(l),s===h-1){if("set"===u){if(n.pure&&g[l]===c)return{node:g[l]};i.default.lazyGetter(g,l)?Object.defineProperty(g,l,{value:c,enumerable:!0,configurable:!0}):n.persistent&&!d.mutableLeaf?g[l]=(0,a.shallowClone)(c):g[l]=c}else if("monkey"===u)Object.defineProperty(g,l,{get:c,enumerable:!0,configurable:!0});else if("apply"===u){var m=c(g[l]);if(n.pure&&g[l]===m)return{node:g[l]};i.default.lazyGetter(g,l)?Object.defineProperty(g,l,{value:m,enumerable:!0,configurable:!0}):n.persistent?g[l]=(0,a.shallowClone)(m):g[l]=m}else if("push"===u){if(!i.default.array(g[l]))throw o("push","array",v);n.persistent?g[l]=g[l].concat([c]):g[l].push(c)}else if("unshift"===u){if(!i.default.array(g[l]))throw o("unshift","array",v);n.persistent?g[l]=[c].concat(g[l]):g[l].unshift(c)}else if("concat"===u){if(!i.default.array(g[l]))throw o("concat","array",v);n.persistent?g[l]=g[l].concat(c):g[l].push.apply(g[l],c)}else if("splice"===u){if(!i.default.array(g[l]))throw o("splice","array",v);n.persistent?g[l]=a.splice.apply(null,[g[l]].concat(c)):g[l].splice.apply(g[l],c)}else if("pop"===u){if(!i.default.array(g[l]))throw o("pop","array",v);n.persistent?g[l]=(0,a.splice)(g[l],-1,1):g[l].pop()}else if("shift"===u){if(!i.default.array(g[l]))throw o("shift","array",v);n.persistent?g[l]=(0,a.splice)(g[l],0,1):g[l].shift()}else if("unset"===u)i.default.object(g)?delete g[l]:i.default.array(g)&&g.splice(l,1);else if("merge"===u){if(!i.default.object(g[l]))throw o("merge","object",v);n.persistent?g[l]=(0,a.shallowMerge)({},g[l],c):g[l]=(0,a.shallowMerge)(g[l],c)}else if("deepMerge"===u){if(!i.default.object(g[l]))throw o("deepMerge","object",v);n.persistent?g[l]=(0,a.deepMerge)({},g[l],c):g[l]=(0,a.deepMerge)(g[l],c)}n.immutable&&!d.mutableLeaf&&(0,a.deepFreeze)(g);break}i.default.primitive(g[l])?g[l]={}:n.persistent&&(g[l]=(0,a.shallowClone)(g[l])),n.immutable&&h>0&&(0,a.freeze)(g),g=g[l]}return i.default.lazyGetter(g,l)?{data:p.root}:{data:p.root,node:g[l]}};var n,i=(n=t("./type"))&&n.__esModule?n:{default:n},a=t("./helpers");function o(t,e,r){return(0,a.makeError)('Baobab.update: cannot apply the "'+t+'" on a non '+e+" (path: /"+r.join("/")+").",{path:r})}},{"./helpers":4,"./type":6}],8:[function(t,e,r){"use strict";r.__esModule=!0,r.default=void 0;var n=s(t("emmett")),i=s(t("./cursor")),a=s(t("./type")),o=t("./helpers");function s(t){return t&&t.__esModule?t:{default:t}}var h=function(t){var e,r;function n(e,r){var n;return(n=t.call(this)||this).tree=e,n.mapping=null,n.state={killed:!1},n.refresh(r),n.handler=function(t){if(!n.state.killed){var e=n.getWatchedPaths();return(0,o.solveUpdate)(t.data.paths,e)?n.emit("update"):void 0}},n.tree.on("update",n.handler),n}r=t,(e=n).prototype=Object.create(r.prototype),e.prototype.constructor=e,e.__proto__=r;var s=n.prototype;return s.getWatchedPaths=function(){var t=this;return Object.keys(this.mapping).map((function(e){var r=t.mapping[e];return r instanceof i.default?r.solvedPath:t.mapping[e]})).reduce((function(e,r){if(r=[].concat(r),a.default.dynamicPath(r)&&(r=(0,o.getIn)(t.tree._data,r).solvedPath),!r)return e;var n=a.default.monkeyPath(t.tree._monkeys,r);return n?e.concat((0,o.getIn)(t.tree._monkeys,n).data.relatedPaths()):e.concat([r])}),[])},s.getCursors=function(){var t=this,e={};return Object.keys(this.mapping).forEach((function(r){var n=t.mapping[r];n instanceof i.default?e[r]=n:e[r]=t.tree.select(n)})),e},s.refresh=function(t){if(!a.default.watcherMapping(t))throw(0,o.makeError)("Baobab.watch: invalid mapping.",{mapping:t});this.mapping=t;var e={};for(var r in t)e[r]=t[r]instanceof i.default?t[r].path:t[r];this.get=this.tree.project.bind(this.tree,e)},s.release=function(){this.tree.off("update",this.handler),this.state.killed=!0,this.kill()},n}(n.default);r.default=h},{"./cursor":3,"./helpers":4,"./type":6,emmett:1}]},{},[2])(2)}));