New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@herb-ert/enumjs

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@herb-ert/enumjs

"A lightweight utility class for managing enumerable values in JavaScript."

latest
Source
npmnpm
Version
1.1.4
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

🧩 Enum.js

A lightweight, versatile utility class for creating and managing enumerable values in JavaScript. Great for maintaining clean, ordered value sets like directions, states, roles, and more.

NPM version NPM downloads GitHub issues GitHub pull requests GitHub contributors GitHub forks GitHub stars

✨ Features

  • Create immutable, ordered enumerations
  • Access values by index, label, or relative position
  • Supports iteration, mapping, filtering, and other functional utilities
  • Easily retrieve first, last, random, and adjacent values
  • Enhanced validation for enum values
  • Works in both Node.js and browser environments
  • Fully documented and tested

📦 Installation

npm install @herb-ert/enumjs

If you're using ES Modules:

import Enum from '@herb-ert/enumjs';

Or with CommonJS:

const Enum = require('@herb-ert/enumjs');

🚀 Usage

const Directions = new Enum('NORTH', 'EAST', 'SOUTH', 'WEST');

console.log(Directions.NORTH);        // "NORTH"
console.log(Directions.values);       // ["NORTH", "EAST", "SOUTH", "WEST"]
console.log(Directions.next('EAST')); // "SOUTH"
console.log(Directions.random());     // Randomly picks one of the directions
console.log([...Directions]);         // ["NORTH", "EAST", "SOUTH", "WEST"]

🧰 API

Constructor

new Enum(...values)

Properties

NameDescription
valuesArray of all enum values
lengthNumber of values in the enum
firstFirst value
lastLast value

Methods

MethodDescription
Enum.of(...args)Creates a new instance of the Enum class with the provided arguments.
next(currentValue)Retrieves the next value in the sequence from the values array.
previous(currentValue)Retrieves the previous value in the values array relative to the given current value.
compare(firstValue, secondValue)Compares the positions of two values within the values array and determines their relative difference.
getByIndex(index)Retrieves the value at the specified index from the values array.
has(value)Checks if a specified value exists in the collection.
indexOf(value)Returns index of value or -1
toString(value)Converts the current instance into its string representation.
isFirst(value)Checks if the provided value is the first element in the values array.
isLast(value)Checks if the provided value is the last element in the array values array.
random()Retrieves a random element from the values array.
filter(predicate)Filters the values of the current instance based on a provided predicate function.
some(predicate)Checks if at least one element in the array satisfies the provided testing function.
every(predicate)Evaluates whether all elements in the values array satisfy the provided predicate function.
map(callback)Applies a callback function to each element in the values array and returns a new array with the results.
reduce(callback, initialValue)Reduces the array to a single value using a provided callback function.
forEach(callback)Iterates over each element in the collection and executes the provided callback function.
[Symbol.iterator]()Returns the default iterator for the object.

📦 More Examples

Creating Enums

You can instantiate an enum in two equivalent ways:

const Roles1 = new Enum('USER', 'MODERATOR', 'ADMIN');  // Using the constructor
const Roles2 = Enum.of('USER', 'MODERATOR', 'ADMIN');   // Via the static helper

Accessing Basic Properties

console.log(Roles2.values)            // ['USER', 'MODERATOR', 'ADMIN']
console.log(Roles2.first);            // "USER"       — the first value in the enum
console.log(Roles2.last);             // "ADMIN"      — the last value in the enum
console.log(Roles2.length)            // 3

Functional Utilities

Enum supports most of the array utility methods. Here's how you can use them.

const Status = Enum.of('IDLE', 'RUNNING', 'DONE');

console.log(Status.some(s => s.startsWith('R')));           // true  (because "RUNNING" starts with "R")
console.log(Status.every(s => typeof s === 'string'));      // true  (all values are strings)
console.log(Status.map(s => s.toLowerCase()));              // ["idle", "running", "done"]
console.log(Status.reduce((acc, val) => acc + val[0], '')); // "IRD"  (concatenates first letters of each status)
const Short = Status.filter(s => s.length <= 4);            // ["IDLE", "DONE"]
console.log(Status.random());                               // e.g. "RUNNING"

Iteration

Enums are iterable, so you can spread or loop over them.

for (const s of Status) {
  console.log(s);
}

console.log([...Status]); // ["IDLE", "RUNNING", "DONE"]

🧪 Validation

The constructor will throw an error if: Any value is not a string There are duplicate values

new Enum('A', 'B', 42);       // ❌ TypeError: Enum values must be strings. Invalid entries: 42
new Enum('A', 'B', 'A');      // ❌ Error: Enum values must be unique. Duplicates found: RED

🔧 License

MIT © herb-ert

Keywords

enum

FAQs

Package last updated on 05 May 2025

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