UNPKG

6.28 kBJavaScriptView Raw
1const e=[8,9,"a","b"];function t(e){return JSON.parse(JSON.stringify(e,null,0))}function s(e,t){for(const s of e.entries())t(s[1],s[0]);return e}function r(e="",t="|",s={}){return e.split(t).reduce(((e,r,i)=>{const n=[];return(Array.isArray(s[r])?s[r]:[s[r]]).forEach((s=>0===i?n.push(s):e.forEach((e=>n.push(`${e}${t}${s}`))))),n}),[])}function i(e,t,i,n,a){e.forEach((e=>{const h=t.get(e);s(e.includes(i)?r(e,i,a):Array.isArray(a[e])?a[e]:[a[e]],(e=>{if(h.has(e)){const t=h.get(e);t.delete(n),0===t.size&&h.delete(e)}}))}))}function n(e,t){return e instanceof Object&&t instanceof Object?s(Object.keys(t),(s=>{e[s]instanceof Object&&t[s]instanceof Object?e[s]=n(e[s],t[s]):Array.isArray(e[s])&&Array.isArray(t[s])?e[s]=e[s].concat(t[s]):e[s]=t[s]})):e=Array.isArray(e)&&Array.isArray(t)?e.concat(t):t,e}function a(){return(65536*(Math.random()+1)|0).toString(16).substring(1)}function h(e,t,i,n,a,h){s(null===h?e:[h],(e=>{const h=t.get(e);e.includes(i)?s(r(e,i,a),(e=>{!1===h.has(e)&&h.set(e,new Set),h.get(e).add(n)})):s(Array.isArray(a[e])?a[e]:[a[e]],(e=>{!1===h.has(e)&&h.set(e,new Set),h.get(e).add(n)}))}))}function o(){return a()+a()+"-"+a()+"-4"+a().substr(0,3)+"-"+e[Math.floor(4*Math.random())]+a().substr(0,3)+"-"+a()+a()+a()}class c{constructor({delimiter:e="|",id:t=o(),index:s=[],key:r="",versioning:i=!1}={}){return this.data=new Map,this.delimiter=e,this.id=t,this.index=s,this.indexes=new Map,this.key=r,this.size=0,this.versions=new Map,this.versioning=i,Object.defineProperty(this,"registry",{enumerable:!0,get:()=>Array.from(this.data.keys())}),this.reindex()}async batch(e,t="set",s=!1){let r;try{const i="del"===t?e=>this.del(e,!0,s):e=>this.set(null,e,!0,!0,s);r=await Promise.all(this.beforeBatch(e,t).map(i)),r=this.onbatch(r,t)}catch(e){throw this.onerror("batch",e),e}return r}beforeBatch(e){return e}beforeClear(){}beforeDelete(){}beforeSet(){}clear(){return this.beforeClear(),this.size=0,this.data.clear(),this.indexes.clear(),this.versions.clear(),this.reindex().onclear(),this}del(e,t=!1,s=!1,r=!1){if(!1===this.has(e))throw new Error("Record not found");const n=this.get(e,!0);this.beforeDelete(e,t,s,r),i(this.index,this.indexes,this.delimiter,e,n),this.data.delete(e),--this.size,this.ondelete(e,t,r,s),this.versioning&&this.versions.delete(e)}dump(e="records"){let t;return t="records"===e?Array.from(this.entries()):Array.from(this.indexes).map((e=>(e[1]=Array.from(e[1]).map((e=>(e[1]=Array.from(e[1]),e))),e))),t}entries(){return this.data.entries()}find(e={},t=!1){const s=Object.keys(e).sort(((e,t)=>e.localeCompare(t))).join(this.delimiter),i=this.indexes.get(s)||new Map;let n=[];if(i.size>0){const a=r(s,this.delimiter,e);n=Array.from(a.reduce(((e,t)=>(i.has(t)&&i.get(t).forEach((t=>e.add(t))),e)),new Set)).map((e=>this.get(e,t)))}return t?n:this.list(...n)}filter(e=(()=>{}),t=!1){const s=t?(e,t)=>t:(e,t)=>Object.freeze([e,Object.freeze(t)]),r=this.reduce(((t,r,i,n)=>(e.call(n,r)&&t.push(s(i,r)),t)),[]);return t?r:Object.freeze(r)}forEach(e,s){return this.data.forEach(((s,r)=>e(t(s),t(r))),s||this.data),this}get(e,s=!1){const r=t(this.data.get(e)||null);return s?r:this.list(e,r)}has(e){return this.data.has(e)}keys(){return this.data.keys()}limit(e=0,t=0,s=!1){const r=this.registry.slice(e,e+t).map((e=>this.get(e,s)));return s?r:this.list(...r)}list(...e){return Object.freeze(e.map((e=>Object.freeze(e))))}map(e,t=!1){const s=[];return this.forEach(((t,r)=>s.push(e(t,r)))),t?s:this.list(...s)}onbatch(e){return e}onclear(){}ondelete(){}onerror(){}onset(){}async override(e,t="records"){if("indexes"===t)this.indexes=new Map(e.map((e=>[e[0],new Map(e[1].map((e=>[e[0],new Set(e[1])])))])));else{if("records"!==t)throw new Error("Invalid type");this.indexes.clear(),this.data=new Map(e),this.size=this.data.size}return!0}reduce(e,t,s=!1){let r=t||this.data.keys().next().value;return this.forEach(((t,i)=>{r=e(r,t,i,this,s)}),this),r}reindex(e){const t=e?[e]:this.index;return e&&!1===this.index.includes(e)&&this.index.push(e),s(t,(e=>this.indexes.set(e,new Map))),this.forEach(((e,r)=>s(t,(t=>h(this.index,this.indexes,this.delimiter,r,e,t))))),this}search(e,t,r=!1){const i=new Map,n="function"==typeof e,a=e&&"function"==typeof e.test;return e&&s(t?Array.isArray(t)?t:[t]:this.index,(t=>{let s=this.indexes.get(t);s&&s.forEach(((s,h)=>{switch(!0){case n&&e(h,t):case a&&e.test(Array.isArray(h)?h.join(", "):h):case h===e:s.forEach((e=>{!1===i.has(e)&&this.has(e)&&i.set(e,this.get(e,r))}))}}))})),r?Array.from(i.values()):this.list(...Array.from(i.values()))}async set(e,s,r=!1,a=!1,c=!1,d=!1){let l,u,f=t(s);return null==e&&(e=this.key&&void 0!==f[this.key]?f[this.key]:o()),this.beforeSet(e,s,r,a,c,d),!1===this.has(e)?(++this.size,this.versioning&&this.versions.set(e,new Set)):(l=this.get(e,!0),i(this.index,this.indexes,this.delimiter,e,l),this.versioning&&this.versions.get(e).add(Object.freeze(t(l))),!1===a&&(f=n(t(l),f))),this.data.set(e,f),h(this.index,this.indexes,this.delimiter,e,f,null),u=this.get(e),this.onset(u,r,d,c),u}sort(e,t=!0){return t?Object.freeze(this.limit(0,this.size,!0).sort(e).map((e=>Object.freeze(e)))):this.limit(0,this.size,!0).sort(e)}sortBy(e,t=!1){const r=[],i=[];let n;return!1===this.indexes.has(e)&&this.reindex(e),n=this.indexes.get(e),n.forEach(((e,t)=>i.push(t))),s(i.sort(),(e=>n.get(e).forEach((e=>r.push(this.get(e,t)))))),t?r:this.list(...r)}toArray(e=!0){const t=Array.from(this.data.values());return e&&(s(t,(e=>Object.freeze(e))),Object.freeze(t)),t}values(){return this.data.values()}where(e,t=!1,s="||"){const r=this.index.filter((t=>t in e));return r.length>0?this.filter(new Function("a",`return (${r.map((t=>{let r;if(Array.isArray(e[t]))r=`Array.isArray(a['${t}']) ? ${e[t].map((e=>`a['${t}'].includes(${"string"==typeof e?`'${e}'`:e})`)).join(` ${s} `)} : (${e[t].map((e=>`a['${t}'] === ${"string"==typeof e?`'${e}'`:e}`)).join(` ${s} `)})`;else if(e[t]instanceof RegExp)r=`Array.isArray(a['${t}']) ? a['${t}'].filter(i => ${e[t]}.test(a['${t}'])).length > 0 : ${e[t]}.test(a['${t}'])`;else{const s="string"==typeof e[t]?`'${e[t]}'`:e[t];r=`Array.isArray(a['${t}']) ? a['${t}'].includes(${s}) : a['${t}'] === ${s}`}return r})).join(") && (")});`),t):[]}}function d(e=null,t={}){const s=new c(t);return Array.isArray(e)&&s.batch(e,"set"),s}export{d as haro};
\No newline at end of file