mentie 0.1.22 → 0.2.23
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/index.js +2 -1
- package/modules/crypto.js +27 -0
- package/modules/environment.js +12 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the hash of the given data using the specified algorithm.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} data - The data to be hashed.
|
|
5
|
+
* @param {string} [algo='sha256'] - The algorithm to be used for hashing. Defaults to 'sha256'.
|
|
6
|
+
* @param {string} [_digest='hex'] - The encoding of the output hash. Defaults to '
|
|
7
|
+
* @returns {Promise<string>} The hashed value of the data.
|
|
8
|
+
*/
|
|
9
|
+
export async function hash( data, algo='sha256', _digest='hex' ) {
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
|
|
13
|
+
// Native node way
|
|
14
|
+
const { createHash } = await import( 'crypto' )
|
|
15
|
+
const _hash = createHash( algo ).update( data ).digest( _digest )
|
|
16
|
+
return _hash
|
|
17
|
+
|
|
18
|
+
} catch {
|
|
19
|
+
|
|
20
|
+
// Dependency-based way
|
|
21
|
+
const { default: hash } = await import( 'hash.js' )
|
|
22
|
+
const _hash = hash[ algo ]().update( data ).digest( _digest )
|
|
23
|
+
return _hash
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
}
|
package/modules/environment.js
CHANGED
|
@@ -17,6 +17,12 @@ export const env = {}
|
|
|
17
17
|
*/
|
|
18
18
|
env.is_web = () => typeof window !== 'undefined'
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @returns {boolean} True if running in a web browser and the URL includes 'localhost', otherwise false.
|
|
23
|
+
*/
|
|
24
|
+
env.is_localhost = () => env.is_web() && `${ location.href }`.includes( 'localhost' )
|
|
25
|
+
|
|
20
26
|
/**
|
|
21
27
|
* Checks if the code is running in the Cypress testing environment within a web browser.
|
|
22
28
|
* @returns {boolean} True if running in Cypress, otherwise false.
|
|
@@ -78,6 +84,12 @@ env.loglevel = () => env.web_loglevel() || env.node_loglevel() || env.dev() ? 'i
|
|
|
78
84
|
*/
|
|
79
85
|
export const is_web = env.is_web()
|
|
80
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Checks if the code is running in a web environment and the URL includes 'localhost'.
|
|
89
|
+
* @returns {boolean} Returns true if the code is running in a web environment and the URL includes 'localhost', otherwise returns false.
|
|
90
|
+
*/
|
|
91
|
+
export const is_localhost = env.is_localhost()
|
|
92
|
+
|
|
81
93
|
/**
|
|
82
94
|
* Checks if the code is running within a Cypress environment.
|
|
83
95
|
* @returns {boolean} Returns true if the code is running in a Cypress environment, otherwise returns false.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mentie",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.23",
|
|
4
4
|
"description": "Mentor's toolbelt",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"promise-parallel-throttle": "^3.5.0",
|
|
32
|
-
"promise-retry": "^2.0.1"
|
|
32
|
+
"promise-retry": "^2.0.1",
|
|
33
|
+
"hash.js": "^1.1.7"
|
|
33
34
|
},
|
|
34
35
|
"dependencies": {
|
|
35
36
|
"safe-stable-stringify": "^2.4.3"
|