@opndev/util 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changes ADDED
@@ -0,0 +1,32 @@
1
+ Revision history for @opndev/util
2
+
3
+ {{NEXT}}
4
+
5
+ * Maintainer release:
6
+ * Make use of gitlab pages for documenation
7
+ * Add none() function
8
+ * Add range() function
9
+ * Bugfix: Allow string values in pow_* functions
10
+
11
+ 0.0.5 2023-08-27 15:33:21-0400
12
+
13
+ * Include .mjs files in package
14
+
15
+ 0.0.4 2023-08-27 15:13:21-0400
16
+
17
+ * Add two mathematical functions:
18
+ * is_pow_of_two
19
+ * pow_of_two
20
+
21
+ 0.0.3 2021-11-12 17:15:02-0400
22
+
23
+ * maintainer release for ESM
24
+
25
+ 0.0.2 2020-05-15 16:19:23Z
26
+
27
+ * Allow any() to check for integers as well
28
+ * Update README.md
29
+
30
+ 0.0.1 2020-05-13 02:42:33Z
31
+
32
+ * First release
package/README.md CHANGED
@@ -3,28 +3,7 @@
3
3
  This is a utility function package that tries to solve issues by keeping things
4
4
  DRY. It is an ESM package.
5
5
 
6
- ## Exports
7
-
8
- This module exports two functions:
9
-
10
- * any(needle, haystack)
11
- * defined(something)
12
-
13
- ##
14
-
15
- ```
16
- import { any, defined } from '@opndev/util'
17
-
18
- let foo;
19
- if (defined(foo)) {
20
- // do things
21
- }
22
-
23
- if (any('bar', ['foo', 'bar', 'baz'])) {
24
- // do things
25
- }
26
-
27
- ```
6
+ [If you want to know more, have a look at our documenation](https://opndev.gitlab.io/javascript/opndev-js-util/)
28
7
 
29
8
  ## Development
30
9
 
@@ -32,11 +11,14 @@ Try to keep functions small and try not to depend on any outside functions. We
32
11
  aim to keep the dependency graph as small as possible. The use of `esm` is
33
12
  welcomed and writing tests is encouraged.
34
13
 
14
+ ### jsdoc
15
+
16
+ You can build the documentation by running `npm run jsdoc`.
17
+
35
18
  ### eslint
36
19
 
37
20
  The ES Linting profile is flexible and does not try to enforce much. There is
38
- more than one way to do it:
39
- [TIMTOWTDI](https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it)
21
+ more than one way to do it ([TIMTOWTDI](https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it)).
40
22
 
41
23
  Try to stay constistent, but forcing a programming style upon others is bad.
42
24
 
@@ -46,7 +28,7 @@ Please educate yourself and use a global gitignore file. Somes files will
46
28
  always be there. I'm looking at you `node_modules`.
47
29
 
48
30
  ### The lockfiles
49
- You'll also see that `package-lock.json` and/or `yarn.lock` is ignored. I
31
+ You'll also see that `package-lock.json` and/or `yarn.lock` are ignored. I
50
32
  strongly believe that pinning packages lead to bit rot, as you never update
51
33
  packages. When a downstream module breaks its API you should know and update
52
34
  your code accordingly. The same goes for possible upstream modules. If you want
package/lib/any.mjs ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Check if a value is preset in the given array, can be done via a function or
3
+ * a primitive. Objects tend to be harder. Use lodash instead.
4
+ *
5
+ * @function any
6
+ * @param {*} needle Anything
7
+ * @param {Array} haystack Anything
8
+ * @returns {Boolean} true if needle is found, false otherwise
9
+ */
10
+ export default function any(needle, haystack) {
11
+
12
+ if (!Array.isArray(haystack))
13
+ throw "haystack is not an array";
14
+
15
+ if (typeof needle === 'function')
16
+ return haystack.some(needle)
17
+
18
+ return haystack.includes(needle)
19
+ }
20
+
21
+ export { any }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Check if a variable is defined
3
+ * @function defined
4
+ * @param {*} what Anything really
5
+ * @returns {Boolean} true if defined, false otherwise
6
+ */
7
+
8
+ export default function defined(what) {
9
+ if (what === null)
10
+ return false
11
+
12
+ if (typeof what === 'undefined')
13
+ return false
14
+
15
+ /* This will probably never be hit:
16
+ * https://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined
17
+ */
18
+ if (what === undefined)
19
+ return false
20
+
21
+ return true
22
+ }
23
+
24
+ export { defined };
25
+
package/lib/index.mjs CHANGED
@@ -1,9 +1,20 @@
1
1
  /**
2
+ *
3
+ * @module module:@opndev/utils
4
+ *
5
+ * @exports any
6
+ * @exports none
7
+ * @exports defined
8
+ * @exports pow_of_two
9
+ * @exports is_pow_of_two
10
+ *
2
11
  * Export all the useful functions. Unfortunatly, without webpack's
3
12
  * require.context we cannot automaticly load everything. So we just keep
4
13
  * typing. ES6 requires strict syntax on import and exports :/
5
14
  */
6
15
  export { any } from './any.mjs'
16
+ export { none } from './none.mjs'
7
17
  export { defined } from './defined.mjs'
8
18
  export { pow_of_two } from './powtwo.mjs'
9
19
  export { is_pow_of_two } from './ispowtwo.mjs'
20
+ export { range } from './range.mjs'
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Check if something is the power of two
3
+ * @function is_pow_of_two
4
+ * @param {Integer} what The number you want to validate as the power of two
5
+ * @returns {Boolean} True is something is the power of two
6
+ *
7
+ * @example
8
+ * // A tournamen draw has to be a power of two
9
+ * if (is_pow_of_two(draw))
10
+ * throw "A draw must be a power of two"
11
+ */
12
+
13
+ export default function is_pow_of_two(i) {
14
+ const parsed = Number.parseInt(i, 10);
15
+ if (Number.isNaN(parsed))
16
+ throw new Error("Not a number");
17
+
18
+ return (!(parsed & (parsed-1)) && parsed) ? true : false;
19
+
20
+ }
21
+
22
+ export { is_pow_of_two };
23
+
package/lib/none.mjs ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Check if a value is not preset in the given array, can be done via a
3
+ * function or a primitive.
4
+ *
5
+ * @function none
6
+ * @param {*} needle Anything
7
+ * @param {Array} haystack Anything
8
+ * @returns {Boolean} true if needle isnt found, false otherwise
9
+ */
10
+
11
+ export default function none(needle, haystack) {
12
+ if (!Array.isArray(haystack))
13
+ throw "haystack is not an array";
14
+
15
+ if (typeof needle === 'function')
16
+ return haystack.some(needle) ? false : true
17
+
18
+ return haystack.includes(needle) ? false : true
19
+ }
20
+
21
+ export { none }
package/lib/powtwo.mjs ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Returns the power of two
3
+ * @function pow_of_two
4
+ * @param {*} what an integer
5
+ * @returns {Integer} the power of two
6
+ *
7
+ * @example
8
+ * // Based on the amount of players you can extract how many rounds a
9
+ * tournament has (in single elimination
10
+ *
11
+ * const rounds = pow_of_two(players)
12
+ */
13
+
14
+ export default function pow_of_two(i) {
15
+ let parsed = Number.parseInt(i, 10);
16
+ if (Number.isNaN(parsed))
17
+ throw new Error("Not a number");
18
+
19
+ let count = 0;
20
+ while (parsed != 1) {
21
+ parsed = parsed >> 1;
22
+ count++;
23
+ }
24
+ return count;
25
+ }
26
+
27
+ export { pow_of_two };
28
+
package/lib/range.mjs ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Return a range
3
+ * @function range
4
+ * @param start {Int}
5
+ * @param stop {Int}
6
+ * @returns {Array} An array with the requested values
7
+ */
8
+
9
+ export default function range(start, stop, step = 1) {
10
+ return Array.from({
11
+ length: (stop - start) / step + 1
12
+ },
13
+ (_, i) => start + i * step
14
+ );
15
+ }
16
+
17
+ export {
18
+ range
19
+ };
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@opndev/util",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Utility functions for OPN Development",
5
5
  "main": "lib/index.mjs",
6
+ "homepage": "https://opndev.gitlab.io/javascript/opndev-js-util/",
6
7
  "repository": {
7
- "url" : "gitlab:opndev/javascipt/opndev-js-util"
8
+ "url": "git+https://gitlab.com/opndev/javascript/opndev-js-util.git"
8
9
  },
9
- "bugs" : {
10
- "url" : "https://gitlab.com/opndev/javascript/opndev-js-util/issues"
10
+ "bugs": {
11
+ "url": "https://gitlab.com/opndev/javascript/opndev-js-util/issues"
11
12
  },
12
13
  "type": "module",
13
- "mode":"auto",
14
+ "mode": "auto",
14
15
  "scripts": {
15
- "test": "tap"
16
+ "test": "tap",
17
+ "jsdoc" : "jsdoc -c jsdoc.json"
16
18
  },
17
19
  "author": {
18
20
  "name": "Wesley Schwengle",
@@ -21,10 +23,12 @@
21
23
  },
22
24
  "license": "MIT",
23
25
  "devDependencies": {
26
+ "jsdoc" : "latest",
24
27
  "tap": "latest"
25
28
  },
26
29
  "files": [
27
- "lib/*.js",
28
- "README.md"
30
+ "lib/*.mjs",
31
+ "README.md",
32
+ "Changes"
29
33
  ]
30
34
  }