storecontrol
Version:
A lightweight JavaScript state management library/tool for both client and server side.
6 lines • 3.91 kB
JavaScript
/*!
* StoreControl
* (c) 2019 Yong Quan Lim
* Released under MIT License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).StoreControl=t()}(this,(function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function t(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}function r(e,t){return r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},r(e,t)}function i(e,t){if(e!==t)throw new TypeError("Cannot instantiate an arrow function")}function o(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function s(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=[],r=!0,i=!1,o=void 0;try{for(var s,a=e[Symbol.iterator]();!(r=(s=a.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(e){i=!0,o=e}finally{try{r||null==a.return||a.return()}finally{if(i)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var a=function(){function n(){var t=this;e(this,n),function(){i(this,t);for(var e=0;e<arguments.length;e++)Object.defineProperty(this,e<0||arguments.length<=e?void 0:arguments[e],{value:[]})}.bind(this)("keys","values","entries")}var r,o,a;return r=n,(o=[{key:"size",value:function(){var e=this.entries.length,t=this.values.length,n=this.keys.length;if(e!==t||e!==n||"number"!=typeof e||e<0)throw new RangeError("Item(s) of entries, values or keys has been altered by external method.");return e}},{key:"set",value:function(e,t){if(null==e||"boolean"==typeof e||"number"==typeof e&&(!isFinite(e)||isNaN(e)))throw new TypeError("Key cannot be null, a boolean, NaN, or an infinite number.");var n=this.keys.indexOf(e);if(n<0)n=this.size(),this.entries[n]=[];else if(this.values[n]instanceof u)throw new TypeError('Cannot replace "'.concat(e,'" because it is a user-defined StoreControl branch.'));return this.keys[n]=e,this.values[n]=t,this.entries[n][0]=e,this.entries[n][1]=t,t}},{key:"get",value:function(e){var t=this.keys.indexOf(e);return t<0?null:this.values[t]}},{key:"has",value:function(e){return this.keys.indexOf(e)>=0}},{key:"branch",value:function(e){if(this.has(e)){try{e=JSON.stringify(e)}catch(t){e=String(e)}throw new TypeError('Property "'.concat(e,'" already exists in this StoreControl instance.'))}return this.set(e,new u)}},{key:"of",value:function(e){var t=this.get(e);if(null===t)throw new ReferenceError('Branch "'.concat(e,'" does not exist in this StoreControl instance.'));if(!(t instanceof u))throw new TypeError('"'.concat(e,'" key does not link to a StoreControl branch.'));return t}},{key:"isBranch",value:function(e){try{return!!this.of(e)}catch(e){return!1}}},{key:"delete",value:function(e){var t=this.keys.indexOf(e);return!(t<0||(this.keys.splice(t,1),this.values.splice(t,1),this.entries.splice(t,1),0))}},{key:"clear",value:function(){this.keys.splice(0),this.values.splice(0),this.entries.splice(0)}},{key:"asMap",value:function(){var e=this,t={};return this.entries.forEach(function(n){var r=s(n,2),o=r[0],a=r[1];i(this,e),t[o]=this.isBranch(o)?a.asMap():a}.bind(this)),t}}])&&t(r.prototype,o),a&&t(r,a),n}(),u=function(t){function i(){return e(this,i),o(this,n(i).call(this))}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&r(e,t)}(i,t),i}(a);return a}));