-
Notifications
You must be signed in to change notification settings - Fork 0
/
objix.min.js
1 lines (1 loc) · 2.8 KB
/
objix.min.js
1
const O=Object,C="constructor",K=O.keys,A=O.assign,I=Symbol.iterator,M={every(t){for(let e in this)if(!t(this[e],e))return!1;return!0},some(t){for(let e in this)if(t(this[e],e))return!0;return!1},map(t,e={}){if(this.map)return this.map(t);for(let i in this)e[i]=t(this[i],i);return e},has(t){return this._some(e=>t==e)},pick(t,e={}){for(let i in this)(t.call?t(this[i],i):t._has(i))&&(e[i]=this[i]);return e},flatMap(t,e={}){for(let i of K(this))for(let[r,s]of t(i,this[i]))e[r]=s;return e},clean(){return this._pick(t=>t)},is(t,e){return e||t!=O?this[C]==t||!e&&this instanceof t:![Number,String,Boolean,Function,Symbol]._has(this[C])},find(t){for(let e in this)if(t.call?t(this[e],e):this[e]._eq(t))return e},extend(...t){return A({},...t)._map((t,e)=>this[e]??t,this)},delete(...t){for(let e of t)delete this[e];return this},clone(t,e){return this._is(O)?!e&&-1==t&&this._len()>10?this._try(global.structuredClone,()=>this._clone(t,1)):this._len()?this._map(e=>e&&t?e._clone(t-1):e):this.map?this:new this[C](this):this.valueOf()},join(...t){let e=A({},this);for(let i of t)K(i)._map(t=>e[t]&&=[].concat(e[t],i[t]));return e},split(t=[]){for(let e in this)this[e]._map((i,r)=>t[r]?t[r][e]=i:t[r]={[e]:i});return t},same(t){return this._pick((e,i)=>e._eq(t[i]))},diff(t){return this._pick((e,i)=>!e._eq(t[i]))},contains(t,e){return t._every((t,e)=>this[e]?._eq(t))||e&&this._some(i=>i._contains(t,e-1))},eq(t,e){return this==t||t&&this._is(t[C])&&this._len()==t._len()&&!(this-t)&&this._every((i,r)=>i==t[r]||e&&i?._eq(t[r],e-1))},len(){return K(this).length},keyBy(t,e,i={}){return this.map(r=>i[e=r._at(t)]=i[e]?[r].concat(i[e]):r),i},at(t){return this[t]||t._split(".")._reduce((t,e)=>t[e],this)},$(t){return t?t._is(String)?t.replace(/\${?([\w\.]+)}?/g,(t,e)=>this._at(e).$()):(t.stringify||t)(this):this._$(JSON).replace(/"(\w+)":/g,"$1:")},memo(t){return t?(...e)=>this[e._$()]??=(this._wait(t).then(t=>delete t[e._$()]),this(...e)):this},bind(t,e,i){return def(this,t,(function(...t){return e(...t,this)})._memo(i)),this},log(t="",e,i="log"){return(!e||e(this))&&console[i](Date().slice(4,24),"-",t,this._$()),this},try(t,e,i,r){try{r=t(this)}catch(s){r=e&&e(s,this)}return i?this:r},new(t){return this._t?new Proxy(this._t._new(t),this._h):A(this._create(),t)},wait(t){return new Promise((e,i)=>t._is(Number)?setTimeout(()=>e(this),1e3*t):(t=t(this,e,i))&&e(t))},trap(t,e,...i){return new Proxy(this,{set(r,s,h){if((!i[0]||i._find(s))&&!t(h,s,r)&&e)throw e+" "+[s,h].$();return r[s]=h},get(t,e){return({_t:t,_h:this})[e]||t[e]}})}};for(let m of["keys","values","entries","create","assign"])M[m]=function(...t){return O[m](this,...t)};let def=(t,e,i)=>t[e]||(O.defineProperty(t,e,{writable:!0,value:i}),i);for(let m in O.prototype[I]=function(){return this._values()[I]()},M){def(O.prototype,"_"+m,M[m]);try{module.exports[m]=(t,...e)=>t["_"+m](...e)}catch{}}