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 = ; var Colors = ConstJs; var myColor = Colorsblue; console; // output trueconsole; // output trueconsole; // output trueconsole; // output trueconsole; // output true console; // output falseconsole; // output false console; // output blueconsole; // output blueconsole; // output blue // See how CamelCase is used to generate the isXxx() functionsvar AppMode = ConstJs;var curMode = AppModeLOG_IN; console; // output trueconsole; // output falseconsole; // 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 = ; ... var Colors = ConstJs;... var myData = name: 'blah blah' color: ColorsRedconsole; // output: true... var myDataStr = JSON;... myData = JSON;// console.log(myData.color.isRed()); // exception thrown out heremyData = ConstJs; // or myData = ConstJs.unJSON(JSON.parse(myDataStr)); the same effectsconsole; // 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;
You use enum.transient
:
var Colors = ConstJsenum;
or use the alias enum.lite
:
var Colors = ConstJsenum;
To compare the data been generated:
var Colors = ConstJs;var LiteColors = ConstJsenum; var myColor = ColorsRed myLiteColor = LiteColorsRed;console;
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;var c = ConstJs; console; // output: undefinedconsole; // output: 'Red'console; // output: true
String Constants
var ConstJs = ; var Weekdays = ConstJs;console; // output {Mon: 'Mon', Tue: 'Tue', Wed: 'Wed'} var today = WeekdaysWed;console; // output: 'Wed';
Bitmap
var ConstJs = ; var ColorFlags = ConstJs;console; // output false var StyleFlags = ConstJs;console; // output true var CityFlags = ConstJs;console; //output trueconsole; // output false var DayFlags = ConstJs;console; // 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 = ; // use array of strings to specify keysvar Color = ConstJs;var myColor = Colorblue; // use arguments array to specify keysvar BuildTool = ConstJs;var myTool = BuildToolgulp; // use object to specify keysvar WeekDay = ConstJsvar myDay = WeekDayMonday;
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 = ; // use array of strings to specify keysvar Color = ConstJs;Colorblue = 'blue';console; // output {_id: 'blue', ...} var WeekDay = ConstJs;WeekDayMon = false;console; // output: 'Mon'WeekDayWed = 'Wed';console; // 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 = ; var ColorFlags = ConstJs;ColorFlagsblue = true;console; // output: trueColorFlagsbrown = false;console; // output: 'undefined'
However constjs
provided an immutable
function to bitmap
thus it ensure the data returned is completely frozen:
var ConstJs = ; var ColorFlags = ConstJsbitmap;ColorFlagsblue = true;console; // output: falseColorFlagsbrown = false;console; // output: 'undefined'
Dependencies
lodash