StructDiff
Detect only differences in the structure of objects. This is useful for validation.
Motivation
Is it the same shape as record m and record n in MongoDB ?
Features
It is determined whether the two objects have the same shape, ignoring the difference in primitive values. Such processing is effective as dynamic validation.
Installation
npm install structfiff
No modules depend on it.
Usage
How to use
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, [type]);
ESModule
import {StructDiff} from "structdiff";
const es_detector = new StructDiff();
let result:boolean = es_detector.isSame(object1, object2, [type]);
params | meaning |
---|---|
object1 | targets for comparison |
object2 | targets for comparison |
type | type of comparison |
type of comparison
value | meaning |
---|---|
0 |
default. Detects differences in structure and value "types". |
1 | Detects differences in structure and values. |
2 | Only structural differences are detected. |
- Prefer evaluation in handlers over type parameters.
With Handler
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.
Default
Detects differences in structure and value "types".
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
Strict Mode
Detects differences in structure and values.
console.log(detector.isSame({a: 1}, {a: 1}, 1))
> true
console.log(detector.isSame({a: 1}, {a: 2}, 1))
> false
Loose Mode
Only structural differences are detected.
console.log(detector.isSame({a: 1}, {a: "2"}, 2))
> true
console.log(detector.isSame({a: 1}, {b: "2"}, 2))
> false
Array order
The difference in the arrangement order is the difference in the structure.
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
Part of the object
Of course, some of the objects can also be compared.
console.log(detector.isSame(_origin.children.john, copy.children.john))
> true
console.log(detector.isSame(_origin.children.john, _origin.children.tom))
> false
Exceptionally
Exceptionally, [] and {} are the same, as are 0 and NaN.
console.log(detector.isSame([], {}))
> true
console.log(detector.isSame(0, NaN))
> true
Note
See demo.md for unclear cases.
Author
License
"structdiff" is under MIT license.