Няма описание

mightyplow 92eb095b2a change prebublish script property преди 5 години
src e21747a247 add source file преди 5 години
tests 2600ba15ec add tests преди 5 години
.gitignore 29cfd573d2 add vscode helper file to be ignored преди 5 години
README.md 597211195f fill README преди 5 години
package-lock.json 92eb095b2a change prebublish script property преди 5 години
package.json 92eb095b2a change prebublish script property преди 5 години

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