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

fun-enums

v1.3.0

Published

Functional enum implementation

Downloads

15

Readme

Enums

Build Status

Functional enum implementation for JS based on @rbuckton's enum proposal.

Install

npm install --save fun-enums

Usage

const { enums } = require('fun-enums');

// Default initialization
const weekdays = enums()("monday", "tuesday", "wednesday", "thursday", "friday");

weekdays.monday; // 0
weekdays.friday; // 4

Enums are frozen and should not be mutated after initializing. To avoid allowing mutation custom enum values should not be objects of depths greater than 1.

Initializers

fun-enums is built around "initializers" which allow you to change the behavior of how the enum is created. The default initalizer is number which sets the first enum value to 0 and increments each following by 1. fun-enums also exposes a string initializer which sets the value equal to the given enum name.

const { enums, string, number } = require('fun-enums');
// Provide other initializers from the package like string
let colors = enums(string)('red', 'green', 'blue')
colors.red; // 'red'

colors = enums(number)('red', 'green', 'blue'); // number does not need to be specified as it is the default behavior
colors.red // 0

Custom initializers

You can also provide your own custom initializers by passing in an initialization function to enums.

const { enums } = require('fun-enums');

function capitalize(en, _prevValue) {
  return en.charAt(0).toUpperCase() + en.slice(1);
}

const colors = enums(capitalize)('red', 'green', 'blue');
colors.red; // 'Red'

Your initializer will be passed these arguments:

| arg | type | description | | --- | ---- | ----------- | | enum | string | the string value passed to the enum to be used as the key | | prevValue | any | the value of the previous enum initialized. Used to increment enum values |

Override syntax

In some cases you want a bit more control over the exact values on your enum. You can override the normal behavior of any given initializer by passing in an object rather than an array.

const { enums } = require('fun-enums');

const colors = enums()({ red: '#f44242', green: '#27c65a', blue: '#003bff' });
colors.red; // '#f44242'

This allows you to give values when running a function may not be the best method for defining the enum. You can also "opt-in" to the initializer whenever you want even when overriding by giving a key undefined.

const { enums, string } = require('fun-enums');

const names = enums(string)({ john: 'johnny', sarah: undefined, tim: 'timothy' });
names.sarah; // 'sarah'
names.tim; // 'timothy'

Objects and Arrays are invalid types for enum values and will be overwritten by default behavior when found.

Enums API

fun-enums follows a similar API as defined by @rbuckton's enums impementing keys, values, entries, has, hasValue, and getName on each enum object.

.keys()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.keys(); // ['red', 'green', 'blue']

.values()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.values(); // [0, 1, 2]

.entries()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.entries(); // [['red', 0], ['green', 1], ['blue', 2]]

.has()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.has('red'); // true
colors.has('purple'); // false

.hasValue()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.hasValue(0); // true
colors.hasValue('red'); // false

.getName()

const { enums } = require('fun-enums');

const colors = enums()('red', 'green', 'blue');
colors.getName(0); // 'red'
colors.getName(99); // undefined