json-deep-compare
Version:
A powerful library for comparing JSON objects with support for deep comparison, regex validation, and customizable options
1 lines • 3.38 kB
JavaScript
class UltraFastComparator{static ultraFastCompare(t,a){return t===a||typeof t==typeof a&&(null==t||null==a||"object"!=typeof t?t===a:Array.isArray(t)&&Array.isArray(a)?this.ultraFastCompareArrays(t,a):this.ultraFastCompareObjects(t,a))}static ultraFastCompareArrays(t,a){const e=t.length;if(e!==a.length)return!1;if(0===e)return!0;for(let r=0;r<e;r++)if(!this.ultraFastCompare(t[r],a[r]))return!1;return!0}static ultraFastCompareObjects(t,a){if(Object.getPrototypeOf(t)!==Object.prototype||Object.getPrototypeOf(a)!==Object.prototype)return!1;const e=Object.keys(t),r=e.length;if(r!==Object.keys(a).length)return!1;if(0===r)return!0;for(let o=0;o<r;o++){const r=e[o];if(!Object.prototype.hasOwnProperty.call(a,r)||!this.ultraFastCompare(t[r],a[r]))return!1}return!0}static ultraFastCompareArraysWithCounts(t,a){const e=t.length;if(e!==a.length)return{matchPercentage:0,totalKeysCompared:Math.max(e,a.length),totalMatched:0,totalUnmatched:Math.max(e,a.length)};if(0===e)return{matchPercentage:100,totalKeysCompared:0,totalMatched:0,totalUnmatched:0};let r=0,o=0,c=!1;for(let s=0;s<e;s++){const l=this.ultraFastCompareWithCounts(t[s],a[s]);r+=l.totalMatched,o+=l.totalUnmatched,o>e/2&&(c=!0)}const s=r+o;return{matchPercentage:s>0?Math.round(r/s*100):100,totalKeysCompared:s,totalMatched:r,totalUnmatched:o,partial:c}}static ultraFastCompareObjectsWithCounts(t,a){if(Object.getPrototypeOf(t)!==Object.prototype||Object.getPrototypeOf(a)!==Object.prototype)return{matchPercentage:0,totalKeysCompared:1,totalMatched:0,totalUnmatched:1};const e=Object.keys(t),r=Object.keys(a),o=e.length,c=r.length;if(o!==c)return{matchPercentage:0,totalKeysCompared:Math.max(o,c),totalMatched:0,totalUnmatched:Math.max(o,c)};if(0===o)return{matchPercentage:100,totalKeysCompared:0,totalMatched:0,totalUnmatched:0};let s=0,l=0;for(let r=0;r<o;r++){const c=e[r];if(!Object.prototype.hasOwnProperty.call(a,c)){l++;continue}const n=this.ultraFastCompareWithCounts(t[c],a[c]);if(s+=n.totalMatched,l+=n.totalUnmatched,l>o/2){const t=l+(o-(r+1)),a=o;return{matchPercentage:a>0?Math.round(s/a*100):0,totalKeysCompared:a,totalMatched:s,totalUnmatched:t}}}const n=s+l;return{matchPercentage:n>0?Math.round(s/n*100):100,totalKeysCompared:n,totalMatched:s,totalUnmatched:l}}static ultraFastCompareWithResult(t,a){const e=this.ultraFastCompareWithCounts(t,a);return{matched:{keys:[],values:[]},unmatched:{keys:[],values:[],types:[]},regexChecks:{passed:[],failed:[]},summary:{matchPercentage:e.matchPercentage,totalKeysCompared:e.totalKeysCompared,totalMatched:e.totalMatched,totalUnmatched:e.totalUnmatched,totalRegexChecks:0}}}static ultraFastCompareWithCounts(t,a){if(t===a)return"object"==typeof t&&null!==t?Array.isArray(t)?this.ultraFastCompareArraysWithCounts(t,t):this.ultraFastCompareObjectsWithCounts(t,t):{matchPercentage:100,totalKeysCompared:1,totalMatched:1,totalUnmatched:0};if(typeof t!=typeof a)return{matchPercentage:0,totalKeysCompared:1,totalMatched:0,totalUnmatched:1};if(null==t||null==a){const e=t===a;return{matchPercentage:e?100:0,totalKeysCompared:1,totalMatched:e?1:0,totalUnmatched:e?0:1}}if("object"!=typeof t){const e=t===a;return{matchPercentage:e?100:0,totalKeysCompared:1,totalMatched:e?1:0,totalUnmatched:e?0:1}}return Array.isArray(t)&&Array.isArray(a)?this.ultraFastCompareArraysWithCounts(t,a):this.ultraFastCompareObjectsWithCounts(t,a)}}module.exports=UltraFastComparator;