mentie 0.1.8 → 0.1.10

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.
@@ -7,6 +7,13 @@
7
7
  * @returns {boolean} Returns true if the code is running in a web environment, otherwise returns false.
8
8
  */
9
9
  export const is_web = typeof window !== 'undefined'
10
+
11
+ /**
12
+ * Checks if the code is running within a Cypress environment.
13
+ * @returns {boolean} Returns true if the code is running in a Cypress environment, otherwise returns false.
14
+ */
15
+ export const is_cypress = typeof Cypress !== 'undefined'
16
+
10
17
  /**
11
18
  * Checks if the code is running in a Node environment.
12
19
  * @returns {boolean} Returns true if the code is running in a Node environment, otherwise returns false.
@@ -1,5 +1,5 @@
1
1
  // Import environment data
2
- import { dev, is_emulator, loglevel } from "./environment.js"
2
+ import { dev, is_cypress, is_emulator, loglevel } from "./environment.js"
3
3
 
4
4
  const should_log = levels => {
5
5
 
@@ -13,23 +13,36 @@ const should_log = levels => {
13
13
 
14
14
  }
15
15
 
16
+ /**
17
+ * Adds a stack trace IN PLACE to the provided messages array if executed in a browser context
18
+ * and if the URL query string contains `trace=true`.
19
+ *
20
+ * @param {Array} messages - The array of messages to potentially add a trace to.
21
+ * @returns {Array} - The modified array of messages, including a stack trace if conditions were met.
22
+ */
16
23
  const add_trace = messages => {
17
24
 
18
- // Do nothing if we are not in a browser
19
- if( typeof window === 'undefined' ) return null
20
-
21
- // Try to add stack to messages
25
+ // Try to add stack to messages if needed
22
26
  try {
23
27
 
28
+ // Do nothing if we are not in a browser
29
+ if( typeof window === 'undefined' ) return messages
30
+
31
+ // If there is no trace=true in the url, do nothing
32
+ if( !window.location?.search?.includes?.( 'trace=true' ) ) return messages
33
+
34
+
24
35
  // Get the stack trace
25
36
  let { stack } = new Error()
26
37
 
27
- // Remove the first line of the stack trace as it is always the error message
38
+ // Remove the first line of the stack trace
28
39
  stack = stack.split( '\n' ).slice( 1 ).join( '\n' )
29
40
 
30
- // Add the trace to the messages
41
+ // Annotate the provided messages
31
42
  messages.push( { stack } )
32
43
 
44
+ return messages
45
+
33
46
  } catch ( error ) {
34
47
 
35
48
  // This should never happen but we'll add it so we don't crash in unexpected situations
@@ -39,6 +52,27 @@ const add_trace = messages => {
39
52
 
40
53
  }
41
54
 
55
+ /**
56
+ * Modifies the input messages by potentially stringifying them if in a Cypress environment,
57
+ * and by appending a stack trace if the conditions specified in `add_trace` are met.
58
+ *
59
+ * @param {Array} messages - The array of messages to be annotated.
60
+ * @returns {void} - This function modifies the input array directly and does not return a value.
61
+ */
62
+ const annotate_messages = messages => {
63
+
64
+
65
+ // If we are running in cypress, stringify the messages because they become unavailable in the console
66
+ if( is_cypress ) messages = messages.map( message => JSON.stringify( message, null, 2 ) )
67
+
68
+ // Annotate the provided messages
69
+ messages = add_trace( messages )
70
+
71
+ // Return the annotated messages
72
+ return messages
73
+
74
+ }
75
+
42
76
  /**
43
77
  * Logs the provided messages to the console.
44
78
  * Only logs in development mode OR if ?loglevel= or LOG_LEVEL= is set to one of the following: 'error', 'warn', 'info'.
@@ -58,8 +92,8 @@ export function log( ...messages ) {
58
92
  // Log the messages if the loglevel matches
59
93
  if( dev || should_log( levels ) ) {
60
94
 
61
- // Add the trace to the messages
62
- add_trace( messages )
95
+ // Annotate the provided messages
96
+ annotate_messages( messages )
63
97
 
64
98
  // Log the messages
65
99
  console.log( ...messages )
@@ -83,8 +117,8 @@ log.info = function( ...messages ) {
83
117
  // Log the messages if the loglevel matches
84
118
  if( is_emulator || should_log( levels ) ) {
85
119
 
86
- // Add the trace to the messages
87
- add_trace( messages )
120
+ // Annotate the provided messages
121
+ annotate_messages( messages )
88
122
 
89
123
  // Log the messages
90
124
  console.info( ...messages )
@@ -108,8 +142,8 @@ log.warn = function( ...messages ) {
108
142
  // Log the messages if the loglevel matches
109
143
  if( dev || should_log( levels ) ) {
110
144
 
111
- // Add the trace to the messages
112
- add_trace( messages )
145
+ // Annotate the provided messages
146
+ annotate_messages( messages )
113
147
 
114
148
  // Log the messages
115
149
  console.warn( ...messages )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",