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

xy-enum

v1.4.4

Published

枚举

Downloads

47

Readme

XYEnum 枚举

安装

使用 npm:

npm install xy-enum -S

使用 jsDelivr CDN:


<script src="https://cdn.jsdelivr.net/npm/xy-enum/dist/index.min.js"></script>

示例

基础用法

import XYEnum from "xy-enum";

const myEnum = new XYEnum([
    { key: "key1", value: 1, desc: "选项1" },
    { key: "key2", value: 2, desc: "选项2" },
    { key: "key3", value: 3, desc: "选项3" },
    { key: "key4", value: 4, desc: "选项4" }
]);

// 通过 value 获取 key
myEnum.getKey(1);

// 通过 key 获取 value
myEnum.getValue('key1');

// 通过 key 获取 desc
myEnum.getDesc("key1");

// 通过 value 获取 desc
myEnum.getDesc(1);

// 通过 key 获取 record
myEnum.get(1);

// 通过 value 获取 record
myEnum.get("key1");

// 通过 options
myEnum.getOptions();

// 获取列表
myEnum.getList();

// 判断 key 是否存在
myEnum.has("key1");

// 判断 value 是否存在
myEnum.has(1);

// 对比
myEnum.is(1, "key1");

// 对比多个
myEnum.is([1, 2, 3], "key1");

自定义字段名

import XYEnum from "xy-enum";

const myEnum = new XYEnum(
    [
        { key: "key1", value: 1, desc: "选项1" },
        { key: "key2", value: 2, desc: "选项2" },
        { key: "key3", value: 3, desc: "选项3" },
        { key: "key4", value: 4, desc: "选项4" }
    ]);

const options = myEnum.getOptions({
    id: "value",
    title: "desc"
});

API

参数

| 名称 | 说明 | 类型 | 默认值 | |------|-----------------------|---------|-----| | data | 枚举数据,详细结构见下表 | array | - |

Data

以下三个字段为必填项

| 名称 | 说明 | 类型 | |-------|-------------------------------|-------------------| | key | 通过 key 可以获取对应数据 | number string | | value | 通过 value 可以获取对应数据 | number string | | desc | 描述,在 getOptions 中会作为 label 返回 | number string | | ... | 其他字段根据实际业务场景定义 | ... |

方法

get(val)

getKey(val)

getValue(val)

getDesc(val)

getOptions([config])

getList()

is(val1, val2)

has(val)