Detect only differences in the structure of objects. This is useful for validation.
Is it the same shape as record m and record n in MongoDB ?
It is determined whether the two objects have the same shape, ignoring the difference in primitive values. Such processing is effective as dynamic validation.
npm install structfiff
example
const object1 = {
children: {
john: {
schooling: true, hobby: [{name: "Cycling"}, {name: "Dance", type: "HipHop"}],
pet: [{type: "dog", name: "Max"}], age: 12
},
tom: {
schooling: false, hobby: [{name: "Squash"}], pet: [{type: "cat", name: "Chloe"}], age: 5
}
}
};
const object2 = {
children: {
tom: {
pet: [{type: "cat", name: "Chloe"}], age: 5, schooling: false, hobby: [{name: "Squash"}]
},
john: {
hobby: [{name: "Cycling"}, {name: "Dance", type: "HipHop"}],
pet: [{name: "Max",type: "dog"}], age: 12, schooling: true
}
}
};
CommonJS
const structdiff: any = require("structdiff");
const cjs_detector = new structdiff.StructDiff();
let result:boolean = cjs_detector.isSame(object1, object2, [comp_type]);
ESModule
import {StructDiff} from "structdiff";
const es_detector = new StructDiff();
let result:boolean = es_detector.isSame(object1, object2, [comp_type]);
comp_type:
0: default. Detects differences in structure and value "types".
1: Detects differences in structure and values.
2: Only structural differences are detected.
CommonJS
const structdiff: any = require("structdiff");
class CJSHandler extends structdiff.DetectHandler {
constructor() {
super()
}
public compare(s: any, d: any): boolean {
return ((typeof s) === (typeof d));
}
}
const cjs_detector = new structdiff.StructDiff(new CJSHandler());
let result:boolean = cjs_detector.isSame(object1, object2);
ESModule
import {DetectHandler, StructDiff} from "structdiff";
class ESHandler extends DetectHandler {
constructor() {
super()
}
public compare(s: any, d: any): boolean {
return ((typeof s) === (typeof d));
}
}
const es_detector = new StructDiff(new ESHandler());
let result:boolean = es_detector.isSame(object1, object2);
comp_type: ignored.
console.log(detector.isSame({a: 1, b: 1}, {a: 1}))
> false
console.log(detector.isSame({a: 1}, {a: 1, b: 1}))
> false
console.log(detector.isSame({a: 1}, {a: 2}, 0))
> true
console.log(detector.isSame({a: 1}, {b: 2}, 0))
> false
console.log(detector.isSame({a: 1}, {a: 1}, 1))
> true
console.log(detector.isSame({a: 1}, {a: 2}, 1))
> false
console.log(detector.isSame({a: 1}, {a: "2"}, 2))
> true
console.log(detector.isSame({a: 1}, {b: "2"}, 2))
> false
console.log(detector.isSame([{a: 1}, {b: 1}], [{a: 1}, {b: 1}]))
> true
console.log(detector.isSame([{a: 1}, {b: 1}], [{b: 1}, {a: 1}]))
> false
console.log(detector.isSame(_origin.children.john, copy.children.john))
> true
console.log(detector.isSame(_origin.children.john, _origin.children.tom))
> false
console.log(detector.isSame([], {}))
> true
console.log(detector.isSame(0, NaN))
> true
See demo.md for unclear cases.
"structdiff" is under MIT license.
Generated using TypeDoc