object-diff-gen
v1.0.1
Published
A utility function to compare two objects based on missing properties, types and values
Downloads
6
Maintainers
Readme
JavaScriptObjectComparator v1.0.1
This utility method conpares two Javascript objects and provides the following information as a javascript object:
- Missing properties
- Properties having Mismatch types
- Properties having Mismatch values
Installation
npm i --save object-diff-gen
Example Usage
let incomingStructure = {
deviceName: "A",
device: {
id: 1,
value: "3", // type mistmatch
prop1: "new type", // value mismatch
// missing key
signals: {
rawType: "digital", // value mismatch
rawValue: 3 // value mismatch
}
}
};
let baseStructure = {
deviceName: "A",
device: {
id: 1,
value: 3,
prop1: "old type",
prop2: "some value",
signals: {
rawType: "digitals",
rawValue: 2
}
}
};
console.log(objectComparator([baseStructure, incomingStructure]));
Example Output
{
missingkeys: [ 'device.prop2' ],
mismatchValue: [
'device',
'device.value',
'device.prop1',
'device.prop2',
'device.signals',
'device.signals.rawType',
'device.signals.rawValue'
],
mismatchType: [ 'device.value', 'device.prop2' ]
}
Reference Object
Reference Object can be changed using the "ref" parameter to the function. By default 0th Object is considered for reference.
objectComparator([baseStructure, incomingStructure], ref = 0 or 1)