Easy Bits

Enums, BitFlags, BitFields, BitMasks and BitArrays for JavaScript & TypeScript.
Usage
BitFlags + BitField:
const options = new BitFlags('OPTION1', 'OPTION2', 'OPTION3');
const configuration = options.createBitField();
configuration.on(options.OPTION1 | options.OPTION3);
configuration.on(options.OPTION1, options.OPTION3);
configuration.test(options.OPTION1);
configuration.test(options.OPTION2);
configuration.test(options.OPTION3);
configuration.testAny(options.OPTION1 | options.OPTION2);
configuration.testAll(options.OPTION1 | options.OPTION2);
configuration.count();
configuration.flipAll();
configuration.count();
configuration.test(options.OPTION2);
const clone = configuration.clone();
const serializedOptions = options.serialize();
const serializedBitfield = configuration.serialize();
const deserializedOptions = BitFlags.deserialize(serializedOptions);
const deserializedBitfield = BitField.deserialize(serializedBitfield);
BitFields and BitArrays are interchangeable, their APIs are identical.
The only difference between them is how many flags they support (BitField is limited to 31 flags) and their performance
(BitField is about 25% faster than BitArray). This is due to how they internally store the data.
Enums:
const Day = new Enum('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');
console.log(Day.MONDAY);
console.log(Day.MONDAY.name);
console.log(String(Day.MONDAY));
console.log(Day.FRIDAY.ordinal);
console.log(Number(Day.FRIDAY));
Day.MY_OWN_DAY = 1337;
Day.MONDAY = 42;
console.log(Object.keys(Day));
console.log(Day.values());
for (let value of Day) {}
Day.forEach((value, name) => {});
switch (value) {
case Day.MONDAY:
break;
case Day.TUESDAY:
break;
}
console.log(Day.MONDAY.equals(Day.FRIDAY));
console.log(Day.MONDAY === OtherEnum.CONSTANT);
console.log(Day.MONDAY == OtherEnum.CONSTANT);
Note that the examples above uses ECMAScript 2015 features.
TypeScript
// Typings for the Enum class is not provided, use TypeScripts' enum instead!
enum Direction { NORTH, SOUTH, EAST, WEST }
// No typings for BitFlags either. Again, use TypeScripts' enum!
enum FontStyle {
NORMAL = 0,
BOLD = 1 << 1,
ITALICS = 1 << 2,
UNDERSCORED = 1 << 3,
UPPERCASE = 1 << 4,
MY_FAVORITE = BOLD | ITALICS
}
const configuration: BitSet<FontStyle> = new BitField<FontStyle>();
configuration.on(FontStyle.BOLD | FontStyle.UPPERCASE);
configuration.off(Direction.NORTH); // ERROR: argument type Direction is not assignable to parameter type FontStyle
Installation
From NPM: run npm install easy-bits --save.
From GitHub: download easy-bits.min.js.
and then import
ES2015 style: import { Enum } from 'easy-bits';.
or link in HTML: <script src="easy-bits.min.js"></script>.
Support
| Environment |
Supported Version |
| Node |
8 and up |
| Chrome |
26 and up |
| Firefox |
4 and up |
| Edge |
13 and up |
| Internet Explorer |
10 and up |
| Safari |
7 and up |
Contribute
Use the issue tracker to report bugs or make feature requests. Pull requests are welcome, but it may be a good idea to create an issue to discuss any
changes beforehand.
License
MIT, see LICENSE file.
评论