mentie 0.0.11 → 0.1.2

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.
@@ -17,13 +17,13 @@ export const is_node = typeof process !== 'undefined' && process.versions && pro
17
17
  * Checks if the code is running in a Firebase functions emulator environment.
18
18
  * @returns {boolean} Returns true if the code is running in a Firebase environment, otherwise returns false.
19
19
  */
20
- export const is_emulator = process.env.FUNCTIONS_EMULATOR === 'true'
20
+ export const is_emulator = typeof process !== 'undefined' && process.env?.FUNCTIONS_EMULATOR === 'true'
21
21
 
22
22
  // ///////////////////////////////
23
23
  // Mode and loglevel detection
24
24
  // ///////////////////////////////
25
25
 
26
- const node_dev = process.env.NODE_ENV === 'development'
26
+ const node_dev = typeof process !== 'undefined' && process.env?.NODE_ENV === 'development'
27
27
  const web_dev = typeof location !== 'undefined' && ( `${ location.href }`.includes( 'debug=true' ) || `${ location.href }`.includes( 'localhost' ) )
28
28
 
29
29
  /**
@@ -34,15 +34,15 @@ export const dev = node_dev || web_dev
34
34
 
35
35
  /**
36
36
  * The log level for web applications.
37
- * @type {string}
37
+ * @type {string} - Log level. Valid values are: 'info', 'warn', 'error'
38
38
  */
39
- export const web_loglevel = is_web && new URLSearchParams( location.search ).get( 'loglevel' )
39
+ export const web_loglevel = is_web && new URLSearchParams( location?.search ).get( 'loglevel' )
40
40
 
41
41
  /**
42
42
  * The log level for the Node environment.
43
- * @type {string}
43
+ * @type {string} - Log level. Valid values are: 'info', 'warn', 'error'
44
44
  */
45
- export const node_loglevel = process.env.LOG_LEVEL
45
+ export const node_loglevel = process.env?.LOG_LEVEL
46
46
 
47
47
 
