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

constjs

v0.0.4

Published

A simple utility for generating an enum/const/bitmap object with a string contains enum keys

Downloads

132

Readme

constjs

Create const/enum/bitmap object with key names specified in String, Array, Object or Arguments

Usage

npm install constjs

Enum (Java style)

var ConstJs = require('constjs');

var Colors = ConstJs.enum("blue red");

var myColor = Colors.blue;

console.log(myColor.isBlue()); // output true
console.log(myColor.is('blue')); // output true
console.log(myColor.is('BLUE')); // output true
console.log(myColor.is(0)); // output true
console.log(myColor.is(Colors.blue)); // output true

console.log(myColor.isRed()); // output false
console.log(myColor.is('red')); // output false

console.log(myColor._id); // output blue
console.log(myColor.name()); // output blue
console.log(myColor.toString()); // output blue

// See how CamelCase is used to generate the isXxx() functions
var AppMode = ConstJs.enum('SIGN_UP, LOG_IN, FORGOT_PASSWORD');
var curMode = AppMode.LOG_IN;

console.log(curMode.isLogIn()); // output true
console.log(curMode.isSignUp()); // output false
console.log(curMode.isForgotPassword()); // output false

Enum serialization/deserialization with JSON

Adding methods to enum elements could be very handy for use, however there is major concern about applying this style. Once you serialize your enum into JSON format string and parse it back, you loose all methods and suddenly all your code doesn't work anymore. Fortunately constjs provides a way unJSON() to to help you alleviate this concern:

var ConstJs = require('constjs');

...

var Colors = ConstJs.enum('Red, Green, Blue');
...

var myData = {
    name: 'blah blah',
    color: Colors.Red
}
console.log(myData.color.isRed()); // output: true
...

var myDataStr = JSON.stringfy(myData);
...

myData = JSON.parse(myDataStr);
// console.log(myData.color.isRed()); // exception thrown out here
myData = ConstJs.unJSON(myData); 
// or myData = ConstJs.unJSON(JSON.parse(myDataStr)); the same effects
console.log(myData.color.isRed()); // output: true

However there is cost associated with the unJSON feature, say, your JSON string will contains additional data to support deserialization. If you are sure you don't need to deserialize your enum data, constjs provides a way to generate relatively more lightweight enum data.

Instead of

var Colors = ConstJs.enum('Red, Green, Blue');

You use enum.transient:

var Colors = ConstJs.enum.transient('Red, Green, Blue');

or use the alias enum.lite:

var Colors = ConstJs.enum.lite('Red, Green, Blue');

To compare the data been generated:

var Colors = ConstJs.enum('Red, Green, Blue');
var LiteColors = ConstJs.enum.lite('Red, Green, Blue');

var myColor = Colors.Red, myLiteColor = LiteColors.Red;
console.log({myColor: myColor, myLiteColor: myLiteColor});

You should be able to see something like:

{
    myColor: {_id: 'Red', _seq: 0, _kl: ['Red', 'Green', 'Blue']},
    myLiteColor: {_id: 'Red', _seq: 0}
}

And after unJSON call on myLiteColor, you will find that the isRed(), isGreen() and isBlue() method is no longer there. However you can still use name(), toString() and is() method on the myColor object after unJSON():

...
var s = JSON.stringify(myLiteColor);
var c = ConstJs.unJSON(s);

console.log(c.isRed); // output: undefined
console.log(c.name()); // output: 'Red'
console.log(c.is('Red')); // output: true

String Constants

var ConstJs = require('constjs');

var Weekdays = ConstJs.const("Mon, Tue, Wed");
console.log(Weekdays); // output {Mon: 'Mon', Tue: 'Tue', Wed: 'Wed'}

var today = Weekdays.Wed;
console.log(today); // output: 'Wed';

Bitmap

var ConstJs = require('constjs');

var ColorFlags = ConstJs.bitmap("blue red");
console.log(ColorFlags.blue); // output false

var StyleFlags = ConstJs.bitmap(true, "rustic model minimalist");
console.log(StyleFlags.rustic); // output true

var CityFlags = ConstJs.bitmap({Chengdu: true, Sydney: false});
console.log(CityFlags.Chengdu); //output true
console.log(CityFlags.Sydney); // output false

var DayFlags = ConstJs.bitmap(true, {Mon: false, Tue: true});
console.log(DayFlags.Mon); // output false. Default val wont override specified val if the type is boolean 

Input variations

Instead of a string of keys sepated by separators specified above, it can use another two variaions of input to specify keys:


var ConstJs = require('constjs');

// use array of strings to specify keys
var Color = ConstJs.enum(["blue", "red"]);
var myColor = Color.blue;

// use arguments array to specify keys
var BuildTool = ConstJs.enum("gulp", "grunt");
var myTool = BuildTool.gulp;

// use object to specify keys
var WeekDay = ConstJs.enum({
    Monday: null,
    Tuesday: null
})
var myDay = WeekDay.Monday;

The input variations are supported by all three generators: enum, const and bitmap

Immutatibility

constjs tried to use Object.freeze() to make the enum/const object be immutabile:

var ConstJs = require('constjs');

// use array of strings to specify keys
var Color = ConstJs.enum(["blue", "red"]);
Color.blue = 'blue';
console.log(Color.blue); // output {_id: 'blue', ...}

var WeekDay = ConstJs.const('Mon Tue');
WeekDay.Mon = false;
console.log(WeekDay.Mon); // output: 'Mon'
WeekDay.Wed = 'Wed';
console.log(WeekDay.Wed); // output: 'undefined'

For bitmap object, constjs use Object.seal() so the flag could be set/unset. However it still doesn't allow properties been removed or added:

var ConstJs = require('constjs');

var ColorFlags = ConstJs.bitmap("blue red");
ColorFlags.blue = true;
console.log(ColorFlags.blue); // output: true
ColorFlags.brown = false;
console.log(ColorFlags.brown); // output: 'undefined'

However constjs provided an immutable function to bitmap thus it ensure the data returned is completely frozen:

var ConstJs = require('constjs');

var ColorFlags = ConstJs.bitmap.immutable("blue red");
ColorFlags.blue = true;
console.log(ColorFlags.blue); // output: false
ColorFlags.brown = false;
console.log(ColorFlags.brown); // output: 'undefined'

Dependencies

  • lodash