Git Product home page Git Product logo

enum-tool's Introduction

enum-tool

The purpose of this library is resolving the troubled expressions in actual development.

  • It is bother to manage enum and info that is associated with the enum.
  • No Intelligence in traditional coding.

๐Ÿ“• Older Version Doc

๐Ÿš€ Features

  • ๐Ÿ’ช Type Strong: Written in TypeScript
  • ๐Ÿ’ก Code Intelligence: Powered by Typescript

๐Ÿ“ฆ Install

npm i enum-tool

๐Ÿฆ„ Full Usage

import { enumify } from 'enum-tool'

// init enum
// tip: you must use `as const` at the end of array.
//      you can also use /** @type {const} */ in javascript develop environment
const SexEnum = enumify([
  { key: 'MALE', value: 'male', meta: { desc: 'male' } },
  { key: 'FEMALE', value: 'female' },
] as const)

// output: 0
// you can get code intelligence here
console.log(SexEnum.MALE)

const maleVo = SexEnum.get(SexEnum.MALE)

// output: male
// you can get code intelligence here
console.log(maleVo.value)

// you can get all enum options by call `getAll` method
const sexList = SexEnum.getAll()

The SexEnum is equivalent to

const SexEnum: {
  MALE: 'male'
  FEMALE: 'female'
}

The enumify is the core function of the library. It accepts the array of enum info as params. In single enum info obj, the key field is matched with the key of SexEnum, the value is matched with the value of SexEnum. You can set custom data in meta field.

Call SexEnum.get method get the single enum info.

Call SexEnum.all method get the all enum info list.

๐Ÿ‘พ Compare code style with tradition

Not use library

// init enum
const SexEnum = {
  MALE: 'male',
  FEMALE: 'female'
}

// init enum info list
const sexList = [
  { label: 'man', value: SexEnum.MALE },
  { label: 'woman', value: SexEnum.FEMALE },
]

// mock enum value from backend
const unknownSex = 'unknown'

// render info
if (unknownSex === SexEnum.MALE || unknownSex === SexEnum.FEMALE) {
  // tip: there has no code intelligence with maleVo, you can't get female info in coding
  // you need call find method, it is not concision
  const maleVo = sexList.find(item => item.value === unknownSex)
  render(maleVo)
}

Using library

// only init and maintain at here
const SexEnum = enumify([
  { key: 'MALE', value: 'male', meta: { label: 'man' } },
  { key: 'FEMALE', value: 'female', meta: { label: 'woman' } }
] as const)

// mock enum value from backend
const unknownSex = 'unknown'

// render info
if (unknownSex === SexEnum.MALE || unknownSex === SexEnum.FEMALE) {
  // you can get code intelligence here
  const unknownVo = SexEnum.get(unknownSex)
  render(unknownVo)
}

enum-tool's People

Contributors

micaiguai avatar zsyfrontend avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.