48
48
  /**
@@ -1,5 +1,17 @@
1
1
  // Import environment data
2
- import { dev, loglevel } from "./environment.js"
2
+ import { dev, is_emulator, loglevel } from "./environment.js"
3
+
4
+ const should_log = levels => {
5
+
6
+ // Check if the loglevel matches this call
7
+ const valid_levels = [ 'info', 'warn', 'error' ]
8
+
9
+ // Check if the loglevel is valid
10
+ if( !valid_levels.includes( loglevel ) ) console.warn( `Invalid log level: ${ loglevel }` )
11
+
12
+ return levels.includes( loglevel )
13
+
14
+ }
3
15
 
4
16
  /**
5
17
  * Logs the provided messages to the console.
@@ -15,17 +27,16 @@ import { dev, loglevel } from "./environment.js"
15
27
  export function log( ...messages ) {
16
28
 
17
29
  // Check if the loglevel matches this call
18
- const levels = [ 'error', 'warn', 'info' ]
19
- const should_log = dev || levels.includes( loglevel )
30
+ const levels = [ 'info' ]
20
31
 
21
32
  // Log the messages if the loglevel matches
22
- if( should_log ) console.log( ...messages )
33
+ if( dev || should_log( levels ) ) console.log( ...messages )
23
34
 
24
35
  }
25
36
 
26
37
  /**
27
38
  * Logs the provided info messages to the console.
28
- * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn'
39
+ * Only logs in firebase emulator or if ?loglevel= or LOG_LEVEL= is set to: 'info'
29
40
  * 🎯 Goal: log info trace messages used only for extremely granular debugging
30
41
  * @example log.info( `Retreived key '${ key }' of type '${ typeof key }' from localstorage: `, cache )
31
42
  * @param {...any} messages - The messages to be logged.
@@ -33,17 +44,16 @@ export function log( ...messages ) {
33
44
  log.info = function( ...messages ) {
34
45
 
35
46
  // Check if the loglevel matches this call
36
- const levels = [ 'error', 'warn', 'info' ]
37
- const should_log = dev || levels.includes( loglevel )
47
+ const levels = [ 'info' ]
38
48
 
39
49
  // Log the messages if the loglevel matches
40
- if( should_log ) console.info( ...messages )
50
+ if( is_emulator || should_log( levels ) ) console.info( ...messages )
41
51
 
42
52
  }
43
53
 
44
54
  /**
45
55
  * Logs the provided info messages to the console.
46
- * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn'
56
+ * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'warn', 'info'
47
57
  * 🎯 Goal: log warnings of things that should not happen, but do not break functionality
48
58
  * @example log.warn( `Transaction history was empty, this should never happen: `, history )
49
59
  * @param {...any} messages - The messages to be logged.
@@ -51,17 +61,16 @@ log.info = function( ...messages ) {
51
61
  log.warn = function( ...messages ) {
52
62
 
53
63
  // Check if the loglevel matches this call
54
- const levels = [ 'error', 'warn' ]
55
- const should_log = dev || levels.includes( loglevel )
64
+ const levels = [ 'warn', 'info' ]
56
65
 
57
66
  // Log the messages if the loglevel matches
58
- if( should_log ) console.warn( ...messages )
67
+ if( dev || should_log( levels ) ) console.warn( ...messages )
59
68
 
60
69
  }
61
70
 
62
71
  /**
63
72
  * Logs the provided error messages to the console.
64
- * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error'
73
+ * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn', 'info'
65
74
  * @scope log errors that impact proper functioning of the application
66
75
  * @example log.error( `Error connecting to database: `, error )
67
76
  * @param {...any} messages - The messages to be logged.
@@ -69,9 +78,9 @@ log.warn = function( ...messages ) {
69
78
  log.error = function( ...messages ) {
70
79
 
71
80
  // Check if the loglevel matches this call
72
- const levels = [ 'error' ]
81
+ const levels = [ 'error', 'warn', 'info' ]
73
82
  const should_log = dev || levels.includes( loglevel )
74
- if( !should_log ) return
83
+ if( !dev || !should_log ) return
75
84
 
76
85
  // Log the messages if the loglevel matches
77
86
  console.error( ...messages )
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Rounds a number to the specified number of decimals.
3
+ *
4
+ * @param {number} number - The number to round.
5
+ * @param {number} [decimals=4] - The number of decimals to round to. Default is 4.
6
+ * @returns {number} The rounded number.
7
+ */
1
8
  export const round_number_to_decimals = ( number, decimals=4 ) => {
2
9
 
3
10
  if( number == undefined ) return ''
@@ -7,12 +14,13 @@ export const round_number_to_decimals = ( number, decimals=4 ) => {
7
14
  }
8
15
 
9
16
  /**
10
- * Generates a random number between 1 and the specified maximum number.
17
+ * Generates a random number between a minimum and maximum value.
11
18
  *
12
- * @param {number} max_number - The maximum number for generating the random number.
19
+ * @param {number} max_num - The maximum value for the random number.
20
+ * @param {number} [min_num=1] - The minimum value for the random number
13
21
  * @returns {number} The generated random number.
14
22
  */
15
- export const random_number_of_max = max_number => Math.floor( Math.random() * max_number ) + 1
23
+ export const random_number_between = ( max_num, min_num=1 ) => Math.floor( Math.random() * ( max_num - min_num + 1 ) ) + min_num
16
24
 
17
25
 
18
26
  /**
@@ -21,4 +29,4 @@ export const random_number_of_max = max_number => Math.floor( Math.random() * ma
21
29
  * @param {number} length - The length of the random number.
22
30
  * @returns {number} - The generated random number.
23
31
  */
24
- export const random_number_of_length = length => Number( [ ...Array( length ) ].map( () => random_number_of_max( 9 ) ).join( '' ) )
32
+ export const random_number_of_length = length => parseInt( Array.from( { length }, () => Math.floor( Math.random() * 10 ) ).join( '' ), 10 )
@@ -69,7 +69,7 @@ export async function make_retryable( async_function, { retry_times=5, cooldown_
69
69
  * @returns {Promise<Array>} - A promise that resolves to an array of results from the async functions.
70
70
  * @see {@link https://www.npmjs.com/package/promise-parallel-throttle|promise-parallel-throttle}
71
71
  */
72
- async function throttle_and_retry( async_function_array=[], { max_parallell=2, retry_times, cooldown_in_s, cooldown_entropy, logger, fail_fast=true } ) {
72
+ export async function throttle_and_retry( async_function_array=[], { max_parallel=2, retry_times, cooldown_in_s, cooldown_entropy, logger, fail_fast=true } ) {
73
73
 
74
74
  // Function dependencies
75
75
  const Throttle = await import( 'promise-parallel-throttle' )
@@ -82,7 +82,7 @@ async function throttle_and_retry( async_function_array=[], { max_parallell=2, r
82
82
 
83
83
  // Throttle configuration
84
84
  const throttle_config = {
85
- maxInProgress: max_parallell,
85
+ maxInProgress: max_parallel,
86
86
  failFast: fail_fast,
87
87
  ...logger && { progressCallback: logger }
88
88
  }
package/modules/text.js CHANGED
@@ -9,19 +9,23 @@
9
9
  export const truncate = ( text, length=100, suffix='...' ) => {
10
10
  if( !text ) return ''
11
11
  if( text.length <= length ) return text
12
- return `${ text.slice( 0, length ) }${ suffix }`
12
+ return `${ text.slice( 0, length ).trim() }${ suffix }`
13
13
  }
14
14
 
15
15
  /**
16
16
  * Copies the given text to the clipboard.
17
17
  * @param {string} text - The text to be copied to the clipboard.
18
- * @param {string} [success_message='Copied to clipboard'] - The success message to be displayed after copying.
18
+ * @param {object} options - The options for copying to the clipboard.
19
+ * @param {string} [options.success_message='Copied to clipboard'] - The success message to be displayed after copying.
20
+ * @param {function} [options.alerter] - The custom function to display the success message.
19
21
  * @returns {Promise<void>} - A promise that resolves when the text is successfully copied to the clipboard.
20
22
  */
21
- export const copy_to_clipboard = async ( text, success_message='Copied to clipboard' ) => {
23
+ export const copy_to_clipboard = async ( text, { success_message='Copied to clipboard', alerter } ) => {
22
24
  if( !navigator?.clipboard?.writeText ) return alert( 'Your browser does not support copying to clipboard' )
23
25
  await navigator.clipboard.writeText( text )
24
- alert( success_message )
26
+
27
+ if( alerter ) alerter( success_message )
28
+ else alert( success_message )
25
29
  }
26
30
 
27
31
  /**
@@ -30,4 +34,7 @@ export const copy_to_clipboard = async ( text, success_message='Copied to clipbo
30
34
  * @param {string} string - The input string.
31
35
  * @returns {string} The capitalized string.
32
36
  */
33
- export const capitalise = string => `${ string?.charAt( 0 ).toUpperCase() }${ string?.slice( 1, string?.length ) }`
37
+ export const capitalise = string => {
38
+ if( !string ) return ''
39
+ return `${ string?.charAt( 0 ).toUpperCase() }${ string?.slice( 1, string?.length ) }`
40
+ }
package/modules/time.js CHANGED
@@ -7,9 +7,9 @@
7
7
  export const wait = ( ms, error=false ) => new Promise( ( res, rej ) => setTimeout( error ? rej : res, ms ) )
8
8
 
9
9
  /**
10
- * Converts a timestamp to RFC-822 date format.
10
+ * Converts a timestamp to RFC-822 date format, specifically in GMT.
11
11
  * @param {number} timestamp - The timestamp to convert.
12
- * @returns {string} The RFC-822 formatted date string.
12
+ * @returns {string} The RFC-822 formatted date string in GMT.
13
13
  */
14
14
  export function timestamp_to_RFC822( timestamp ) {
15
15
 
@@ -18,7 +18,7 @@ export function timestamp_to_RFC822( timestamp ) {
18
18
  const months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
19
19
 
20
20
  // Create a date object from the timestamp
21
- const date = new Date( timestamp )
21
+ const date = new Date( timestamp )
22
22
 
23
23
  // Get components of the date
24
24
  const dayOfWeek = days[date.getUTCDay()]
@@ -35,11 +35,10 @@ export function timestamp_to_RFC822( timestamp ) {
35
35
  const minutesStr = minutes < 10 ? '0' + minutes : minutes
36
36
  const secondsStr = seconds < 10 ? '0' + seconds : seconds
37
37
 
38
- // Get the timezone on the running machine
39
- const { timeZone } = Intl.DateTimeFormat().resolvedOptions()
38
+
40
39
 
41
40
  // Construct the RFC-822 date string
42
- return `${ dayOfWeek }, ${ dayStr } ${ month } ${ year } ${ hoursStr }:${ minutesStr }:${ secondsStr } ${ timeZone }`
41
+ return `${ dayOfWeek }, ${ dayStr } ${ month } ${ year } ${ hoursStr }:${ minutesStr }:${ secondsStr } GMT`
43
42
  }
44
43
 
45
44
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.0.11",
3
+ "version": "0.1.2",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",