mightyplow 5 年 前
コミット
597211195f
1 ファイル変更70 行追加0 行削除
  1. 70 0
      README.md

+ 70 - 0
README.md

@@ -0,0 +1,70 @@
+# 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
+```