mentie 0.1.16 → 0.1.19

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
@@ -1,3 +1,5 @@
1
+ import { log } from "./logging.js"
2
+
1
3
  /**
2
4
  * Cache object for storing data.
3
5
  * @type {Object}
@@ -15,6 +17,17 @@ const _cache = {}
15
17
  * @returns {*} The cached value.
16
18
  */
17
19
  export function cache( key, value ) {
20
+
21
+ // If the key is undefined, log a warning
22
+ if( key === undefined ) {
23
+ log.warn( `The cache key is undefined, this may indicate a bug in your cache logic` )
24
+ }
25
+
26
+ // Warn if the key contains 'undefined'
27
+ if( key.includes( 'undefined' ) ) {
28
+ log.warn( `The cache key ${ key } contains 'undefined', this may indicate a bug in your cache logic` )
29
+ }
30
+
18
31
  if( value ) _cache[key] = value
19
32
  return _cache[key]
20
33
  }
@@ -11,4 +11,56 @@ export const email_regex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/
11
11
  * @param {string} string - The string to be sanitized.
12
12
  * @returns {string} - The sanitized string.
13
13
  */
14
- export const sanetise_string = string => `${ string }`.trim().toLowerCase()
14
+ export const sanetise_string = string => `${ string }`.trim().toLowerCase()
15
+
16
+
17
+ /**
18
+ * Checks if an object contains all the required properties.
19
+ * @param {Object} obj - The object to check.
20
+ * @param {Array} required_properties - The array of required properties.
21
+ * @param {boolean} [error_on_fail=true] - Determines whether to throw an error if properties are missing.
22
+ * @returns {boolean} - Returns true if all required properties are present, otherwise returns false.
23
+ * @throws {Error} - Throws an error if properties are missing and `error_on_fail` is true.
24
+ */
25
+ export const require_props = ( obj={}, required_properties=[], error_on_fail=true ) => {
26
+
27
+ // Get the keys on the object
28
+ const keys = Object.keys( obj )
29
+
30
+ // Check that required props are present
31
+ const contains_all_required = required_properties.every( key => keys.includes( key ) )
32
+
33
+ // If properties are missing, throw errors or return false
34
+ if( error_on_fail && !contains_all_required ) throw new Error( `Missing required properties on object` )
35
+ if( !contains_all_required ) return false
36
+
37
+ // If all good, return true
38
+ return true
39
+
40
+ }
41
+
42
+ /**
43
+ * Validates the properties of an object against a list of allowed properties.
44
+ *
45
+ * @param {Object} obj - The object to validate.
46
+ * @param {Array} allowed_properties - The list of allowed properties.
47
+ * @param {boolean} [error_on_fail=true] - Determines whether to throw an error if unknown properties are found.
48
+ * @returns {boolean} - Returns true if all properties are allowed, false otherwise.
49
+ * @throws {Error} - Throws an error if unknown properties are found and `error_on_fail` is true.
50
+ */
51
+ export const allow_props = ( obj={}, allowed_properties=[], error_on_fail=true ) => {
52
+
53
+ // Get the keys on the object
54
+ const keys = Object.keys( obj )
55
+
56
+ // Check that required props are present
57
+ const unknownProperties = keys.filter( key => !allowed_properties.includes( key ) )
58
+
59
+ // If properties are missing, throw errors or return false
60
+ if( error_on_fail && unknownProperties.length ) throw new Error( `Unknown properties on object: ${ unknownProperties.join( ', ' ) }` )
61
+ if( unknownProperties.length ) return false
62
+
63
+ // If all good, return true
64
+ return true
65
+
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.1.16",
3
+ "version": "0.1.19",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",