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

string-enum

v1.2.1

Published

Creates simple and immutable sets of names.

Downloads

37

Readme

string-enum

Build Status Coverage Status npm version Supported Platform js version

Creates simple and immutables sets of names (strings).

These sets are guaranteed to be immutable. Moreover, if you ever mispell a name, an error will be raised immediately, instead of letting undefined cripple and damage your running program in unexpected ways, thus following a fail-fast approach.

Installation

npm install string-enum

Usage (node)

import StringEnum from "string-enum";         // ES6 modules
// or
const StringEnum = require("string-enum");    // Classic way

const eyeColors = StringEnum(
    "brown",
    "blue",
    "green",
    "hazel");

console.log(eyeColors.brown);   // brown
console.log(eyeColors.green);   // green

console.log(eyeColors.bronw);   // Error, undefined eye color.

eyeColors.brown = "blue";       // Error, cant modify existing color

eyeColors.red = "red";          // Error, cant add new color

delete eyeColors.brown;         // Error, cant delete color

Usage (browser)

<script src="path/to/string-enum.js"></script>
<!-- or -->
<script src="path/to/string-enum-min.js"></script>
<!-- or -->
<script src="https://unpkg.com/string-enum"></script>
<!-- or -->
<script src="https://unpkg.com/string-enum/dist/string-enum-min.js"></script>

<script>
    const eyeColors = StringEnum (
        "brown",
        "blue",
        "green",
        "hazel");
    alert(`You have beautiful ${eyeColors.hazel} eyes!`);
</script>

Downloads

More usage

Construction

To create a new set of enum values, all the followings are equivalent :

const eyeColors = StringEnum("brown", "blue", "green", "hazel");

const eyeColors = StringEnum( ["brown", "blue", "green", "hazel"] );

const eyeColors = new StringEnum("brown", "blue", "green", "hazel");

Enum elements should be strings :

const numbers = StringEnum(1, 2, 3);        // TypeError

Strings that dont qualify as valid JS identifiers are allowed, but then you must use bracket notation to access them in your code later on :

const otherColors = StringEnum('light-blue', 'lime-green');

otherColors['light-blue']                   // OK
otherColors.light-blue                      // Not working

Enum elements cant be repeated :

const colors = StringEnum('red', 'blue', 'red');    // Error

Equality

Enum elements are just normal strings, hence:

eyeColors.blue === 'blue';                  // true

Iteration

Enum elements are iterable with either for... of... or for... in...:

for(const color in eyeColors) { ... }

for(const color of eyeColors) { ... }

Membership lookup

To check beforehand if an element is part of the Enum set, you may use the in operator:

'blue' in eyeColors;                // true
'red' in eyeColors;                 // false

Special object properties

StringEnum are objects and as such do own special objects properties, e.g. __proto__ and constructor, just like most other JS objects. However, StringEnum permits you to override these objects, because most of the time the user don't care about them. However, if they are not overridden, they behave normally. Note that the in operator has been modified to respond only to the enum elements.

eyeColors.__proto__             // prototype
eyeColors.constructor           // constructor
eyeColors.toString()            // string conversion
'constructor' in eyeColors      // false

const specials = StringEnum('constructor', 'toString');
specials.constructor            // "constructor"
specials.toString               // "toString"
specials.valueOf()              // value conversion
'constructor' in specials       // true
'valueOf' in specials           // false

Other stuff

eyeColors instanceof StringEnum;    // true

Object.keys(eyeColors)              // ['browns', etc.]
Object.values(eyeColors)            // ['browns', etc.]
[...eyeColors]                      // ['browns', etc.]

{...eyeColors}                      // { brown:'brown', etc. }

ES6 module versions

The package also exists in ES6 module versions to be used on the web or with Node. Note that the -module versions may be used on a server that cant properly serve .mjs extension as a JavaScript MIME type.

Comparison with other Enum packages

There are many other enum packages available on npm. Most of them are useful for their particular use cases but none of them provides precisely what StringEnum means to do. StringEnum has been developed as part of another project that needed just what it does. The motivation was to furnish an immutable set of predefined names that catches most errors and (stupid) mistakes. It is a very simple code and very simple to use.

License

MIT