Giter Club home page Giter Club logo

enums's Introduction

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

enums's People

Contributors

dependabot[bot] avatar jaredk3nt avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.