|
@@ -1,45 +1,25 @@
|
|
# flags
|
|
# 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.
|
|
|
|
|
|
+A small utility which can be used to manipulate specific bits in a sequence
|
|
|
|
+of bits.
|
|
|
|
|
|
## Installation
|
|
## Installation
|
|
```
|
|
```
|
|
npm i @mightyplow/flags
|
|
npm i @mightyplow/flags
|
|
```
|
|
```
|
|
|
|
+<a name="set"></a>
|
|
|
|
|
|
-## API
|
|
|
|
-
|
|
|
|
-### set (flag: number | number[], sequence = 0): number
|
|
|
|
-
|
|
|
|
|
|
+## set(flag, [sequence]) ⇒ <code>number</code>
|
|
Sets one or multiple flags on a sequence of bits.
|
|
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.
|
|
|
|
|
|
+**Kind**: global function
|
|
|
|
|
|
----
|
|
|
|
|
|
+| Param | Type | Default | Description |
|
|
|
|
+| --- | --- | --- | --- |
|
|
|
|
+| flag | <code>number</code> \| <code>Array.<number></code> | | The bits which should be set |
|
|
|
|
+| [sequence] | <code>number</code> | <code>0</code> | The bit sequence to set the flags on |
|
|
|
|
|
|
-### toggle (flag: number | number[], sequence = 0): number
|
|
|
|
-
|
|
|
|
-Inverts one or multiple flags on a sequence of bits.
|
|
|
|
-
|
|
|
|
----
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-## Examples
|
|
|
|
-
|
|
|
|
-### Store option flags
|
|
|
|
-
|
|
|
|
-```
|
|
|
|
|
|
+**Example**
|
|
|
|
+```js
|
|
import { set } from '@mightyplow/flags'
|
|
import { set } from '@mightyplow/flags'
|
|
|
|
|
|
const Option = {
|
|
const Option = {
|
|
@@ -50,21 +30,41 @@ const Option = {
|
|
|
|
|
|
const base = 0b000;
|
|
const base = 0b000;
|
|
console.log(set([Option.foo, Option.baz], base).toSting(2)) // "101"
|
|
console.log(set([Option.foo, Option.baz], base).toSting(2)) // "101"
|
|
-
|
|
|
|
```
|
|
```
|
|
|
|
+<a name="unset"></a>
|
|
|
|
|
|
-### Create a function to check flags
|
|
|
|
|
|
+## unset(flag, [sequence]) ⇒ <code>number</code>
|
|
|
|
+Unsets one or multiple flags on a sequence of bits.
|
|
|
|
|
|
-```
|
|
|
|
-import { isset } from '@mightyplow/flags'
|
|
|
|
|
|
+**Kind**: global function
|
|
|
|
|
|
-const Option = {
|
|
|
|
- foo: 2 ** 0,
|
|
|
|
- bar: 2 ** 1,
|
|
|
|
- baz: 2 ** 2
|
|
|
|
-}
|
|
|
|
|
|
+| Param | Type | Default | Description |
|
|
|
|
+| --- | --- | --- | --- |
|
|
|
|
+| flag | <code>number</code> \| <code>Array.<number></code> | | The bits which should be unset |
|
|
|
|
+| [sequence] | <code>number</code> | <code>0</code> | The bit sequence to set the flags on |
|
|
|
|
+
|
|
|
|
+<a name="toggle"></a>
|
|
|
|
+
|
|
|
|
+## toggle(flag, [sequence]) ⇒ <code>number</code>
|
|
|
|
+Inverts one or multiple flags on a sequence of bits.
|
|
|
|
+
|
|
|
|
+**Kind**: global function
|
|
|
|
+
|
|
|
|
+| Param | Type | Default | Description |
|
|
|
|
+| --- | --- | --- | --- |
|
|
|
|
+| flag | <code>number</code> \| <code>Array.<number></code> | | The bits which should be inverted |
|
|
|
|
+| [sequence] | <code>number</code> | <code>0</code> | The bit sequence to set the flags on |
|
|
|
|
+
|
|
|
|
+<a name="isset"></a>
|
|
|
|
+
|
|
|
|
+## isset(flag, [sequence]) ⇒ <code>boolean</code>
|
|
|
|
+Check whether one or multiple flags are set. Returns only true when
|
|
|
|
+all requested bits are set.
|
|
|
|
+
|
|
|
|
+**Kind**: global function
|
|
|
|
+
|
|
|
|
+| Param | Type | Default | Description |
|
|
|
|
+| --- | --- | --- | --- |
|
|
|
|
+| flag | <code>number</code> \| <code>Array.<number></code> | | The bits which should be inverted |
|
|
|
|
+| [sequence] | <code>number</code> | <code>0</code> | The bit sequence to set the flags on |
|
|
|
|
|
|
-const hasFooAndBaz = (flags) => isset([Option.foo, Option.baz], flags)
|
|
|
|
-console.log(hasFooAndBaz(0b101)) // true
|
|
|
|
-console.log(hasFooAndBaz(0b001)) // false
|
|
|
|
-```
|
|
|