npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

enum-value-label

v1.0.6

Published

Enum/Value/Label Management Tools Package(枚举管理工具包)

Downloads

9

Readme

EnumValueLabel

Features

  • 支持Enum、Value互相转换
  • 支持Enum、Value获取Label
  • 支持Value、Label转换成数组
  • 支持TS,全属性自动推导
  • 已通过单测冒烟

为什么使用EnumValueLabel / Why use EnumValueLabel

在日常开发中会大量出现以下情况

  1. 首先创建枚举
    export enum UserType {
         STUDENT = 1, // 学生
         EXPERT,      // 专家
         LECTURER,    // 主讲
         HEAD_TEACHER // 班主任
     }
  2. 之后再根据枚举创建对应映射
    export const userTypeMap = {
        [UserType.STUDENT]: '学生',
        [UserType.EXPERT]: '专家',
        [UserType.LECTURER]: '主讲',
        [UserType.HEAD_TEACHER]: '班主任'
    }
  3. 在需要的场景内进行格式转换(以下拉菜单需要转换成数组举例)
    const userTypeOptions = Object.keys(constLabel.userTypeMap).map((value) => ({
        label: userTypeMap[value],
        value
      }));

这样大量重复的动作不仅浪费时间,而且毫无意义,所以本包出现并解决此问题。

Usage

  1. 安装 install

    npm i enum-value-label -D
  2. 基础使用 EnumValueLabel

    参数:需要定义的集成数组对象,使用时请在定义的结尾使用 as const 以便TS可以推导出类型

    约定:

    1. 传入对象的key为原枚举的key
    2. 传入对象的value是一个有且仅有两项的数组,第一项为原枚举的value,第二项为原映射的value
    import { EnumValueLabel } from 'enum-value-label';
    
    const userTypeEnum = EnumValueLabel({
        STUDENT: [1, '学生'],
        EXPERT: [2, '专家'],
        LECTURER: [3, { zh: '主讲', en: 'lecturer' }],
        HEAD_TEACHER: [4, '班主任']
    } as const); // 使用 as const 用以强调类型
    
    console.log(userTypeEnum['STUDENT'])  // 1 
    console.log(userTypeEnum[2])         // EXPERT
    console.log(userTypeEnum.LECTURER)  // 3

    支持TS推导

    支持TS推导

  3. 内置方法 _label / _l

    参数:传入对象的 keyvalue的第一项

    _label_l 二者效果相同, 仅是缩写关系

    import { EnumValueLabel } from 'enum-value-label';
    
    const userTypeEnum = EnumValueLabel({
        STUDENT: [1, '学生'],
        EXPERT: [2, '专家'],
        LECTURER: [3, { zh: '主讲', en: 'lecturer' }],
        HEAD_TEACHER: [4, '班主任']
    } as const); // 使用 as const 用以强调类型
    
    console.log(userTypeEnum._label(1))  // '学生'
    console.log(userTypeEnum._label("LECTURER"))  // { zh: '主讲', en: 'lecturer' }
    
    console.log(userTypeEnum._l(1))  // '学生'
    console.log(userTypeEnum._l("LECTURER"))  // { zh: '主讲', en: 'lecturer' }

    支持TS推导

    支持TS推导

    支持TS推导

  4. 内置方法 _array / _a

    参数:期望生成的对象数组中对象的key,传入顺序分别对应原对象value的第一项第二项

    _array_a 二者效果相同, 仅是缩写关系

    import { EnumValueLabel } from 'enum-value-label';
    
    const userTypeEnum = EnumValueLabel({
        STUDENT: [1, '学生'],
        EXPERT: [2, '专家'],
        LECTURER: [3, { zh: '主讲', en: 'lecturer' }],
        HEAD_TEACHER: [4, '班主任']
    } as const); // 使用 as const 用以强调类型
    
    console.log(userTypeEnum._array())  
    // [
    //   { value: 1, label: '学生' },
    //   { value: 2, label: '专家' },
    //   { value: 3, label: { zh: '主讲', en: 'lecturer' } },
    //   { value: 4, label: '班主任' }
    // ]
    
    console.log(userTypeEnum._array('id', 'name'))
    // [
    //   { id: 1, name: '学生' },
    //   { id: 2, name: '专家' },
    //   { id: 3, name: { zh: '主讲', en: 'lecturer' } },
    //   { id: 4, name: '班主任' }
    // ]
    
    // _array 和 _a 二者效果相同, 不再演示

    支持TS推导

    支持TS推导

    支持TS推导

  5. 内置方法 _format / _f

    参数:一个方法用于处理每一项的value 即方法的两个参数分别为原对象value的第一项第二项 详看下面事例

    _format_f 二者效果相同, 仅是缩写关系

    import { EnumValueLabel } from 'enum-value-label';
    
    const userTypeEnum = EnumValueLabel({
        STUDENT: [1, '学生'],
        EXPERT: [2, '专家'],
        LECTURER: [3, { zh: '主讲', en: 'lecturer' }],
        HEAD_TEACHER: [4, '班主任']
    } as const); // 使用 as const 用以强调类型
    
    const testFn = (value, label) => ({ value: label, label: value });
    
    console.log(userTypeEnum._format(testFn)) 
    
    // [
    //   { value: '学生', label: 1 },
    //   { value: '专家', label: 2 },
    //   { value: { zh: '主讲', en: 'lecturer' }, label: 3 },
    //   { value: '班主任', label: 4 }
    // ]
    
    // _format 和 _f 二者效果相同, 不再演示

    支持TS推导

    支持TS推导

未来

如有希望支持的功能和想法可联系作者共同讨论