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

@gradecam/tsenum

v1.2.0

Published

Super simple typescript library for string-compatible enums

Downloads

12,380

Readme

Description

Typescript supports enum now, but there are times when you may want a little more flexibility than built-in enums give you; this package was adapted from an example in a gist linked from a typescript issue which I cannot now find, but suffice it to say that though we have modified it a bit the idea is not original to us.

Installing

npm install --save @gradecam/tsenum

Basic Usage

import {MakeEnum, TypeFromEnum} from 'tsenum';

const Colors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
    Violet: 'violet',
    Black: 'black'
});
type Colors = TypeFromEnum<typeof Colors>;

// type Colors = 'red' | 'blue' | 'green' | 'violet' | 'black'
// value Colors is a frozen object with the keys expected
// typeof Colors.Red is 'red', et al

carColor: Colors = 'red'; // valid
carColor = Colors.Green; // valid
carColor = 'yellow'; // typescript error, not a valid color

Combining types

MakeEnum will merge multiple enum objects into one (up to 9), allowing you to combine types.

import {MakeEnum, TypeFromEnum} from 'tsenum';

const PrimaryColors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
});
type PrimaryColors = TypeFromEnum<typeof PrimaryColors>;

const SecondaryColors = MakeEnum({
    Yellow: 'yellow',
    Cyan: 'cyan',
    Magenta: 'magenta'
});
type SecondaryColors = TypeFromEnum<typeof SecondaryColors>;

const AllColors = MakeEnum(PrimaryColors, SecondaryColors);
type AllColors = TypeFromEnum<typeof AllColors>;
// type AllColors = 'red' | 'blue' | 'green' | 'yellow' | 'cyan' | 'magenta'

Getting an array of possible values

Sometimes you may want an array of possible values, such as when defining an enum type in a mongoose schema. Since the enum is an object, you can use Object.values to get that:

import {MakeEnum, TypeFromEnum} from 'tsenum';

const PrimaryColors = MakeEnum({
    Red: 'red',
    Blue: 'blue',
    Green: 'green',
});
type PrimaryColors = TypeFromEnum<typeof PrimaryColors>;

const PrimaryColorList = Object.values(PrimaryColors); // ['red', 'blue', 'green']
// typeof PrimaryColorList = Array<'red'|'blue'|'green'>

Allowed value types

Currently you can use any string, number, or boolean as a value