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

@voidvolker/enum

v1.0.0

Published

Simple Enum JS class

Downloads

1

Readme

Enum

Simple Enum JS class.


Table of Contents


Install

npm i @voidvolker/enum

Load

ES module:

import Enum from '@voidvolker/enum'
import { Enum } from '@voidvolker/enum'

Require:

const Enum = require('@voidvolker/enum')
const { Enum } = require('@voidvolker/enum')

~ Quick start ~

import Enum from '@voidvolker/enum'

const roles = new Enum({ none: 0, admin: 1 }, [
    'moderator',
    'manager',
    'client',
    'user'
])
console.log(roles.key.admin) // -> 1
console.log(roles.key.moderator) // -> 2
console.log(roles.key.manager) // -> 3
console.log(roles.key.client) // -> 4
console.log(roles.key.user) // -> 5

Enum() Constructor

Create new Enum instance. In case when pairs is used - counter will be changed to last value from pairs object.

new Enum(names)
new Enum(pairs)
new Enum(pairs, names)
new Enum(names, options)
new Enum(pairs, options)
new Enum(pairs, names, options)

First argument: pairs or names, second - names or options, third - always options

Arguments:

| Option | Type | Description | | --- | --- | --- | | pairs | Object -> name: value<Number> | Object with pairs in format name: value | | names | Array<String> | A list of names in string format | | options | Object | Additional options |

Options:

| Option | Type | Description | | --- | --- | --- | | i | Number | Inital counter value | | next | Function | Counter increment callback. Default: this.i++ |

Methods

.forKeys()

Execute callback once for each key.

.forKeys(cb)
.forKeys(cb, thisArg)

| Option | Type | Description | | --- | --- | --- | | cb* | Function, cb(key) | Callback to execute, required | | thisArg | * | A value to use as this when executing callback. Default - current enum instance. | | | | | | return | Enum | Returns current enum instance |

Callback options:

| Option | Type | Description | | --- | --- | --- | | key | String | Key |

.forValues()

Execute callback once for each value.

.forValues(cb)
.forValues(cb, thisArg)

| Option | Type | Description | | --- | --- | --- | | cb* | Function, cb(value) | Callback to execute, required | | thisArg | * | A value to use as this when executing callback. Default - current enum instance. | | | | | | return | Enum | Returns current enum instance |

Callback options:

| Option | Type | Description | | --- | --- | --- | | value | Number | Value |

.forEach()

Execute callback once for each key.

.forKeys(cb)
.forKeys(cb, thisArg)

| Option | Type | Description | | --- | --- | --- | | cb* | Function, cb(key, value) | Callback to execute, required | | thisArg | * | A value to use as this when executing callback. Default - current enum instance. | | | | | | return | Enum | Returns current enum instance |

Callback options:

| Option | Type | Description | | --- | --- | --- | | key | String | Key | | value | Number | Value |

.keys()

Return array with all keys of current enum.

.values()

Return array with all values of current enum.

.hasKey()

Check if key in current enum with Object.prototype.hasOwnProperty method.

.hasKey(key)

.hasValue()

Check if value in current enum with Object.prototype.hasOwnProperty method.

.hasValue(value)

.next()

Callback to change counter value for current enum.

.raw()

Create raw enum object with pairs key: value and value: key from current enum instance. Object created with null prototype for safe key/value conversion. Usefull in case when you don't need any other features of Enum class and you just need a simple index of keys/values. To store counter in object - Symbol is used stored in Enum.index property.

let enm = new Enum(['foo', 'bar'])
let rawEnm = enm.raw()
let enmCnt = enm.i
let rawCounter = rawEnm[Enum.index]

Properties

Enum instance properties was created in constructor.

.i

Counter value.

  • Type: Number
  • Default: 0

.key

key -> value map to get value for selected key.

  • Type: Object
const enm = new Enum(['foo', 'bar'])
console.log(enm.key.foo) // -> 0
console.log(enm.key.bar) // -> 1

.value

value -> key map to get key for selected value.

  • Type: Object
const enm = new Enum(['foo', 'bar'])
console.log(enm.value[0]) // -> 'foo'
console.log(enm.value[1]) // -> 'bar'

Static methods

Enum.new()

Create enum using global counter from constructor. Arguments same as for Enum() constructor. i defined in counter will be used. If not - global counter from Enum.i will be used.

Enum.raw()

Alias to new Enum(...).raw() and have same argumenst as Enum() constructor.

Enum.raw(pairs, list, options)

Enum.fromRaw()

Create Enum instance from raw object. Values and names filtered by value type: string - name, number - value.

Enum.fromRaw(rawEnm)

Enum.next()

Method to change counter value for current enum.

Static properties

Enum.i

Global index counter. Initial value: 0

Enum.index

Symbol for saving counter in raw object.

Enum.default

Object with default options. Options:

  • i: 0

Default options override behavior: user options -> current constructor default -> Enum.default


Development

Install development dependencies:

npm -D i @voidvolker/enum

Run coffee compiler:

npm run w

Run tests or tests watcher:

npm test
npm run wt

Run live dev.js file with debugger:

npm run dev

Build:

npm run build

Build results are saved to dist folder. Babel and rollup is used.

PR and bugs

If you find a bug or want to add some awesome feature - feel free to send PR. Be sure the code is simple and effective and don't forget to add tests.

What next?

Have fun!