mentie 0.3.20 → 0.3.22

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.
@@ -3,17 +3,21 @@
3
3
  * @param {Array} array
4
4
  * @returns {Array} The shuffled array.
5
5
  */
6
- export const shuffle_array = array => {
6
+ export const shuffle_array = ( array, in_place=true ) => {
7
+
7
8
 
8
9
  // Validate input
9
10
  if( Array.isArray( array ) === false ) throw new Error( 'Input must be an array' )
10
11
 
12
+ // Handle in-place shuffling
13
+ const to_shuffle = in_place ? array : [ ...array ]
14
+
11
15
  // Fisher-Yates shuffle
12
- for( let i = array.length - 1; i > 0; i-- ) {
16
+ for( let i = to_shuffle.length - 1; i > 0; i-- ) {
13
17
  const j = Math.floor( Math.random() * ( i + 1 ) );
14
- [ array[i], array[j] ] = [ array[j], array[i] ]
18
+ [ to_shuffle[i], to_shuffle[j] ] = [ to_shuffle[j], to_shuffle[i] ]
15
19
  }
16
20
 
17
- return array
21
+ return to_shuffle
18
22
 
19
23
  }
@@ -7,13 +7,26 @@ 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
+ * Trims whitespace from each line of a multiline string.
12
+ * @param {string} string - The multiline string to trim.
13
+ * @returns {string} - The trimmed multiline string.
14
+ */
15
+ export const multiline_trim = string => `${ string }`.split( '\n' ).map( s => s?.trim() ).join( '\n' ).trim()
16
+
10
17
  /**
11
18
  * Sanitizes a string by removing leading and trailing whitespace and converting it to lowercase.
12
19
  *
13
20
  * @param {string} string - The string to be sanitized.
21
+ * @param {boolean} [multiline=true] - Whether to trim each line of a multiline string.
14
22
  * @returns {string} - The sanitized string.
15
23
  */
16
- export const sanetise_string = string => `${ string }`.trim().toLowerCase()
24
+ export const sanetise_string = ( string, multiline=true ) => {
25
+ string = `${ string }`.toLowerCase()
26
+ if( multiline ) string = multiline_trim( string )
27
+ else string = string.trim()
28
+ return string
29
+ }
17
30
 
18
31
  /**
19
32
  * Validates whether a given string is a valid IPv4 address.
@@ -23,7 +36,7 @@ export const sanetise_string = string => `${ string }`.trim().toLowerCase()
23
36
  export const is_ipv4 = ip => {
24
37
 
25
38
  // Split the IP address into its components and make then numbers
26
- const octets = ip.split( '.' ).map( octet => parseInt( octet, 10 ) )
39
+ const octets = `${ ip }`.split( '.' ).map( octet => parseInt( octet, 10 ) )
27
40
 
28
41
  // Check if the IP address has 4 octets and each octet is between 0 and 255
29
42
  return octets.length === 4 && octets.every( octet => !isNaN( octet ) && octet >= 0 && octet <= 255 )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.3.20",
3
+ "version": "0.3.22",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",