mentie 0.5.1 → 0.5.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.
Files changed (2) hide show
  1. package/modules/cache.js +104 -0
  2. package/package.json +2 -2
package/modules/cache.js CHANGED
@@ -47,6 +47,110 @@ export function cache( key, value, expires_in_ms=Infinity ) {
47
47
  return _cache[key]?.value
48
48
  }
49
49
 
50
+ /**
51
+ * Merges a value into the cached value at the specified key.
52
+ * If the cached value is an object, it merges the properties.
53
+ * If the cached value is an array, it appends the new values.
54
+ * If the cached value is a number, it increments by the new value.
55
+ * If the cached value is a string, it appends the new string.
56
+ * For other types, it overwrites the cached value.
57
+ *
58
+ * @param {string} key - The key of the cached value to merge into.
59
+ * @param {*} value - The value to merge into the cached value.
60
+ * @param {number} [expires_in_ms=Infinity] - The expiration time in milliseconds for the merged value (optional).
61
+ * @returns {*} The merged cached value.
62
+ */
63
+ cache.merge = ( key, value, expires_in_ms=Infinity ) => {
64
+
65
+ // Get type of value
66
+ const type = Array.isArray( value ) ? 'array' : typeof value
67
+
68
+ // If array, append
69
+ if( type === 'array' ) {
70
+
71
+ // Get current array
72
+ let existing_value = cache( key ) || []
73
+
74
+ // If existing value is not an array, log a warning and overwrite
75
+ if( !Array.isArray( existing_value ) ) {
76
+ log.warn( `Cache key ${ key } is not an array, overwriting with new array value` )
77
+ existing_value = []
78
+ }
79
+
80
+ // Append new values
81
+ const new_value = [ ...existing_value, ...value ]
82
+
83
+ // Save merged array to cache
84
+ return cache( key, new_value, expires_in_ms )
85
+
86
+ }
87
+
88
+ // If object, merge
89
+ if( type === 'object' ) {
90
+
91
+ // Get current object
92
+ let existing_value = cache( key ) || {}
93
+
94
+ // If existing value is not an object, log a warning and overwrite
95
+ if( typeof existing_value !== 'object' || Array.isArray( existing_value ) ) {
96
+ log.warn( `Cache key ${ key } is not an object, overwriting with new object value` )
97
+ existing_value = {}
98
+ }
99
+
100
+ // Merge objects
101
+ const new_value = { ...existing_value, ...value }
102
+
103
+ // Save merged object to cache
104
+ return cache( key, new_value, expires_in_ms )
105
+
106
+ }
107
+
108
+ // If number, increment
109
+ if( type === 'number' ) {
110
+
111
+ // Get current number
112
+ let existing_value = cache( key ) || 0
113
+
114
+ // If existing value is not a number, log a warning and overwrite
115
+ if( typeof existing_value !== 'number' ) {
116
+ log.warn( `Cache key ${ key } is not a number, overwriting with new number value` )
117
+ existing_value = 0
118
+ }
119
+
120
+ // Increment number
121
+ const new_value = existing_value + value
122
+
123
+ // Save incremented number to cache
124
+ return cache( key, new_value, expires_in_ms )
125
+
126
+ }
127
+
128
+ // If string, append
129
+ if( type === 'string' ) {
130
+
131
+ // Get current string
132
+ let existing_value = cache( key ) || ''
133
+
134
+ // If existing value is not a string, log a warning and overwrite
135
+ if( typeof existing_value !== 'string' ) {
136
+ log.warn( `Cache key ${ key } is not a string, overwriting with new string value` )
137
+ existing_value = ''
138
+ }
139
+
140
+ // Append string
141
+ const new_value = existing_value + value
142
+
143
+ // Save appended string to cache
144
+ return cache( key, new_value, expires_in_ms )
145
+
146
+ }
147
+
148
+ // For other types, just overwrite
149
+ log.warn( `Cache.merge() does not support type ${ type }, overwriting value for key ${ key }` )
150
+ return cache( key, value, expires_in_ms )
151
+
152
+ }
153
+
50
154
  /**
51
155
  * Restores the cache from a given cache object.
52
156
  * If the cache object is empty, it will overwrite the current cache.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -35,4 +35,4 @@
35
35
  "dependencies": {
36
36
  "safe-stable-stringify": "^2.4.3"
37
37
  }
38
- }
38
+ }