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

bitmask-decorator

v1.0.8

Published

TypeScript decorators to facilitate storing of boolean features as bitwise.

Downloads

11

Readme

Bitmask Decorator

Install

yarn add bitmask-decorator

What is this?

Suppose you are creating a game that uses pawns. Each token has four boolean features. Something like: high or low, light or dark, round or square, with hole or without hole. If you use a boolean, each characteristic will require a byte.

class Pawn {
  round: boolean = true;
  dark: boolean = true;
  tall: boolean = true;
  hole: boolean = true;
}

This library offers two TypeScript decorators that allow these features to be stored in the form of bitwise:

import {
  bitmask as mask,
  bitflag as flag,
  bitwise,
  Bitmask,
} from "bitmask-decorator";

interface Test extends BitwiseMask {}

@mask
class Test {
  @flag(true) private foo!: bitwise;
  @flag(false) private bar!: bitwise;
  @flag(1) private baz!: bitwise;
  @flag(0) private bam!: bitwise;
}
  • @bitmask:

    • Needed to process the @bitflags at instantiation time.
  • @bitflag:

    • The decorator's argument is the initial state of the bit.
    • private is necessary to prevent autocompletion of the property (which was deleted by the decorator anyway).
    • The type bitwise is number | boolean.
    • The ! is needed to suppress 'baz' is declared but its value is never read. ts(6133).

A class namesake interface is needed for TypeScript to infer the bitwise-related methods generated.

Methods

binary

binary(bits?: 8 | 16 | 24 | 32): string

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(false) private bar!: bitwise
    @flag(1) private baz!: bitwise
}

const t = new Test()
t.binary()                              // 0b00000101
t.binary(8)                             // 0b00000101
t.binary(16)                    // 0b0000000000000101
t.binary(24)            // 0b000000000000000000000101
t.binary(32)    // 0b00000000000000000000000000000101

getBitwiseMask

getBitwiseMask(): number

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(false) private bar!: bitwise
    @flag(1) private baz!: bitwise
}

const t = new Test()
t.getBitwiseMask() === 5 // true
t.getBitwiseMask() === b.w1 | b.w3 // true

isBitwiseMask

isBitwiseMask(bitwise: number): boolean

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(false) private bar!: bitwise
    @flag(1) private baz!: bitwise
}

const t = new Test()
t.isBitwiseMask(5) // true
t.getBitwiseMask(b.w1 | b.w3) // true
t.getBitwiseMask(b.w2) // false
t.getBitwiseMask(b.w1 | b.w2) // false

setBitwiseMask

setBitwiseMask(bitwise: number): this

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(false) private bar!: bitwise
    @flag(1) private baz!: bitwise
}

const t = new Test()
t.binary() // 0b00000101
t.setBitwiseMask(2).setBitwiseMask(b.24)
t.binary() // 0b00001111

clearBitwiseMask

clearBitwiseMask(bitwise: number): this

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(true) private bar!: bitwise
    @flag(true) private baz!: bitwise
    @flag(true) private yop!: bitwise
}

const t = new Test()
t.binary() // 0b00001111
t.clearBitwiseMask(b.w1).clearBitwiseMask(b.w2 | b.w3)
t.binary() // 0b00001000

toggleBitwiseMask

toggleBitwiseMask(bitwise: number): this

@mask
class Test {
    @flag(true) private foo!: bitwise
    @flag(false) private bar!: bitwise
    @flag(true) private baz!: bitwise
    @flag(false) private yop!: bitwise
}

const t = new Test()
t.binary() // 0b00000101
t.toggleBitwiseMask(b.w1).toggleBitwiseMask(b.w2 | b.w3 | b.w4)
t.binary() // 0b00001010

Utilities

b.w

Avoid typos with b:

import { b } from "./lib/main"

Where b is the following object:

const b = {
    w1: 1,
    w2: 2,
    w3: 4,
    w4: 8,
    w5: 16,
    w6: 32,
    w7: 64,
    w8: 128
}

Tests

To run unit tests run:

yarn test