UNPKG

imba

Version:

Intuitive and powerful language for building webapps that fly

2 lines (1 loc) 5.88 kB
var d=Symbol.for("#__init__"),R=Symbol.for("#__initor__");var F=Symbol.for("#meta"),x=Symbol.for("imba");var q={SUPERCALLS:1<<3,CONSTRUCTOR:1<<4},a={IsExtension:1<<0,IsTag:1<<1,HasDescriptors:1<<2,HasSuperCalls:1<<3,HasConstructor:1<<4,HasFields:1<<5,HasMixins:1<<6,HasInitor:1<<7,HasDecorators:1<<8,IsObjectExtension:1<<9,IsMixin:1<<10},m=new Map,T=globalThis[x]||(globalThis[x]={counter:0,classes:m});function g(e,t={}){return m.has(e)||m.set(e,{symbol:Symbol(e.name),parent:Object.getPrototypeOf(e.prototype)?.constructor,for:e,uses:null,inits:null,id:T.counter++,...t}),m.get(e)}function _(e,t){return typeof t=="string"?typeof e===t:t?.[Symbol.hasInstance]?.(e)}function j(e){return e?.toIterable?.()||e}function E(e,t){if(!e||!t)return!1;if(e.get)return t.get===e.get;if(e.set)return t.set===e.set;if(e.value)return e.value===t.value}function C(e,t,r,n={}){let o=e.constructor;!r&&t&&(r=Object.getOwnPropertyDescriptors(t),delete r.constructor,r[d]&&(console.warn(`Cannot define plain fields when extending class ${o.name}`),delete r[d]));let i=g(o);if(i&&i.augments){let u=new Map;for(let s of Object.keys(r)){let l=Object.getOwnPropertyDescriptor(e,s);for(let f of i.augments){let h=u.get(f);h||u.set(f,h={});let y=Object.getOwnPropertyDescriptor(f.prototype,s);y&&!E(l,y)?console.warn("wont extend",s,y,l):h[s]=r[s]}}for(let[s,l]of u)Object.keys(l).length&&C(s.prototype,null,l)}return Object.defineProperties(e,r),e}function b(e,t){let r=g(e),n=g(t),o=n.parent;for(;o&&g(o)?.flags&a.IsMixin;){b(e,o),o=null;break}if(o&&!(e.prototype instanceof o))throw new Error(`Mixin ${t.name} has superclass not present in target class`);if(!n.augments){n.augments=new Set;let u=n.ref=Symbol(t.name),s=Object[Symbol.hasInstance];t.prototype[u]=!0,Object.defineProperty(t,Symbol.hasInstance,{value:function(l){return this===t?l&&!!l[u]:s.call(this,l)}})}if(e.prototype[n.ref])return e;if(n.uses)for(let u of n.uses)b(e,u);n.augments.add(e),r.uses||(r.uses=[]),r.uses.push(t);let i=Object.getOwnPropertyDescriptors(t.prototype);return delete i.constructor,i[d]&&(r.inits||(r.inits=[]),r.inits.push(t.prototype[d]),delete i[d]),Object.defineProperties(e.prototype,i),t?.mixed instanceof Function&&t.mixed(e),e}var p={cache:{},self:null,target:null,proxy:new Proxy({},{apply:(e,t,...r)=>p.target[t].apply(p.self,r),get:(e,t)=>Reflect.get(p.target,t,p.self),set:(e,t,r,n)=>Reflect.set(p.target,t,r,p.self)})};function c(e,t,r,n,o=null){let i=Object.getPrototypeOf(e.prototype),u=(n&(a.HasMixins|a.IsExtension))==a.HasMixins,s;if(u&&(m.set(e,m.get(i.constructor)),i=Object.getPrototypeOf(i)),o){let f=n&a.IsObjectExtension?o:o.prototype,h=g(e);if(h.uses){o===f&&console.warn("Cannot extend object with mixins");for(let y of h.uses)b(o,y)}return n&a.HasSuperCalls&&(p.cache[t]=Object.create(Object.getPrototypeOf(f),Object.getOwnPropertyDescriptors(f))),C(f,e.prototype),o}let l=i?.constructor;if(s=g(e,{symbol:t}),Object.defineProperty(e,F,{value:s,enumerable:!1,configurable:!0}),r&&e.name!==r&&Object.defineProperty(e,"name",{value:r,configurable:!0}),s.flags=n,n&a.HasConstructor&&(e.prototype[R]=t),s.uses)for(let f of s.uses)f.mixes?.(e);return l?.inherited instanceof Function&&l.inherited(e),e}var L=Symbol(),$=class{get size(){return this.length}set size(t){this.length=t}get emptyΦ(){return this.size===0}get anyΦ(){return this.size>0}get manyΦ(){return this.size>1}get oneΦ(){return this.size===1}get random(){return this[Math.floor(Math.random()*this.length)]}clear(){return this.length=0,this}async resolve(){return await Promise.all(this)}get unique(){let t=[];for(let r=0,n=j(this),o=n.length;r<o;r++){let i=n[r];t.indexOf(i)==-1&&t.push(i)}return t}add(t){return this.has(t)||this.push(t),t}has(t){return this.includes(t)}sorted(t,r=!1){let n=this.slice(),o=t;return typeof t=="string"?o=function(i){return i[t]}:_(t,Map)?o=function(i){return t.get(i)}:_(t,Function)||(o=function(i){return i}),r?n.sort(function(i,u){let s=o(i),l=o(u);return s<l?1:s>l?-1:0}):n.sort(function(i,u){let s=o(i),l=o(u);return s>l?1:s<l?-1:0})}delete(t){let r=this.indexOf(t);return r<0?!1:(this.splice(r,1),!0)}},H=$;(()=>{c($,L,"Array",1,Array)})();globalThis.sleep=function(e=0,t){return new Promise(function(r){return setTimeout(function(){return t&&t(),r()},e)})};Math.randΞint=function(e,t){return e=Math.ceil(e),t=Math.floor(t),Math.floor(Math.random()*(t-e+1)+e)};Math.randΞfloat=function(e,t){return Math.random()*(t-e)+e};Math.randΞbool=function(){return Math.random()<.5};var Z=Symbol(),O=class{to(t){var r=this;return Array.from({length:t-this+1},function(n,o){return r+o})}floor(){return Math.floor(this)}ceil(){return Math.ceil(this)}times(t){let r=0,n=this;for(;r<n;)t(r++)}round(t=1){if(t!=1){let r=1/t;return Math.round(this*r)/r}return Math.round(this)}clamp(t,r){return typeof r!="number"?Math.max(this,t):Math.min(typeof t!="number"?this:Math.max(this,t),r)}lerp(t,r,n=0,o=1){let i=(this-t)/(r-t);return n+i*(o-n)}},I=O;(()=>{c(O,Z,"Number",1,Number)})();var K=Symbol(),S=class{filter(t){return new Set(Array.from(this).filter(t))}},v=S;(()=>{c(S,K,"Set",1,Set)})();var N=Symbol(),M=class{setnx(t,r){return this.get(t)??(this.set(t,r)&&r)}},z=M;(()=>{c(M,N,"WeakMap",1,WeakMap)})();var U=Symbol(),w=class{setnx(t,r){return this.get(t)??(this.set(t,r)&&r)}},D=w;(()=>{c(w,U,"Map",1,Map)})();var V=Symbol(),P=class{get upperΦ(){return/^[A-Z][A-Z_\d]*$/.test(this)}get numΦ(){return this.intΦ||this.floatΦ}get intΦ(){return/^-?(0|[1-9]\d*)$/.test(this)}get floatΦ(){return/^-?\d+\.\d+$/.test(this)}get pascalΦ(){return/^[A-Z]+[a-z]+[A-Za-z]*/.test(this)}get slug(){let t=this.normalize("NFKD");return t=t.replace(/[\u0300-\u036f]/g,""),t=t.trim(),t=t.toLowerCase(),t=t.replace(/[^a-z0-9 -]/g,""),t=t.replace(/\s+/g,"-"),t=t.replace(/-+/g,"-"),t}pluck(t){let r=this.match(t),n=r&&r.length;return n>2?r.slice(1):r?r[n-1]:null}},A=P;(()=>{c(P,V,"String",1,String)})();