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

@org.slashlib/ts-enums-and-flags

v0.1.2

Published

slashlib.org enumeration and flag generator for typescript

Downloads

3

Readme

enumeration and flag generator for typescript

provides typesave enumerations and flags

commonjs package with typings, compiled for es2017(es8)

building the library

  • Fork or download from https://github.com/org-slashlib/ts-enums-and-flags
  • Run npm install
  • Run npm run test
  • Run grunt
  • Change to subfolder dist/ts-enums-and-flags and run npm pack

installing the library

This guide assumes, that you are familiar with the use of npm.

If you built the library on your own: npm install path/to/@org.slashlib-ts-enums-and-flags-<version>.tgz

If you want to install the public version on npm: npm install @org.slashlib/ts-enums-and-flags --save

usage

import { enumerate } from "@org.slashlib/ts-enums-and-flags";

// Note: Both literal arrays must be of same size. stringliteral[0] will be mapped to numberliteral[0]
//       numberliteral[n] is called "index" of stringliteral[n]
const STRINGLITERALS = [ <const>"enum0", <const>"enum1", <const>"enum2", <const>"enum3", <const>"enum4" ];
const NUMBERLITERALS = [ <const>0,       <const>1,       <const>2,       <const>3,       <const>4       ];

const enumerated = enumerate( STRINGLITERALS, NUMBERLITERALS );

type  EnumType   = keyof typeof enumerated.base;
const EnumValues = Object.freeze( enumerated.enumeration );

let a0   : EnumType = EnumValues.enum0;       // ok
let a1   : EnumType = "enum0";                // ok
let a2   : EnumType = 0;                      // ok

let err1 : EnumType = "something different";  // compiler error
let err2 : EnumType = 10;                     // compiler error

// caveats: (you can ... but you'd better not... this will bypass compiler checks)
let c01  : EnumType = EnumValues[ 0 ];        // ok => EnumValues.0 (pseudo!)
let c02  : EnumType = EnumValues[ 200 ];      // ok and accepted by compiler, but
                                              // will result in c02 === undefined
let c03  : EnumType = EnumValues[ "enum1" ];  // ok => EnumValues.enum1
let c04  : EnumType = EnumValues[ "foo" ];    // ok and accepted by compiler, but
                                              // will result in c04 === undefined

// Returns all enumerations as enumerated names
let names   = EnumValues.keys();                        // => [ "enum0", ... ]
// Return all enumerations as enumerated indexes/values
let indices = EnumValues.indexes();                     // => [ 0, 1, ... ]

// Returns an enumerated index/value from an enumerated name
// (Transforms enum.string to enum.number)
let test01  = EnumValues.indexOf( "enum1" );            // => 1
let test02  = EnumValues.indexOf( EnumValues.enum1 );   // => 1


// Returns an enumerated name from  an enumerated index/value
// (Transforms enum.number to enum.string)
let test11   = EnumValues.byIndex( 2 ); // => EnumValues.enum2

// Normalizes any given enumerated name and enumerated index/value
// to an enumerated value.
// (Transforms enum.number and enum.string to enum.number)
let test21   = EnumValues.toIndex( "enum2" );           // => 2
let test21   = EnumValues.toIndex( EnumValues.enum2 );  // => 2
let test21   = EnumValues.toIndex( 2 );                 // => 2

// Normalizes any given enumerated name and enumerated index/value
// to an enumerated name.
// (Transforms enum.number and enum.string to enum.string)
let test31   = EnumValues.toKey( "enum2" );           // => EnumValues.enum2 / "enum2"
let test31   = EnumValues.toKey( EnumValues.enum2 );  // => EnumValues.enum2 / "enum2"
let test31   = EnumValues.toKey( 2 );                 // => EnumValues.enum2 / "enum2"


let test41: EnumType = 0; // any of 0 | 1 | 2 | 3 | 4 | enum0 | enum1 ... will work
switch( EnumValues.toKey( test41 )) {
   case EnumValues.enum0: {
      //statements;
      break;
   }
   case EnumValues.enum1: {
      //statements;
      break;
   }
   default: {
      //statements;
      break;
   }
}

switch( EnumValues.toIndex( test41 )) {
   case 0: {
      //statements;
      break;
   }
   case 1: {
      //statements;
      break;
   }
   default: {
      //statements;
      break;
   }
}