mentie 0.2.26 → 0.2.27
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/validations.js +36 -0
- package/package.json +1 -1
package/modules/validations.js
CHANGED
|
@@ -63,4 +63,40 @@ export const allow_props = ( obj={}, allowed_properties=[], error_on_fail=true )
|
|
|
63
63
|
// If all good, return true
|
|
64
64
|
return true
|
|
65
65
|
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Compares two objects and returns the differences between them.
|
|
71
|
+
* @param {Object} obj1 - The first object to compare.
|
|
72
|
+
* @param {Object} obj2 - The second object to compare.
|
|
73
|
+
* @param {boolean} [include_originals=false] - Determines whether to include the original objects in the return value.
|
|
74
|
+
* @returns {Object} - An object containing the changes between the two objects.
|
|
75
|
+
*/
|
|
76
|
+
export const shallow_compare_objects = ( obj1={}, obj2={}, include_originals=false ) => {
|
|
77
|
+
|
|
78
|
+
// Get all keys between the two objects
|
|
79
|
+
const keys = [ ...new Set( [ ...Object.keys( obj1 ), ...Object.keys( obj2 ) ] ) ]
|
|
80
|
+
|
|
81
|
+
// Generate a diff object that has changed keys that have a { from, to } structure where additions and removals are marked using undefined
|
|
82
|
+
const changes = keys.reduce( ( acc, key ) => {
|
|
83
|
+
|
|
84
|
+
// Check if the key is in both objects
|
|
85
|
+
if( JSON.stringify( obj1[ key ] ) === JSON.stringify( obj2[ key ] ) ) return acc
|
|
86
|
+
|
|
87
|
+
// Generate from/to object
|
|
88
|
+
const changes = { from: obj1[ key ], to: obj2[ key ] }
|
|
89
|
+
|
|
90
|
+
// Add the change to the accumulator
|
|
91
|
+
acc[ key ] = changes
|
|
92
|
+
|
|
93
|
+
// Return the accumulator
|
|
94
|
+
return acc
|
|
95
|
+
|
|
96
|
+
}, {} )
|
|
97
|
+
|
|
98
|
+
if( include_originals ) return { changes, obj1, obj2 }
|
|
99
|
+
|
|
100
|
+
return changes
|
|
101
|
+
|
|
66
102
|
}
|