easy-bits
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

Easy Bits

npm Build Status Quality Status Coverage Status Code Style License

Enums, BitFlags, BitFields, BitMasks and BitArrays for JavaScript & TypeScript.

API Reference

Usage

BitFlags + BitField:

const options = new BitFlags('OPTION1', 'OPTION2', 'OPTION3');
const configuration = options.createBitField();

configuration.on(options.OPTION1 | options.OPTION3); // Set option1 & option3 bits to true
configuration.on(options.OPTION1, options.OPTION3);  // Same as the above

configuration.test(options.OPTION1); // true
configuration.test(options.OPTION2); // false
configuration.test(options.OPTION3); // true
configuration.testAny(options.OPTION1 | options.OPTION2); // true
configuration.testAll(options.OPTION1 | options.OPTION2); // false

configuration.count(); // 2
configuration.flipAll();
configuration.count(); // 1
configuration.test(options.OPTION2); // true

const clone = configuration.clone();

// Serialize
const serializedOptions = options.serialize();        // 'OPTION1,OPTION2,OPTION3'
const serializedBitfield = configuration.serialize(); // '010'
// Deserialize
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);         // EnumConstant('MONDAY':0)
console.log(Day.MONDAY.name);    // 'MONDAY'
console.log(String(Day.MONDAY)); // 'EnumConstant(MONDAY:4)'
console.log(Day.FRIDAY.ordinal); // 4
console.log(Number(Day.FRIDAY)); // 4

// Enums are immutable
Day.MY_OWN_DAY = 1337; // TypeError: Cannot add property MY_OWN_DAY, object is not extensible
Day.MONDAY = 42;       // TypeError: Cannot set property MONDAY of [object Object] which has only a getter

// Enums are iterable
console.log(Object.keys(Day)); // ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', ...]
console.log(Day.values());     // [EnumConstant('MONDAY':0), EnumConstant('TUESDAY':1), ...]
for (let value of Day) {}
Day.forEach((value, name) => {});

// Enums are easy to use
switch (value) {
  case Day.MONDAY:
    break;
  case Day.TUESDAY:
    break;
}

console.log(Day.MONDAY.equals(Day.FRIDAY));     // false
console.log(Day.MONDAY === OtherEnum.CONSTANT); // false
console.log(Day.MONDAY == OtherEnum.CONSTANT);  // false

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.

Package Sidebar

Install

npm i easy-bits

Weekly Downloads

43

Version

1.2.2

License

MIT

Unpacked Size

164 kB

Total Files

16

Last publish

Collaborators

  • easypeasy