mentie 0.3.13 → 0.3.15

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/cache.js CHANGED
@@ -25,8 +25,8 @@ export function cache( key, value, expires_in_ms=Infinity ) {
25
25
  }
26
26
 
27
27
  // Warn if the key contains 'undefined'
28
- if( `${ key }`.includes( 'undefined' ) ) {
29
- log.warn( `The cache key ${ key } contains 'undefined', this may indicate a bug in your cache logic` )
28
+ if( `${ key }`.includes( 'undefined' ) || `${ key }`.includes( 'null' ) ) {
29
+ log.warn( `The cache key is ${ key }, this may indicate a bug in your cache logic` )
30
30
  }
31
31
 
32
32
  // If value is provided, save value and expiration
@@ -42,6 +42,49 @@ export function cache( key, value, expires_in_ms=Infinity ) {
42
42
  return _cache[key]?.value
43
43
  }
44
44
 
45
+ /**
46
+ * Restores the cache from a given cache object.
47
+ * If the cache object is empty, it will overwrite the current cache.
48
+ * If the cache object has keys that already exist in the current cache, those keys will be skipped.
49
+ * @param {Object} cache_object - The cache object to restore.
50
+ * @returns {Object} The updated cache object.
51
+ * @throws {Error} If the cache_object is not an object or is null.
52
+ */
53
+ cache.restore = ( cache_object ) => {
54
+
55
+ // Impute type of cache_object
56
+ let input_type = typeof cache_object
57
+ if( cache_object === null ) input_type = 'null'
58
+ if( Array.isArray( cache_object ) ) input_type = 'array'
59
+
60
+ // Check if the cache_object is an object, specifically check it is not an array
61
+ if( input_type != 'object' ) {
62
+ throw new Error( `cache.restore() expects an object, got ${ input_type }` )
63
+ }
64
+
65
+ // If current cache has no keys, restore the cache object
66
+ if( !Object.keys( _cache ).length ) {
67
+ // Assign the cache_object content to the cache obect
68
+ Object.assign( _cache, cache_object )
69
+ log.info( `Cache restored with ${ Object.keys( cache_object ).length } keys` )
70
+ return _cache
71
+ }
72
+
73
+ // If current cache has keys, only overwrite keys that are not already in cache, warn for keys that are skipped
74
+ const keys = Object.keys( cache_object )
75
+ for( const key of keys ) {
76
+ if( _cache[key] ) {
77
+ log.warn( `Cache key ${ key } already exists, skipping restore` )
78
+ } else {
79
+ _cache[key] = cache_object[ key ]
80
+ }
81
+ }
82
+
83
+ log.info( `Cache restored with ${ keys.length } keys, ${ Object.keys( _cache ).length } total keys in cache` )
84
+ return _cache
85
+
86
+ }
87
+
45
88
  /**
46
89
  *
47
90
  * @returns {Object} - A copy of the cache object.
@@ -84,11 +127,17 @@ cache.stats = () => {
84
127
  }
85
128
 
86
129
  /**
87
- * Function to inspect concurrency.
130
+ * Function to inspect concurrency calls of a function. Add this as a logger in a function to log out the concurrency value.
88
131
  *
89
132
  * @param {Function} logger - The logger function.
90
133
  * @param {string} key_prefix - The key for the concurrency value, used to tag the logging
91
134
  * @returns {number} - The concurrency value.
135
+ * @example
136
+ * import { cache, concurrency } from 'mentie';
137
+ * function often_called_function() {
138
+ * concurrency( console.log, 'important_function' );
139
+ * // ... function logic ...
140
+ * }
92
141
  */
93
142
  export function concurrency( logger, key_prefix ) {
94
143
 
@@ -69,7 +69,7 @@ env.web_loglevel = () => env.is_web() && new URLSearchParams( location?.search )
69
69
  * Retrieves the log level set via environment variables in a Node.js environment.
70
70
  * @returns {string|undefined} The log level from environment variables, or undefined if not set.
71
71
  */
72
- env.node_loglevel = () => env.is_node() && process.env?.LOGLEVEL && process.env?.LOG_LEVEL
72
+ env.node_loglevel = () => env.is_node() && ( process.env?.LOGLEVEL || process.env?.LOG_LEVEL )
73
73
 
74
74
  /**
75
75
  * Retrieves the effective log level based on the environment. Defaults to 'info' in development environments, 'error' otherwise.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",