@opndev/util 0.0.5 → 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 +32 -0
- package/README.md +7 -25
- package/lib/any.mjs +4 -12
- package/lib/defined.mjs +1 -1
- package/lib/index.mjs +11 -0
- package/lib/ispowtwo.mjs +9 -3
- package/lib/none.mjs +21 -0
- package/lib/powtwo.mjs +11 -4
- package/lib/range.mjs +19 -0
- package/package.json +8 -4
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
|
-
|
|
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`
|
|
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
CHANGED
|
@@ -3,22 +3,14 @@
|
|
|
3
3
|
* a primitive. Objects tend to be harder. Use lodash instead.
|
|
4
4
|
*
|
|
5
5
|
* @function any
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
6
|
+
* @param {*} needle Anything
|
|
7
|
+
* @param {Array} haystack Anything
|
|
8
8
|
* @returns {Boolean} true if needle is found, false otherwise
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
function _assert_array(haystack) {
|
|
12
|
-
|
|
13
|
-
if (Array.isArray(haystack))
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
throw "haystack is not an array";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
10
|
export default function any(needle, haystack) {
|
|
20
11
|
|
|
21
|
-
|
|
12
|
+
if (!Array.isArray(haystack))
|
|
13
|
+
throw "haystack is not an array";
|
|
22
14
|
|
|
23
15
|
if (typeof needle === 'function')
|
|
24
16
|
return haystack.some(needle)
|
package/lib/defined.mjs
CHANGED
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'
|
package/lib/ispowtwo.mjs
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Check if something is the power of two
|
|
3
3
|
* @function is_pow_of_two
|
|
4
|
-
* @
|
|
4
|
+
* @param {Integer} what The number you want to validate as the power of two
|
|
5
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"
|
|
6
11
|
*/
|
|
7
12
|
|
|
8
13
|
export default function is_pow_of_two(i) {
|
|
9
|
-
|
|
14
|
+
const parsed = Number.parseInt(i, 10);
|
|
15
|
+
if (Number.isNaN(parsed))
|
|
10
16
|
throw new Error("Not a number");
|
|
11
17
|
|
|
12
|
-
return (!(
|
|
18
|
+
return (!(parsed & (parsed-1)) && parsed) ? true : false;
|
|
13
19
|
|
|
14
20
|
}
|
|
15
21
|
|
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
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Returns the power of two
|
|
3
3
|
* @function pow_of_two
|
|
4
|
-
* @
|
|
4
|
+
* @param {*} what an integer
|
|
5
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)
|
|
6
12
|
*/
|
|
7
13
|
|
|
8
14
|
export default function pow_of_two(i) {
|
|
9
|
-
|
|
15
|
+
let parsed = Number.parseInt(i, 10);
|
|
16
|
+
if (Number.isNaN(parsed))
|
|
10
17
|
throw new Error("Not a number");
|
|
11
18
|
|
|
12
19
|
let count = 0;
|
|
13
|
-
while (
|
|
14
|
-
|
|
20
|
+
while (parsed != 1) {
|
|
21
|
+
parsed = parsed >> 1;
|
|
15
22
|
count++;
|
|
16
23
|
}
|
|
17
24
|
return count;
|
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,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opndev/util",
|
|
3
|
-
"version": "0.0.
|
|
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": "git+https://gitlab.com/opndev/
|
|
8
|
+
"url": "git+https://gitlab.com/opndev/javascript/opndev-js-util.git"
|
|
8
9
|
},
|
|
9
10
|
"bugs": {
|
|
10
11
|
"url": "https://gitlab.com/opndev/javascript/opndev-js-util/issues"
|
|
@@ -12,7 +13,8 @@
|
|
|
12
13
|
"type": "module",
|
|
13
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
30
|
"lib/*.mjs",
|
|
28
|
-
"README.md"
|
|
31
|
+
"README.md",
|
|
32
|
+
"Changes"
|
|
29
33
|
]
|
|
30
34
|
}
|