enum
install
- npm
- pnpm
- yarn
npm istall @jelper/enum
pnpm istall @jelper/enum
yarn istall @jelper/enum
样例
import EnumHelper from '@jelper/enum';
const enumHelper = new EnumHelper([
{ label: 'A', key: 'a', value: 1 },
{ label: 'B', key: 'b', value: 2 },
{ label: 'C', key: 'c', value: 3 },
]);
console.log(enumHelper.get(1)); // { label: 'A', key: 'a', value: 1 },
console.log(enumHelper.getLabel('a')); // A
// 拓展
const enumHelperSuper = EnumHelper.build([
{ label: 'A', key: 'a', value: 1 },
{ label: 'B', key: 'b', value: 2 },
{ label: 'C', key: 'c', value: 3 },
], {
getName() { return 'enumHelperSuper';}
}); // 或者使用继承
console.log(enumHelper.getName()); // enumHelperSuper
Api
EnumHelper
实例属性
属性 | 类型 | 说明 |
---|---|---|
constructor | (list: EnumItem[]) => void | - |
valueOf | () => EnumItem[] | - |
getData | () => EnumItem[] | - |
get | (key: string, value?: EnumItem['value']) => EnumItem | undefined | - |
getLabel | (key: string) => EnumItem['label'] | undefined | - |
getValue | (key: string) => EnumItem['value'] | undefined | - |
静态属性
属性 | 类型 | 说明 |
---|---|---|
build | (list: EnumItem[], extendsObj: Object ) => EnumHelper | - |
Interface
interface EnumItem {
label: string;
key: string;
value?: number | string | boolean | null;
[prop: string]: any;
}