Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.13 KB

File metadata and controls

41 lines (31 loc) · 1.13 KB

EnumSet

A specialized Set implementation for use with numerical enum types. All state is packed into a single 32-bit integer bitfield.

Installation

npm install @marco.spiess/enumset

Usage

import { EnumSet } from "@marco.spiess/enumset";
// Declare an enum
const Directions = {
  Left: 0,
  Up: 1,
  Right: 2,
  Down: 3,
} as const;
type Direction = typeof Directions[keyof typeof Directions];

const enumSet = new EnumSet<Direction>([Directions.Up]);
enumSet.add(Directions.Down);

expect([...enumSet.values()]).toStrictEqual([Directions.Up, Directions.Down]);
expect(enumSet.has(Directions.Left)).toBe(false);

Caveats

  • All enum values must be integers in the interval [0,32). This is enforced through TypeScript.
  • Iteration order breaks the Set contract. The methods forEach, keys, values and [Symbol.iterator] iterate in the ascending order of the enum values and not in insertion order.