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

@kamuisdo/enum-helper-2

v1.2.0

Published

provide multi function to manage enum in front-end project

Downloads

1

Readme

enum-helper

Install

npm i @kamuisdo/enum-helper --save
pnpm add @kamuisdo/enum-helper

Introduce

  • 封装枚举类型的对象,提供多种常用的方法,如根据枚举值获取整个枚举对象
  • 兼容多种类型的属性值

How to use

import EnumHelper from '@kamuisdo/enum-helper'

// 实例化枚举对象
const myEnum =  new EnumHelper([
	{ id: 1, value: 'a' },
	{ id: 2, value: 'b' },
])

myEnum.getById(1)	// { id: 1, value: 'a' }
myEnum.getByValue('a')	// { id: 1, value: 'a' }

实例化

  • new EnumHelper 接受一个item数组作为构造函数的参数,item对象中的idvalue为必填项,可自定义其他属性
type plantType = number | string | boolean
type item = {
	id: plantType
	value: plantType
	[key: string]: any
}
  • id重复时会抛出错误

API

  • toObjectArray()
    • 返回整个item数组
  • getById(id)
    • 根据id返回item,如果id没有找到,返回undefined;如果id是null/NaN/undefined,返回undefined
  • getByValue(value)
    • 根据value返回item
  • getBy(key, keyValue)
    • 根据属性key获取item
const myEnum = new EnumHelper([
	{
		id: 1,
		value: 'apple',
		color: 'red',
		share: () => {
			return 'give you an apple'
		},
	}
])
myEnum.getByValue('color','red')	// { id:1, value: 'apple', color ... }
  • toArray(key)
    • 根据属性key获取对应值的数组
const myEnum = new EnumHelper([
	{ id: 1, value: 'a' },
	{ id: 2, value: 'b' },
])
myEnum.toArray('value')	// ['a','b']
  • toAntdSelectOptions()
    • 返回适用于AntdSelectOptions默认配置的数组
const myEnum = new EnumHelper([
	{ id: 1, value: 'a' },
	{ id: 2, value: 'b' },
])
myEnum.toAntdSelectOptions() // { value: 1, label: 'a' },{ value: 2, label: 'b' }