mentie 0.3.21 → 0.3.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/modules/environment.js +1 -1
- package/modules/manipulations.js +54 -0
- package/modules/validations.js +1 -38
- package/package.json +1 -1
package/modules/environment.js
CHANGED
|
@@ -63,7 +63,7 @@ env.dev = () => env.node_dev() || env.web_dev()
|
|
|
63
63
|
* Retrieves the log level set via URL parameters in a web environment.
|
|
64
64
|
* @returns {string|null} The log level from URL parameters, or null if not set.
|
|
65
65
|
*/
|
|
66
|
-
env.web_loglevel = () => env.is_web() && new URLSearchParams( location?.search ).get( 'loglevel' )
|
|
66
|
+
env.web_loglevel = () => env.is_web() && new URLSearchParams( location?.search ).get( 'loglevel' ) || new URLSearchParams( location?.search ).get( 'log_level' )
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Retrieves the log level set via environment variables in a Node.js environment.
|
package/modules/manipulations.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { is_ipv4 } from "./validations"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Shuffle an array using the Fisher-Yates algorithm.
|
|
3
5
|
* @param {Array} array
|
|
@@ -20,4 +22,56 @@ export const shuffle_array = ( array, in_place=true ) => {
|
|
|
20
22
|
|
|
21
23
|
return to_shuffle
|
|
22
24
|
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Trims whitespace from each line of a multiline string.
|
|
29
|
+
* @param {string} string - The multiline string to trim.
|
|
30
|
+
* @returns {string} - The trimmed multiline string.
|
|
31
|
+
*/
|
|
32
|
+
export const multiline_trim = string => `${ string }`.split( '\n' ).map( s => s?.trim() ).join( '\n' ).trim()
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Sanitizes a string by removing leading and trailing whitespace and converting it to lowercase.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} string - The string to be sanitized.
|
|
38
|
+
* @param {boolean} [multiline=true] - Whether to trim each line of a multiline string.
|
|
39
|
+
* @returns {string} - The sanitized string.
|
|
40
|
+
*/
|
|
41
|
+
export const sanetise_string = ( string, multiline=true ) => {
|
|
42
|
+
string = `${ string }`.toLowerCase()
|
|
43
|
+
if( multiline ) string = multiline_trim( string )
|
|
44
|
+
else string = string.trim()
|
|
45
|
+
return string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Sanitizes and optionally validates an IPv4 address.
|
|
50
|
+
* @param {Object} options - The options object.
|
|
51
|
+
* @param {string} options.ip - The IP address to sanitize.
|
|
52
|
+
* @param {boolean} [options.validate=false] - Whether to validate the IP address.
|
|
53
|
+
* @param {boolean} [options.error_on_invalid=false] - Whether to throw an error on an invalid IP.
|
|
54
|
+
* @returns {string|null} The sanitized IPv4 address, or null if validation fails and error_on_invalid is false.
|
|
55
|
+
* @throws {Error} If the IP address is not provided or invalid when error_on_invalid is true.
|
|
56
|
+
*/
|
|
57
|
+
export const sanetise_ipv4 = ( { ip, validate=false, error_on_invalid=false } ) => {
|
|
58
|
+
|
|
59
|
+
// Ensure ip was provided
|
|
60
|
+
if( !ip ) throw new Error( 'IP address is required' )
|
|
61
|
+
|
|
62
|
+
// Sanetise ip address as string
|
|
63
|
+
ip = sanetise_string( ip )
|
|
64
|
+
|
|
65
|
+
// Remove ipv6 prefix if present
|
|
66
|
+
ip = ip.replace( '::ffff:', '' )
|
|
67
|
+
|
|
68
|
+
// Check if the IP address is valid
|
|
69
|
+
if( validate && !is_ipv4( ip ) ) {
|
|
70
|
+
if( error_on_invalid ) throw new Error( `Invalid IPv4 address: ${ ip }` )
|
|
71
|
+
return null
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Return the sanitized IP address
|
|
75
|
+
return ip
|
|
76
|
+
|
|
23
77
|
}
|
package/modules/validations.js
CHANGED
|
@@ -7,13 +7,6 @@ import stringify from "safe-stable-stringify"
|
|
|
7
7
|
*/
|
|
8
8
|
export const email_regex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Sanitizes a string by removing leading and trailing whitespace and converting it to lowercase.
|
|
12
|
-
*
|
|
13
|
-
* @param {string} string - The string to be sanitized.
|
|
14
|
-
* @returns {string} - The sanitized string.
|
|
15
|
-
*/
|
|
16
|
-
export const sanetise_string = string => `${ string }`.trim().toLowerCase()
|
|
17
10
|
|
|
18
11
|
/**
|
|
19
12
|
* Validates whether a given string is a valid IPv4 address.
|
|
@@ -23,7 +16,7 @@ export const sanetise_string = string => `${ string }`.trim().toLowerCase()
|
|
|
23
16
|
export const is_ipv4 = ip => {
|
|
24
17
|
|
|
25
18
|
// Split the IP address into its components and make then numbers
|
|
26
|
-
const octets = ip
|
|
19
|
+
const octets = `${ ip }`.split( '.' ).map( octet => parseInt( octet, 10 ) )
|
|
27
20
|
|
|
28
21
|
// Check if the IP address has 4 octets and each octet is between 0 and 255
|
|
29
22
|
return octets.length === 4 && octets.every( octet => !isNaN( octet ) && octet >= 0 && octet <= 255 )
|
|
@@ -49,36 +42,6 @@ export const is_ipv6 = ip => {
|
|
|
49
42
|
|
|
50
43
|
}
|
|
51
44
|
|
|
52
|
-
/**
|
|
53
|
-
* Sanitizes and optionally validates an IPv4 address.
|
|
54
|
-
* @param {Object} options - The options object.
|
|
55
|
-
* @param {string} options.ip - The IP address to sanitize.
|
|
56
|
-
* @param {boolean} [options.validate=false] - Whether to validate the IP address.
|
|
57
|
-
* @param {boolean} [options.error_on_invalid=false] - Whether to throw an error on an invalid IP.
|
|
58
|
-
* @returns {string|null} The sanitized IPv4 address, or null if validation fails and error_on_invalid is false.
|
|
59
|
-
* @throws {Error} If the IP address is not provided or invalid when error_on_invalid is true.
|
|
60
|
-
*/
|
|
61
|
-
export const sanetise_ipv4 = ( { ip, validate=false, error_on_invalid=false } ) => {
|
|
62
|
-
|
|
63
|
-
// Ensure ip was provided
|
|
64
|
-
if( !ip ) throw new Error( 'IP address is required' )
|
|
65
|
-
|
|
66
|
-
// Sanetise ip address as string
|
|
67
|
-
ip = sanetise_string( ip )
|
|
68
|
-
|
|
69
|
-
// Remove ipv6 prefix if present
|
|
70
|
-
ip = ip.replace( '::ffff:', '' )
|
|
71
|
-
|
|
72
|
-
// Check if the IP address is valid
|
|
73
|
-
if( validate && !is_ipv4( ip ) ) {
|
|
74
|
-
if( error_on_invalid ) throw new Error( `Invalid IPv4 address: ${ ip }` )
|
|
75
|
-
return null
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Return the sanitized IP address
|
|
79
|
-
return ip
|
|
80
|
-
|
|
81
|
-
}
|
|
82
45
|
|
|
83
46
|
/**
|
|
84
47
|
* Checks if an object contains all the required properties.
|