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

@cailiao/enum

v0.2.0

Published

一个适用于 JavaScript 和 TypeScript 的拓展枚举类型。

Downloads

4

Readme

枚举类型的拓展类

这是一个适用于 js 或 ts 的枚举类型的拓展类,可以让你方便地创建和使用枚举类型的实例。

安装

你可以使用npm或yarn来安装这个包:

npm install @cailiao/enum
# or
yarn add @cailiao/enum

使用

你可以使用import或require来引入这个包:

import Enum from "@cailiao/enum";
// or
const Enum = require("@cailiao/enum");

然后,你可以使用new关键字来创建一个枚举类型的实例,传入一个对象或者数组作为枚举范围:

  • enum
// 传入一个对象作为枚举范围
const color = new Enum({ red: "red", green: "green", blue: "blue" });
// 传入一个数组作为枚举范围
const direction = new Enum(["up", "down", "left", "right"]);
// 传入一个ts的enum类型作为枚举范围
enum Gender {
  Male,
  Female,
}
const gender = new Enum(Gender);

你也可以传入一个可选的第二个参数,作为实例的默认值,默认值必须被包含在枚举范围之内: 如果缺省默认值,则会将会以第一个参数的第一个字段作为默认值。

  • default Any 可选(optional)
// 传入一个默认值
const color = new Enum({ red: "red", green: "green", blue: "blue" }, "blue");
console.log(color.value); // blue

// 使用缺省值
const color = new Enum({ red: "red", green: "green", blue: "blue" });
console.log(color.value); // red

你还可以传入一个可选的第三个参数,作为实例创建时的配置项:

  • options Object 可选(optional)

    • extensible Boolean

      设置实例是否为可拓展,即是否可以修改实例定义。

      // 允许实例可以拓展
      const color = new Enum(
        { red: "red", green: "green", blue: "blue" }, 
        undefined,
        { extensible: true }
      );
      console.log(color.value); // red
      // 设置可拓展后可以动态修改枚举范围
      color.__enum__.black = 'black'
      color.value = color.black
      console.log(color.value); // black
      
      // Vue@2 中 observable 方法只能监视可拓展对象
      Vue.observable(new Enum([1, 2], 1, { extensible: true }))

属性和方法

每个枚举类型的实例都有以下属性和方法:

value

这是一个特征属性,表示实例的当前值。你可以给这个属性赋予枚举范围以内的任意值。如果赋予枚举范围之外的值,会抛出一个TypeError。

const color = new Enum({ red: "red", green: "green", blue: "blue" });
color.value = "green"; // ok
color.value = "yellow"; // TypeError: Invalid value for enum type

enum

这是一个隐藏属性,存储了传入构造函数的第一个参数的原始值,以便在需要时访问。

const color = new Enum({ red: "red", green: "green", blue: "blue" });
console.log(color.__enum__); // { red: 'red', green: 'green', blue: 'blue' }

枚举范围内的属性

你可以直接在实例上以访问属性的方式访问传入构造函数的第一个参数中的属性,但是注意一个参数中的属性应避免包含value这个键,否则将无法从实例上直接访问本来的枚举值,会被特征属性value覆盖,但这个键的值仍会包含在枚举范围之内,并可以通过__enum__这个属性访问到。

const color = new Enum({ red: "red", green: "green", blue: "blue" });
console.log(color.red); // red
console.log(color.green); // green
console.log(color.blue); // blue

const status = new Enum({ value: 0, pending: 1, done: 2 });
console.log(status.value); // 0 (特征属性value)
console.log(status.pending); // 1
console.log(status.done); // 2
console.log(status.__enum__.value); // 0 (原始枚举值)