e-enum
Version:
ts enum object, 带typescript签名的多字段枚举关联对象映射
2 lines (1 loc) • 1.43 kB
JavaScript
var d=function(){let s={};for(let o=0;o<arguments.length;o++){let y=arguments[o];if(y!=null)for(let i in y)Object.prototype.hasOwnProperty.call(y,i)&&(s[i]=y[i])}return s};function p(r){return Object.prototype.toString.call(r)==="[object Array]"}function O(r,s){for(let o=0;o<r.length;o++)if(r[o]===s)return!0;return!1}var f=["$list","$map","$options"],m=["$is","$eq","$in"];function E(r){var i,a,c;let s={},o={};for(let t in r)if(Object.prototype.hasOwnProperty.call(r,t)){let n=r[t];if(O(f,t))throw new Error(`The built-in property "${t}" cannot be used!`);if(!n||!("code"in n))throw new Error("Code must be specified!");for(let e=0;e<m.length;e++)if(m[e]in n)throw new Error(`The built-in property "${m[e]}" cannot be used!`);if(o[n.code])throw new Error(`The code "${(c=(a=(i=n.code)==null?void 0:i.toString)==null?void 0:a.call(i))!=null?c:""}" has been used!`);let u=d(n,{$eq:e=>e===n.code,$is:e=>e===t,$in:e=>O(e,t)});s[t]=u,o[n.code]=u}return d(s,o,{$list(t=[]){let n=[],l=p(t)?t:[t];for(let u in s)if(Object.prototype.hasOwnProperty.call(s,u)){let e=s[u];!e.$exclude&&!e.$in(l)&&n.push(e)}return n.sort((u,e)=>(u.$sort||0)-(e.$sort||0))},$map(t,n=[]){if(typeof t!="function")throw new Error("is not a function!");let l=[],u=this.$list(n);for(let e=0;e<u.length;e++)l.push(t(u[e]));return l},$options(t=[]){return this.$map(n=>({label:n.label||String(n.code),value:n.code}),t)}})}var C=E;export{C as default,E as eEnum};