UNPKG

create-enum-es

Version:

处理前端枚举的工具方法,支持ts类型检测

2 lines (1 loc) 12.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).$Enum=e()}(this,function(){"use strict";function t(t,e){return t===e||t!=t&&e!=e}function e(e,r){for(var n=e.length;n--;)if(t(e[n][0],r))return n;return-1}var r=Array.prototype.splice;function n(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}n.prototype.clear=function(){this.__data__=[],this.size=0},n.prototype.delete=function(t){var n=this.__data__,o=e(n,t);return!(o<0)&&(o==n.length-1?n.pop():r.call(n,o,1),--this.size,!0)},n.prototype.get=function(t){var r=this.__data__,n=e(r,t);return n<0?void 0:r[n][1]},n.prototype.has=function(t){return e(this.__data__,t)>-1},n.prototype.set=function(t,r){var n=this.__data__,o=e(n,t);return o<0?(++this.size,n.push([t,r])):n[o][1]=r,this};var o="object"==typeof global&&global&&global.Object===Object&&global,a="object"==typeof self&&self&&self.Object===Object&&self,c=o||a||Function("return this")(),i=c.Symbol,u=Object.prototype,s=u.hasOwnProperty,f=u.toString,p=i?i.toStringTag:void 0;var l=Object.prototype.toString;var b=i?i.toStringTag:void 0;function y(t){return null==t?void 0===t?"[object Undefined]":"[object Null]":b&&b in Object(t)?function(t){var e=s.call(t,p),r=t[p];try{t[p]=void 0;var n=!0}catch(t){}var o=f.call(t);return n&&(e?t[p]=r:delete t[p]),o}(t):function(t){return l.call(t)}(t)}function _(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}function j(t){if(!_(t))return!1;var e=y(t);return"[object Function]"==e||"[object GeneratorFunction]"==e||"[object AsyncFunction]"==e||"[object Proxy]"==e}var h,v=c["__core-js_shared__"],d=(h=/[^.]+$/.exec(v&&v.keys&&v.keys.IE_PROTO||""))?"Symbol(src)_1."+h:"";var g=Function.prototype.toString;function O(t){if(null!=t){try{return g.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var m=/^\[object .+?Constructor\]$/,w=Function.prototype,A=Object.prototype,x=w.toString,z=A.hasOwnProperty,M=RegExp("^"+x.call(z).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function S(t){return!(!_(t)||(e=t,d&&d in e))&&(j(t)?M:m).test(O(t));var e}function E(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return S(r)?r:void 0}var P=E(c,"Map"),F=E(Object,"create");var U=Object.prototype.hasOwnProperty;var I=Object.prototype.hasOwnProperty;function T(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function k(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function B(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}T.prototype.clear=function(){this.__data__=F?F(null):{},this.size=0},T.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},T.prototype.get=function(t){var e=this.__data__;if(F){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return U.call(e,t)?e[t]:void 0},T.prototype.has=function(t){var e=this.__data__;return F?void 0!==e[t]:I.call(e,t)},T.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=F&&void 0===e?"__lodash_hash_undefined__":e,this},B.prototype.clear=function(){this.size=0,this.__data__={hash:new T,map:new(P||n),string:new T}},B.prototype.delete=function(t){var e=k(this,t).delete(t);return this.size-=e?1:0,e},B.prototype.get=function(t){return k(this,t).get(t)},B.prototype.has=function(t){return k(this,t).has(t)},B.prototype.set=function(t,e){var r=k(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};function $(t){var e=this.__data__=new n(t);this.size=e.size}$.prototype.clear=function(){this.__data__=new n,this.size=0},$.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},$.prototype.get=function(t){return this.__data__.get(t)},$.prototype.has=function(t){return this.__data__.has(t)},$.prototype.set=function(t,e){var r=this.__data__;if(r instanceof n){var o=r.__data__;if(!P||o.length<199)return o.push([t,e]),this.size=++r.size,this;r=this.__data__=new B(o)}return r.set(t,e),this.size=r.size,this};var D=function(){try{var t=E(Object,"defineProperty");return t({},"",{}),t}catch(t){}}();var L=Object.prototype.hasOwnProperty;function R(e,r,n){var o=e[r];L.call(e,r)&&t(o,n)&&(void 0!==n||r in e)||function(t,e,r){"__proto__"==e&&D?D(t,e,{configurable:!0,enumerable:!0,value:r,writable:!0}):t[e]=r}(e,r,n)}function V(t){return null!=t&&"object"==typeof t}function C(t){return V(t)&&"[object Arguments]"==y(t)}var N=Object.prototype,W=N.hasOwnProperty,q=N.propertyIsEnumerable,G=C(function(){return arguments}())?C:function(t){return V(t)&&W.call(t,"callee")&&!q.call(t,"callee")},H=Array.isArray;var J="object"==typeof exports&&exports&&!exports.nodeType&&exports,K=J&&"object"==typeof module&&module&&!module.nodeType&&module,Q=K&&K.exports===J?c.Buffer:void 0,X=(Q?Q.isBuffer:void 0)||function(){return!1},Y=/^(?:0|[1-9]\d*)$/;function Z(t,e){var r=typeof t;return!!(e=null==e?9007199254740991:e)&&("number"==r||"symbol"!=r&&Y.test(t))&&t>-1&&t%1==0&&t<e}function tt(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=9007199254740991}var et={};function rt(t){return function(e){return t(e)}}et["[object Float32Array]"]=et["[object Float64Array]"]=et["[object Int8Array]"]=et["[object Int16Array]"]=et["[object Int32Array]"]=et["[object Uint8Array]"]=et["[object Uint8ClampedArray]"]=et["[object Uint16Array]"]=et["[object Uint32Array]"]=!0,et["[object Arguments]"]=et["[object Array]"]=et["[object ArrayBuffer]"]=et["[object Boolean]"]=et["[object DataView]"]=et["[object Date]"]=et["[object Error]"]=et["[object Function]"]=et["[object Map]"]=et["[object Number]"]=et["[object Object]"]=et["[object RegExp]"]=et["[object Set]"]=et["[object String]"]=et["[object WeakMap]"]=!1;var nt="object"==typeof exports&&exports&&!exports.nodeType&&exports,ot=nt&&"object"==typeof module&&module&&!module.nodeType&&module,at=ot&&ot.exports===nt&&o.process,ct=function(){try{var t=ot&&ot.require&&ot.require("util").types;return t||at&&at.binding&&at.binding("util")}catch(t){}}(),it=ct&&ct.isTypedArray,ut=it?rt(it):function(t){return V(t)&&tt(t.length)&&!!et[y(t)]},st=Object.prototype.hasOwnProperty;function ft(t,e){var r=H(t),n=!r&&G(t),o=!r&&!n&&X(t),a=!r&&!n&&!o&&ut(t),c=r||n||o||a,i=c?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],u=i.length;for(var s in t)!st.call(t,s)||c&&("length"==s||o&&("offset"==s||"parent"==s)||a&&("buffer"==s||"byteLength"==s||"byteOffset"==s)||Z(s,u))||i.push(s);return i}var pt=Object.prototype;function lt(t){var e=t&&t.constructor;return t===("function"==typeof e&&e.prototype||pt)}function bt(t,e){return function(r){return t(e(r))}}var yt=bt(Object.keys,Object),_t=Object.prototype.hasOwnProperty;function jt(t){if(!lt(t))return yt(t);var e=[];for(var r in Object(t))_t.call(t,r)&&"constructor"!=r&&e.push(r);return e}function ht(t){return null!=t&&tt(t.length)&&!j(t)}function vt(t){return ht(t)?ft(t):jt(t)}var dt="object"==typeof exports&&exports&&!exports.nodeType&&exports,gt=dt&&"object"==typeof module&&module&&!module.nodeType&&module,Ot=gt&&gt.exports===dt?c.Buffer:void 0;Ot&&Ot.allocUnsafe;var mt=Object.prototype.propertyIsEnumerable,wt=Object.getOwnPropertySymbols,At=wt?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,a=[];++r<n;){var c=t[r];e(c,r,t)&&(a[o++]=c)}return a}(wt(t),function(e){return mt.call(t,e)}))}:function(){return[]};var xt=bt(Object.getPrototypeOf,Object);function zt(t){return function(t,e,r){var n=e(t);return H(t)?n:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(n,r(t))}(t,vt,At)}var Mt=E(c,"DataView"),St=E(c,"Promise"),Et=E(c,"Set"),Pt=E(c,"WeakMap"),Ft="[object Map]",Ut="[object Promise]",It="[object Set]",Tt="[object WeakMap]",kt="[object DataView]",Bt=O(Mt),$t=O(P),Dt=O(St),Lt=O(Et),Rt=O(Pt),Vt=y;(Mt&&Vt(new Mt(new ArrayBuffer(1)))!=kt||P&&Vt(new P)!=Ft||St&&Vt(St.resolve())!=Ut||Et&&Vt(new Et)!=It||Pt&&Vt(new Pt)!=Tt)&&(Vt=function(t){var e=y(t),r="[object Object]"==e?t.constructor:void 0,n=r?O(r):"";if(n)switch(n){case Bt:return kt;case $t:return Ft;case Dt:return Ut;case Lt:return It;case Rt:return Tt}return e});var Ct=Object.prototype.hasOwnProperty;var Nt=c.Uint8Array;function Wt(t){var e=new t.constructor(t.byteLength);return new Nt(e).set(new Nt(t)),e}var qt=/\w*$/;var Gt=i?i.prototype:void 0,Ht=Gt?Gt.valueOf:void 0;function Jt(t,e,r){var n,o,a,c,i,u=t.constructor;switch(e){case"[object ArrayBuffer]":return Wt(t);case"[object Boolean]":case"[object Date]":return new u(+t);case"[object DataView]":return i=Wt((c=t).buffer),new c.constructor(i,c.byteOffset,c.byteLength);case"[object Float32Array]":case"[object Float64Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":return function(t){var e=Wt(t.buffer);return new t.constructor(e,t.byteOffset,t.length)}(t);case"[object Map]":case"[object Set]":return new u;case"[object Number]":case"[object String]":return new u(t);case"[object RegExp]":return(a=new(o=t).constructor(o.source,qt.exec(o))).lastIndex=o.lastIndex,a;case"[object Symbol]":return n=t,Ht?Object(Ht.call(n)):{}}}var Kt=Object.create,Qt=function(){function t(){}return function(e){if(!_(e))return{};if(Kt)return Kt(e);t.prototype=e;var r=new t;return t.prototype=void 0,r}}();var Xt=ct&&ct.isMap,Yt=Xt?rt(Xt):function(t){return V(t)&&"[object Map]"==Vt(t)};var Zt=ct&&ct.isSet,te=Zt?rt(Zt):function(t){return V(t)&&"[object Set]"==Vt(t)},ee="[object Arguments]",re="[object Function]",ne="[object Object]",oe={};function ae(t,e,r,n,o,a){var c;if(void 0!==c)return c;if(!_(t))return t;var i=H(t);if(i)c=function(t){var e=t.length,r=new t.constructor(e);return e&&"string"==typeof t[0]&&Ct.call(t,"index")&&(r.index=t.index,r.input=t.input),r}(t);else{var u=Vt(t),s=u==re||"[object GeneratorFunction]"==u;if(X(t))return t.slice();if(u==ne||u==ee||s&&!o)c=s?{}:function(t){return"function"!=typeof t.constructor||lt(t)?{}:Qt(xt(t))}(t);else{if(!oe[u])return o?t:{};c=Jt(t,u)}}a||(a=new $);var f=a.get(t);if(f)return f;a.set(t,c),te(t)?t.forEach(function(n){c.add(ae(n,e,r,n,t,a))}):Yt(t)&&t.forEach(function(n,o){c.set(o,ae(n,e,r,o,t,a))});var p=i?void 0:zt(t);return function(t,e){for(var r=-1,n=null==t?0:t.length;++r<n&&!1!==e(t[r],r,t););}(p||t,function(n,o){p&&(n=t[o=n]),R(c,o,ae(n,e,r,o,t,a))}),c}oe[ee]=oe["[object Array]"]=oe["[object ArrayBuffer]"]=oe["[object DataView]"]=oe["[object Boolean]"]=oe["[object Date]"]=oe["[object Float32Array]"]=oe["[object Float64Array]"]=oe["[object Int8Array]"]=oe["[object Int16Array]"]=oe["[object Int32Array]"]=oe["[object Map]"]=oe["[object Number]"]=oe[ne]=oe["[object RegExp]"]=oe["[object Set]"]=oe["[object String]"]=oe["[object Symbol]"]=oe["[object Uint8Array]"]=oe["[object Uint8ClampedArray]"]=oe["[object Uint16Array]"]=oe["[object Uint32Array]"]=!0,oe["[object Error]"]=oe[re]=oe["[object WeakMap]"]=!1;var ce=Object.prototype.hasOwnProperty;var ie=Function.prototype,ue=Object.prototype,se=ie.toString,fe=ue.hasOwnProperty,pe=se.call(Object);function le(t){return!function(t){if(null==t)return!0;if(ht(t)&&(H(t)||"string"==typeof t||"function"==typeof t.splice||X(t)||ut(t)||G(t)))return!t.length;var e=Vt(t);if("[object Map]"==e||"[object Set]"==e)return!t.size;if(lt(t))return!jt(t).length;for(var r in t)if(ce.call(t,r))return!1;return!0}(t)&&t.every(t=>"string"==typeof t)}class be{__enumMap__;__enumLabelMap__;__enumExtraMap__;constructor(t){if(!function(t){if(!V(t)||"[object Object]"!=y(t))return!1;var e=xt(t);if(null===e)return!0;var r=fe.call(e,"constructor")&&e.constructor;return"function"==typeof r&&r instanceof r&&se.call(r)==pe}(t))throw new TypeError("初始化参数值必须是一个object!");this.#t(ae(t,5))}#t(t){const e={},r={};Object.keys(t).forEach(n=>{const o=t[n];if(!H(o))throw new TypeError("初始化参数对象字段的值必是一个array!");this[n]=o[0],e[n]=o[1],e[o[0]]=o[1],r[n]=o[2],r[o[0]]=o[2]}),this.__enumMap__=Object.freeze(t),this.__enumLabelMap__=Object.freeze(e),this.__enumExtraMap__=Object.freeze(r)}value(t){return this[t]}values(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>this.value(t))}options(...t){let e=Object.keys(this.__enumMap__);return le(t)&&(e=Array.from(t)),e.map(t=>({value:this.value(t),label:this.label(t),extra:this.extra(t)}))}label(t){return this.__enumLabelMap__[t]}extra(t){return this.__enumExtraMap__[t]}check(t,e){return this.value(e)===t}}return t=>{const e=new be(t);return Object.freeze(e)}});