json-deep-compare
Version:
A powerful library for comparing JSON objects with support for deep comparison, regex validation, and customizable options
1 lines • 2.19 kB
JavaScript
const Options=require("./Options"),Result=require("./Result"),RegexValidator=require("./RegexValidator"),Comparator=require("./Comparator"),FastComparator=require("./FastComparator"),UltraFastComparator=require("./UltraFastComparator"),BooleanComparator=require("./BooleanComparator");class JSONCompare{constructor(t={}){this.options=new Options(t),this.result=new Result(this.options),this.regexValidator=new RegexValidator(this.options,this.result),this.comparator=new Comparator(this.options,this.result,this.regexValidator),this.useFastMode=FastComparator.shouldUseFastMode(this.options),this.useUltraFastMode=this.shouldUseUltraFastMode()}shouldUseUltraFastMode(){return!(this.options.regexChecks&&0!==Object.keys(this.options.regexChecks).length||this.options.equivalentValues&&0!==Object.keys(this.options.equivalentValues).length||!0!==this.options.strictTypes||this.options.ignoredKeys&&0!==this.options.ignoredKeys.length||!0!==this.options.ignoreExtraKeys||this.options.matchKeysByName)}compare(t,e){if(this.useUltraFastMode)return UltraFastComparator.ultraFastCompareWithResult(t,e);if(this.useFastMode){const s=FastComparator.fastCompare(t,e);return{matched:{keys:[],values:[]},unmatched:{keys:[],values:[],types:[]},regexChecks:{passed:[],failed:[]},summary:{matchPercentage:s.matchPercentage,totalKeysCompared:s.totalKeys,totalMatched:s.matched,totalUnmatched:s.unmatched,totalRegexChecks:0}}}return this.result.reset(),this.comparator.compareObjects(t,e,""),this.result.updateSummary(),this.result.getResult()}isEqual(t,e){return BooleanComparator.booleanCompare(t,e)}compareAndValidate(t,e){if(this.useFastMode&&(!this.options.regexChecks||0===Object.keys(this.options.regexChecks).length)){const s=FastComparator.fastCompare(t,e);return{matched:{keys:[],values:[]},unmatched:{keys:[],values:[],types:[]},regexChecks:{passed:[],failed:[]},summary:{matchPercentage:s.matchPercentage,totalKeysCompared:s.totalKeys,totalMatched:s.matched,totalUnmatched:s.unmatched,totalRegexChecks:0}}}return this.result.reset(),this.comparator.compareObjects(t,e,""),this.regexValidator.validateAllMatchingKeys(e),this.result.updateSummary(),this.result.getResult()}}module.exports=JSONCompare;