mentie 0.0.11 → 0.1.0

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
  /**
@@ -36,13 +36,13 @@ export const dev = node_dev || web_dev
36
36
  * The log level for web applications.
37
37
  * @type {string}
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
43
  * @type {string}
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
  /**
@@ -15,7 +15,7 @@ import { dev, loglevel } from "./environment.js"
15
15
  export function log( ...messages ) {
16
16
 
17
17
  // Check if the loglevel matches this call
18
- const levels = [ 'error', 'warn', 'info' ]
18
+ const levels = [ 'info' ]
19
19
  const should_log = dev || levels.includes( loglevel )
20
20
 
21
21
  // Log the messages if the loglevel matches
@@ -25,7 +25,7 @@ export function log( ...messages ) {
25
25
 
26
26
  /**
27
27
  * 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'
28
+ * Only logs if ?loglevel= or LOG_LEVEL= is set to: 'info'
29
29
  * 🎯 Goal: log info trace messages used only for extremely granular debugging
30
30
  * @example log.info( `Retreived key '${ key }' of type '${ typeof key }' from localstorage: `, cache )
31
31
  * @param {...any} messages - The messages to be logged.
@@ -33,8 +33,8 @@ export function log( ...messages ) {
33
33
  log.info = function( ...messages ) {
34
34
 
35
35
  // Check if the loglevel matches this call
36
- const levels = [ 'error', 'warn', 'info' ]
37
- const should_log = dev || levels.includes( loglevel )
36
+ const levels = [ 'info' ]
37
+ const should_log = levels.includes( loglevel )
38
38
 
39
39
  // Log the messages if the loglevel matches
40
40
  if( should_log ) console.info( ...messages )
@@ -43,7 +43,7 @@ log.info = function( ...messages ) {
43
43
 
44
44
  /**
45
45
  * 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'
46
+ * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'warn', 'info'
47
47
  * 🎯 Goal: log warnings of things that should not happen, but do not break functionality
48
48
  * @example log.warn( `Transaction history was empty, this should never happen: `, history )
49
49
  * @param {...any} messages - The messages to be logged.
@@ -51,7 +51,7 @@ log.info = function( ...messages ) {
51
51
  log.warn = function( ...messages ) {
52
52
 
53
53
  // Check if the loglevel matches this call
54
- const levels = [ 'error', 'warn' ]
54
+ const levels = [ 'warn', 'info' ]
55
55
  const should_log = dev || levels.includes( loglevel )
56
56
 
57
57
  // Log the messages if the loglevel matches
@@ -61,7 +61,7 @@ log.warn = function( ...messages ) {
61
61
 
62
62
  /**
63
63
  * 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'
64
+ * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn', 'info'
65
65
  * @scope log errors that impact proper functioning of the application
66
66
  * @example log.error( `Error connecting to database: `, error )
67
67
  * @param {...any} messages - The messages to be logged.
@@ -69,7 +69,7 @@ log.warn = function( ...messages ) {
69
69
  log.error = function( ...messages ) {
70
70
 
71
71
  // Check if the loglevel matches this call
72
- const levels = [ 'error' ]
72
+ const levels = [ 'error', 'warn', 'info' ]
73
73
  const should_log = dev || levels.includes( loglevel )
74
74
  if( !should_log ) return
75
75
 
@@ -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.0",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",