Nessuna descrizione

mightyplow 86153093bd extract FlagHandler type 5 anni fa
src 86153093bd extract FlagHandler type 5 anni fa
tests 2600ba15ec add tests 5 anni fa
.gitignore 29cfd573d2 add vscode helper file to be ignored 5 anni fa
README.md 597211195f fill README 5 anni fa
package-lock.json 4436982669 1.1.2 5 anni fa
package.json 4436982669 1.1.2 5 anni fa

README.md

flags

Utilites to manipulate single bits in a sequence of bits. This can be used to store specific boolean flags in a memory saving manor.

Installation

npm i @mightyplow/flags

API

set (flag: number | number[], sequence = 0): number

Sets one or multiple flags on a sequence of bits.


unset (flag: number | number[], sequence = 0): number

Unsets one or multiple flags on a sequence of bits.


isset (flag: number | number[], sequence = 0): boolean

Checks one or multiple flags on a sequence of bits. Returns true, when all requested bits are set to 1.


toggle (flag: number | number[], sequence = 0): number

Inverts one or multiple flags on a sequence of bits.


Examples

Store option flags

import { set } from '@mightyplow/flags'

const Option = {
    foo: 2 ** 0,
    bar: 2 ** 1,
    baz: 2 ** 2
}

const base = 0b000;
console.log(set([Option.foo, Option.baz], base).toSting(2)) // "101"

Create a function to check flags

import { isset } from '@mightyplow/flags'

const Option = {
    foo: 2 ** 0,
    bar: 2 ** 1,
    baz: 2 ** 2
}

const hasFooAndBaz = (flags) => isset([Option.foo, Option.baz], flags)
console.log(hasFooAndBaz(0b101)) // true
console.log(hasFooAndBaz(0b001)) // false