Ver código fonte

add jsdoc-to-markdown and configure README build

mightyplow 5 anos atrás
pai
commit
e5e770a378
5 arquivos alterados com 103 adições e 44 exclusões
  1. 9 0
      README.hbs
  2. 44 44
      README.md
  3. 0 0
      global-index.hbs
  4. 2 0
      package.json
  5. 48 0
      src/flags.ts

+ 9 - 0
README.hbs

@@ -0,0 +1,9 @@
+# flags
+A small utility which can be used to manipulate specific bits in a sequence
+of bits.
+
+## Installation
+```
+npm i @mightyplow/flags
+```
+{{>main}}

+ 44 - 44
README.md

@@ -1,45 +1,25 @@
 # 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
 ```
 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.
 
----
-
-### 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.&lt;number&gt;</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'
 
 const Option = {
@@ -50,21 +30,41 @@ const Option = {
 
 const base = 0b000;
 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.&lt;number&gt;</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.&lt;number&gt;</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.&lt;number&gt;</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
-```

+ 0 - 0
global-index.hbs


+ 2 - 0
package.json

@@ -18,7 +18,9 @@
   ],
   "scripts": {
     "compile": "tsc ./src/flags.ts -d -t ESNext --strict --pretty --outDir ./dist/es6",
+    "postcompile": "npm run doc",
     "compile-es5": "npm run compile -- -t ES5 --lib es6 --outDir ./dist/es5",
+    "doc": "jsdoc2md -t README.hbs --partial global-index.hbs --files dist/es6/flags.js > README.md",
     "uglify-es5": "terser -cm -o ./dist/es5/flags.min.js --source-map -- ./dist/es5/flags.js",
     "build-es6": "npm run compile",
     "build-es5": "npm run compile-es5",

+ 48 - 0
src/flags.ts

@@ -10,9 +10,57 @@ const createMultiFlagHandler = <T extends number | boolean>(fn: FlagHandler<T>)
     (flag: number | number[], sequence = 0): T => 
         fn(Array.isArray(flag) ? combine(flag) : flag, sequence) 
 
+/**
+ * Sets one or multiple flags on a sequence of bits.
+ * 
+ * @function
+ * @param {number | number[]} flag The bits which should be set
+ * @param {number} [sequence = 0] The bit sequence to set the flags on
+ * @returns {number}
+ * 
+ * @example
+ * 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"
+ */
 const set = createMultiFlagHandler($set)
+
+/**
+ * Unsets one or multiple flags on a sequence of bits.
+ * 
+ * @function
+ * @param {number | number[]} flag The bits which should be unset
+ * @param {number} [sequence = 0] The bit sequence to set the flags on
+ * @returns {number}
+ */
 const unset = createMultiFlagHandler($unset)
+
+/**
+ * Inverts one or multiple flags on a sequence of bits.
+ * 
+ * @function
+ * @param {number | number[]} flag The bits which should be inverted
+ * @param {number} [sequence = 0] The bit sequence to set the flags on
+ * @returns {number}
+ */
 const toggle = createMultiFlagHandler($toggle)
+
+/**
+ * Check whether one or multiple flags are set. Returns only true when
+ * all requested bits are set.
+ * 
+ * @function
+ * @param {number | number[]} flag The bits which should be inverted
+ * @param {number} [sequence = 0] The bit sequence to set the flags on
+ * @returns {boolean}
+ */
 const isset = createMultiFlagHandler($isset)
 
 export {