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

enumset32

v0.6.4

Published

High-performance, typesafe, small ordered set implemented in TypeScript

Downloads

3

Readme

enumset32

enumset32 License: MPLv2 Follow this project on Twitter

High-performance, typesafe, small ordered set (up to 32 members) implemented in TypeScript

Please consider supporting The Typeverse Project (which includes enumset32) on Patreon.

enumset32 is specialized typesafe set implementation for use with TypeScript numeric enum types having a maximum of 32 members.

The members of the set are defined by a TypeScript numeric enum object. An enumset32 value is extremely compact compared to a JavaScript object as it's runtime representation is merely the lower 32 bits of a primitive JavaScript number value.

The time and space performance characteristics of enumset32 are uber-superb.

As an additional bonus, enumset32 is implementated in TypeScript. This makes for a high-quality, typesafe alternative to otherwise tedious coding and bit-fiddling with JavaScript numbers.

The Java standard library has a class called EnumSet and enumset32 as implemented by The Typeverse Project in TypeScript is very similar in concept.

To understand the "why" for enumset32, the documentation for the EnumSet class in Oracle's Java SE 12 & JDK 12 Docs is good background reading.

Note: Prior to the release of v1.0.0 and depending on your feedback the API is subject to change.

Quickstart Tutorial

Installation

$ npm install --save enumset32

As with most other npm packages enumset32 may also be installed with other package managers such as yarn and pnpm.

Creating enumsets

import { Enumset32, UniversalEnumset32 } from 'enumset32';

// Start out by declaring a TypeScript numeric enum for constructing a universal
// set comprising solely of only members of this enum. The enum members must be
// integer-valued numbers in the range 0..31 inclusive.

enum FileAttribute {
  FILE,
  DIRECTORY,
  SYMLINK,
  HIDDEN = 31
}

// Use the generic UniversalEnumset32 factory constructor function to create
// an enum universe specific to the type of the declared enum.

let fileAttributes: UniversalEnumset32<typeof FileAttribute> = UniversalEnumset32(FileAttribute);

// Creating (sub)sets from discrete enum members in this enum universe:

let fileFlag: Enumset32<typeof FileAttribute> = fileAttributes.set(FileAttribute.FILE);

// display result as string of digits in base2
console.log(fileAttributes.toDigitString(fileFlag, 2));
// => 1

// display result as an array of enum member keys
console.log(fileAttributes.enumKeys(fileFlag));
// => [ "FILE" ]

let fileOrDirectoryFlags: Enumset32<typeof FileAttribute> = fileAttributes.set(
  FileAttribute.FILE,
  FileAttribute.DIRECTORY
);

console.log(fileAttributes.toDigitString(fileOrDirectoryFlags, 2));
// => 11

console.log(fileAttributes.enumKeys(fileOrDirectoryFlags));
// => [ "FILE", "DIRECTORY" ]

let hiddenFlag: Enumset32<typeof FileAttribute> = fileAttributes.set(FileAttribute.HIDDEN);

console.log(fileAttributes.toDigitString(hiddenFlag, 2));
// => 10000000000000000000000000000000

console.log(fileAttributes.enumKeys(hiddenFlag));
// => [ "HIDDEN" ]

Operations on enumsets

// Union of an enumset with a discrete enum member

let hiddenFileOrDirectoryFlags: Enumset32<typeof FileAttribute> = fileAttributes.add(
  fileOrDirectoryFlags,
  FileAttribute.HIDDEN
);

console.log(fileAttributes.toDigitString(hiddenFileOrDirectoryFlags, 2));
// => 10000000000000000000000000000011

console.log(fileAttributes.enumKeys(hiddenFileOrDirectoryFlags));
// => [ "FILE", "DIRECTORY", "HIDDEN" ]

// Union of an enumset with another enumset

let fileOrDirectoryFlags2: Enumset32<typeof FileAttribute> = fileAttributes.addAll(
  fileOrDirectoryFlags,
  fileFlag
);

console.log(fileAttributes.equal(fileOrDirectoryFlags2, fileOrDirectoryFlags));
// => true

console.log(fileAttributes.enumKeys(fileOrDirectoryFlags2));
// => [ "FILE", "DIRECTORY" ]

// Intersection an enumset with another enumset

let testFlags: Enumset32<typeof FileAttribute> = fileAttributes.retainAll(
  fileOrDirectoryFlags,
  hiddenFlag
);

console.log(fileAttributes.empty(testFlags));
// => true

console.log(fileAttributes.enumKeys(testFlags));
// => []

Querying enumsets

// Test presence of a discrete enum member in an enumset

console.log(fileAttributes.includes(fileOrDirectoryFlags, FileAttribute.HIDDEN));
// => false since HIDDEN is absent from 1st enumset

// Test presence of every enum member of 2nd enumset in 1st enumset

console.log(fileAttributes.includesEvery(fileOrDirectoryFlags, hiddenFileOrDirectoryFlags));
// => false since HIDDEN is absent from 1st enumset

JavaScript and TypeScript Examples

The enumset32 package is designed to be used either with vanilla JavaScript (so no transpilation step is required) or with TypeScript for typesafety.

Please refer to the code in the examples directory for both JavaScript and TypeScript examples.

API

See here for the complete enumset32 API.

Benchmarks

See the charts comparing the relative performance of ES2015 Native Set, enumset32, fp-ts & Lodash with respect to common set operations.

License

Mozilla Public License Version 2.0 (MPL-2.0).

Copyright © 2019 Justin Johansson (https://github.com/indiescripter).