UNPKG

order-map

Version:

A order-map collection in the form of key, value, that ensures the index.

11 lines (10 loc) 2.58 kB
/* Copyright (c) 2019 Daybrush name: order-map license: MIT author: Daybrush repository: git+https://github.com/daybrush/order-map.git version: 0.3.1 */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).OrderMap=e()}(this,(function(){"use strict";function t(t,e,r){if(r||2===arguments.length)for(var n,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))}var e=function(){function e(t){this.separator=t,this.orderMap={}}return e.prototype.getFullName=function(t){return t.join(this.separator)},e.prototype.get=function(t){return this.orderMap[this.getFullName(t)]},e.prototype.hasName=function(t){var e=t.length;if(!e)return!1;var r=t[e-1],n=this.get(t.slice(0,e-1));return!!n&&n.indexOf(r)>=0},e.prototype.gets=function(e,r){void 0===r&&(r=!0);var n=[],o=this;return function e(r,i){var a=o.get(r);if(a)return a.forEach((function(o){var a=t(t([],i,!0),[o],!1),s=e(t(t([],r,!0),[o],!1),a);s&&s.length||n.push(t(t([],i,!0),[o],!1))})),a}(e,r?e:[]),n},e.prototype.set=function(t,e){var r=this;return t.forEach((function(e,n){r.addName(t.slice(0,n),e)})),this.orderMap[this.getFullName(t)]=e,e},e.prototype.add=function(t){var e=t.length;return e?this.addName(t.slice(0,-1),t[e-1]):[]},e.prototype.addName=function(t,e){var r=this.get(t)||this.set(t,[]);return-1===r.indexOf(e)&&r.push(e),r},e.prototype.findIndex=function(t,e){var r=this.orderMap[this.getFullName(t)];return r?r.indexOf(e):-1},e.prototype.remove=function(t){var e=this.getFullName(t),r=this.orderMap;for(var n in r)0===n.indexOf(e)&&delete r[n];var o=t.length;if(o){var i=t.slice(0,-1),a=t[o-1];this.splice(i,this.findIndex(i,a),1)}return this},e.prototype.filter=function(r,n,o){void 0===o&&(o=!0);var i=this.gets(r,o).filter(n),a=new e(this.separator),s=o?[]:r;return i.forEach((function(e){a.add(t(t([],s,!0),e,!0))})),a},e.prototype.splice=function(e,r,n){for(var o=[],i=3;i<arguments.length;i++)o[i-3]=arguments[i];var a=this.get(e)||this.set(e,[]);return a.splice.apply(a,t([r,n],o,!1)),this},e.prototype.clear=function(){this.orderMap={}},e.prototype.setObject=function(t){var e=this.orderMap;for(var r in t)e[r]=t[r].slice()},e.prototype.getObject=function(){var t={},e=this.orderMap;for(var r in e)t[r]=e[r].slice();return t},e.prototype.clone=function(){var t=new e(this.separator);return t.setObject(t.orderMap),t},e}();return e})); //# sourceMappingURL=order-map.min.js.map