Full featured, extensible enumerations for javascript.
March 18, 2021 10:43 AM Subscribe
Full featured, extensible enumerations for javascript.
It seems like my pandemic super-power is starting a project, getting side-tracked by some aspect of that project, turning that into its own project and then forgetting about the thing I was originally working on. In that spirit, I give you enumerated types (not just constants!) for javascript.
Also on github.
There are a bunch of similar packages on npm. What makes this one different, I think, is a focus on the core idea of 'enumeration' rather than enumerated constants specifically.
It provides an object, EnumerationBase, on which the different enumerated types are built and which users can use to create others. There are, currently, three types of enumerations that can be created.
1. Enum - your standard enumerated constants
It seems like my pandemic super-power is starting a project, getting side-tracked by some aspect of that project, turning that into its own project and then forgetting about the thing I was originally working on. In that spirit, I give you enumerated types (not just constants!) for javascript.
Also on github.
There are a bunch of similar packages on npm. What makes this one different, I think, is a focus on the core idea of 'enumeration' rather than enumerated constants specifically.
It provides an object, EnumerationBase, on which the different enumerated types are built and which users can use to create others. There are, currently, three types of enumerations that can be created.
1. Enum - your standard enumerated constants
Color = Enum('RED', 'GREEN', 'BLUE'); Color.RED.toInt(); // 0 Color.fromInt(2); // Color.BLUE Color.GREEN.toString() // 'GREEN'2. Flags - Like Enum but each constant's value is a power of 2 so they can be bitwise OR'd together into a single integer.
Ops = Flags('A', 'B', 'C', 'D'); Ops.A | Ops.B | Ops.D; // 113. Ranges - A predefined range of (numerical) values. These aren't constants because the range could be quite large but it restricts the allowable values.
SmallNum = EnumeratedRange(1, 10) SmallNum(1); // OK SmallNum(5); // OK SmallNum(10); // Still OK SmallNum(0); // Error! SmallNum(11); // Error!All of the types support a basic set of methods. You can get the first() or last() element of an enumeration...
Color.first(); // Color.RED SmallNum.last(); // SmallNum(10)...navigate around with next(), previous() or get an iterator() to loop through the sequence (or subsequence, or in reverse)
// Counting SmallNum.iterator().forEach(n => console.log(n)) // Counting down! SmallNum.iterator(SmallNum.last(), SmallNum.first()).forEach(n => console.log(n))They support comparison operations.
Color.RED.isGreater(Color.BLUE); // false SmallNum(1).isEqualTo(SmallNum(1)); // trueAnd, finally, enumerations can be extended by the user with additional custom methods.
Ops = Flags('A', 'B', 'C', 'D').methods({ enable() { console.log(`Enabling ${this}`); } }) Ops.listFromInt(11).forEach(option => option.enable()); // Print to the console: // Enabling A // Enabling B // Enabling D
Role: author/developer
« Older Recent work at USGS Water... | Painted pine linen press... Newer »
posted by AndrewStephens at 12:28 PM on March 26, 2021 [1 favorite]