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

@4a/enum

v0.4.1

Published

simple enum type for js

Downloads

25

Readme

Enum type

简单的Enum类型

Install

npm i @4a/enum

Usage

// ES6
import Enum from '@4a/enum'

// or Nodejs
const Enum = require('@4a/enum')


const DemoEnum = Enum({
    name: "NAME",
    homePage: "HOMEPAGE",
})

DemoEnum.name.rawValue // "NAME"
DemoEnum.homePage.rawValue // "HOMEPAGE"

DemoEnum.$has(DemoEnum.homePage)

DemoEnum.$append('OK', 'OK')
DemoEnum.$extend({ fail: 'FAIL' })
function Message(code, message) {
    return { code, message }
}

const ResultMessage = Enum({
    errorToken: Message(1000, "token error"),
    expiredToken: Message(1001, "token expired"),
})

ResultMessage.errorToken // {code: 1000, message: "token error"}
ResultMessage.expiredToken // {code: 1001, message: "token expired"}

try {
    throw ResultMessage.errorToken.Error()
}
catch(err) {
    if (err instanceof Enum.EnumError) {
        console.log(err.Encode())
    }
    console.log(err)
}

Assert

const assert = require('@4a/enum/assert')

// assert.ok(1 === 2, ResultMessage.errorToken.Error())
// assert.fail(1 === 1, ResultMessage.errorToken.Error())
// assert.equal(1, 2, ResultMessage.errorToken.Error())
// assert.notEqual(1, 1, ResultMessage.errorToken.Error())
// assert.deepEqual([1], [2], ResultMessage.errorToken.Error())

try {
    assert.notDeepEqual([2], [2], ResultMessage.errorToken.Error('custom error message'))
}
catch(err) {
    if (err instanceof Enum.EnumError) {
        console.log(101, err.Encode())
        console.log(101, err.Encode().code)
        console.log(101, err.Encode().message)
        console.log(101, err)
    }
}
const APP = Enum({
    name: 'wechat',
    version: '1.0.0',
    demo: { foo: 'bar' }
})

APP.version.isEqual('1.0.0') // true
APP.version.isNotEqual('1.0.0') // false

APP.demo.isDeepEqual({ foo: 'bar' }) // true
APP.demo.isNotDeepEqual({ foo: 'bar' }) // false

Notes

const Enum = require('@4a/enum')

Enum // 创建的类型是静态类型,默认不可修改
Enum.Enum // Enum原始类
Enum.EnumItem // Enum子项的父类
Enum.EnumError // Enum的错误类型父类


// 创建一个Enum类型
// Enum根据obj参数来创建Enum类型
// obj子项的值如果是js原始对象,EnumItem实例的值就是obj对应子项的值
// obj子项的值如果不是原始对象,enumItem.rawValue属性就是obj对应子项的值
const obj = {a: 1, b: 2, app: 3}
const Message = Enum(obj)


// EnumError
// EnumItem实例可以直接调用Error方法创建EnumError实例,message参数可选
// 如果EnumItem没有message属性,则必须传递message参数
throw Message.app.Error(message)

// EnumError实例可以调用Encode方法来进行重新编码
Message.app.Error(message).Encode()

Example

npm test

Vue

vue.config.js配置中注意配置transpileDependencies属性,让webpack可以编译@4a/enum

// Example
// 兼容低版本浏览器  
transpileDependencies: [
    /[/\\]node_modules[/\\]@4a\/enum[/\\]/,
]