UNPKG

@aldoivan10/linked-array

Version:
2 lines (1 loc) 1.51 kB
"use strict";var a=Object.defineProperty;var n=(c,t)=>a(c,"name",{value:t,configurable:!0});class r extends Array{static{n(this,"LinkedArray")}_index=0;constructor(...t){return super(...t),new Proxy(this,{get(s,e,i){if(typeof e=="string"&&!isNaN(Number(e))){let h=Number(e);return h<0&&(h=s.length+h),Reflect.get(s,h,i)}return Reflect.get(s,e,i)}})}clean(){this.length=0,this._index=0}replace(t,s=0){this.length=0,this.push(...t),this._index=s}first(){return this[0]}last(){const t=this.length||1;return this[t-1]}current(){return this[this._index]}move(t,s){if(t>=0&&t<this.length&&s>=0&&s<this.length){const[e]=this.splice(t,1);this.splice(s,0,e)}}swap(t,s){t>=0&&t<this.length&&s>=0&&s<this.length&&([this[t],this[s]]=[this[s],this[t]])}next(){return this._index=(this._index+1)%this.length,this[this._index]}previous(){return this._index=(this._index-1+this.length)%this.length,this[this._index]}index(){return this._index}at(t){return t<0&&(t=this.length+t),this._index=t,this[t]}clone(){const t=this.map(s=>typeof s=="object"?JSON.parse(JSON.stringify(s)):s);return new r(...t)}removeAt(t){t>=0&&t<this.length&&(this.splice(t,1),t===this._index?this._index=Math.max(0,--this._index):t<this._index&&this._index--)}sorted({order:t="asc",attr:s}={order:"asc"}){const e=n((i,h)=>{const l=s?this.getValueByPath(i,s):i,u=s?this.getValueByPath(h,s):h;return l<u?t==="asc"?-1:1:l>u?t==="asc"?1:-1:0},"compare");return this.sort(e)}getValueByPath(t,s){return s.split(".").reduce((e,i)=>e[i],t)}}exports.LinkedArray=r;