@opndev/util 0.0.3 → 0.0.5
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/lib/any.mjs +29 -0
- package/lib/defined.mjs +25 -0
- package/lib/index.mjs +2 -0
- package/lib/ispowtwo.mjs +17 -0
- package/lib/powtwo.mjs +21 -0
- package/package.json +7 -8
package/lib/any.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
* @params {*} needle Anything
|
|
7
|
+
* @params {Array} haystack Anything
|
|
8
|
+
* @returns {Boolean} true if needle is found, false otherwise
|
|
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
|
+
export default function any(needle, haystack) {
|
|
20
|
+
|
|
21
|
+
_assert_array(haystack)
|
|
22
|
+
|
|
23
|
+
if (typeof needle === 'function')
|
|
24
|
+
return haystack.some(needle)
|
|
25
|
+
|
|
26
|
+
return haystack.includes(needle)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { any }
|
package/lib/defined.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if a variable is defined
|
|
3
|
+
* @function defined
|
|
4
|
+
* @params {*} 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
package/lib/ispowtwo.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if something is the power of two
|
|
3
|
+
* @function is_pow_of_two
|
|
4
|
+
* @params {*} what an integer
|
|
5
|
+
* @returns {Boolean} True is something is the power of two
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export default function is_pow_of_two(i) {
|
|
9
|
+
if (!Number.isInteger(i))
|
|
10
|
+
throw new Error("Not a number");
|
|
11
|
+
|
|
12
|
+
return (!(i & (i-1)) && i) ? true : false;
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { is_pow_of_two };
|
|
17
|
+
|
package/lib/powtwo.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the power of two
|
|
3
|
+
* @function pow_of_two
|
|
4
|
+
* @params {*} what an integer
|
|
5
|
+
* @returns {Integer} the power of two
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export default function pow_of_two(i) {
|
|
9
|
+
if (!Number.isInteger(i))
|
|
10
|
+
throw new Error("Not a number");
|
|
11
|
+
|
|
12
|
+
let count = 0;
|
|
13
|
+
while (i != 1) {
|
|
14
|
+
i = i >> 1;
|
|
15
|
+
count++;
|
|
16
|
+
}
|
|
17
|
+
return count;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { pow_of_two };
|
|
21
|
+
|
package/package.json
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opndev/util",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Utility functions for OPN Development",
|
|
5
5
|
"main": "lib/index.mjs",
|
|
6
6
|
"repository": {
|
|
7
|
-
|
|
8
|
-
"url" : "gitlab:opndev/opndev-js-util"
|
|
9
|
-
|
|
7
|
+
"url": "git+https://gitlab.com/opndev/javascipt/opndev-js-util.git"
|
|
10
8
|
},
|
|
11
|
-
"bugs"
|
|
12
|
-
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://gitlab.com/opndev/javascript/opndev-js-util/issues"
|
|
13
11
|
},
|
|
14
|
-
"
|
|
12
|
+
"type": "module",
|
|
13
|
+
"mode": "auto",
|
|
15
14
|
"scripts": {
|
|
16
15
|
"test": "tap"
|
|
17
16
|
},
|
|
@@ -25,7 +24,7 @@
|
|
|
25
24
|
"tap": "latest"
|
|
26
25
|
},
|
|
27
26
|
"files": [
|
|
28
|
-
"lib/*.
|
|
27
|
+
"lib/*.mjs",
|
|
29
28
|
"README.md"
|
|
30
29
|
]
|
|
31
30
|
}
|