Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@js-bits/enumerate

Package Overview
Dependencies
Maintainers
1
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@js-bits/enumerate

Easy to use, Symbol-based enum implementation

  • 0.5.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-83.33%
Maintainers
1
Weekly downloads
 
Created
Source

Easy to use, Symbol-based enum implementation

Just list values you need and enumerate tag function will create an object with corresponding keys and unique values for your convenience.

Installation

Install with npm:

npm install @js-bits/enumerate

Install with yarn:

yarn add @js-bits/enumerate

Import where you need it:

import enumerate from '@js-bits/enumerate';

or require for CommonJS:

const enumerate = require('@js-bits/enumerate');

How to use

Example 1:

const { FOOT, METER } = enumerate`FOOT METER`;

const convertToFeet = (value, unit = FOOT) => {
  if (unit === METER) {
    return value * 3.281;
  }
  return value;
};

console.log(`${convertToFeet(5)} feet`); // 5 feet
console.log(`${convertToFeet(2, METER)} feet`); // 6.562 feet

Example 2:

const STAR_WARS = enumerate`I II III IV V VI`;

const getEpisodeName = episode => {
  const { I, II, III, IV, V, VI } = STAR_WARS;
  switch (episode) {
    case I:
      return 'The Phantom Menace';
    case II:
      return 'Attack of the Clones';
    case III:
      return 'Revenge of the Sith';
    case IV:
      return 'A New Hope';
    case V:
      return 'The Empire Strikes Back';
    case VI:
      return 'Return of the Jedi';
    default:
      return 'Unknown';
  }
};

console.log(getEpisodeName(STAR_WARS.III)); // Revenge of the Sith
console.log(getEpisodeName(STAR_WARS.IV)); // A New Hope
console.log(getEpisodeName(STAR_WARS.X)); // Error: Invalid enum key: X

Primitive values

By default enumerate converts values to Symbols:

console.log(enumerate`FOOT METER`); // { FOOT: Symbol(FOOT), METER: Symbol(METER) }

You can change this behavior by specifying an appropriate converter:

console.log(enumerate(String)`FOOT METER`); // { FOOT: 'FOOT', METER: 'METER' }
console.log(enumerate(Number)`ZERO ONE TWO`); // { ZERO: 0, ONE: 1, TWO: 2 }
// or
const enumString = enumerate(String);
const enumNumber = enumerate(Number);
console.log(enumString`FOOT METER`); // { FOOT: 'FOOT', METER: 'METER' }
console.log(enumNumber`ZERO ONE TWO`); // { ZERO: 0, ONE: 1, TWO: 2 }

Or you can implement your custom converter:

const enum10s = enumerate((acc, item) => {
  acc[item] = (Object.keys(acc).length + 1) * 10;
  return acc;
});

console.log(enum10s`
  CODE1
  CODE2
  CODE3
`); // { CODE1: 10, CODE2: 20, CODE3: 30 }

But remember that only default behavior guarantees global uniqueness of enumerated values.

Notes

  • Be careful adding new items to an existing numeric enum. Always append them to the end of the list to avoid changing previous item values.
  • Does not include any polyfills, which means that Internet Explorer is not supported.

Keywords

FAQs

Package last updated on 30 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc