JSFiddle - React, Tailwind, and code Playground
HTML
<div id="out1"></div>
<hr />
<div id="out2"></div>
JavaScript
function jfriend00compareObjects(s, t) {
var msgs = [];
if (typeof s !== typeof t) {
msgs.push('two objects not the same type');
return;
}
if (typeof s !== 'object') {
msgs.push('arguments are not typeof === "object"');
return;
}
for (var prop in s) {
if (s.hasOwnProperty(prop)) {
if (t.hasOwnProperty(prop)) {
if (!angular.equals(s[prop], t[prop])) {
msgs.push('property ' + prop + ' does not match');
}
} else {
msgs.push('second object does not have property ' + prop);
}
}
}
// now verify that t doesn't have any properties
// that are missing from s
for (prop in t) {
if (t.hasOwnProperty(prop)) {
if (!s.hasOwnProperty(prop)) {
msgs.push('first object does not have property ' + prop);
}
}
}
return msgs;
}
// The unicorn: https://stackoverflow.com/a/18234317/1028230
// prettier-ignore
String.prototype.formatUnicorn = String.prototype.formatUnicorn || function () {
var str = this.toString(); if (arguments.length) { var t = typeof arguments[0]; var key; var args = 'string' === t || 'number' === t ? Array.prototype.slice.call(arguments) : arguments[0]; for (key in args) { str = str.replace(new RegExp('\\{' + key + '\\}', 'gi'), args[key]); } }
return str;
};
function compareObjects(s, t, propsToIgnore1) {
return compareFirstToSecond(s, t, propsToIgnore1, '/').concat(
compareSecondToFirst(t, s, propsToIgnore1, '/')
);
function compareFirstToSecond(first, second, propsToIgnore, path) {
var msgs = [];
propsToIgnore = propsToIgnore || [];
// Check type
if (typeof first !== typeof second) {
msgs.push(
path +
' -- two objects not the same type $${0}$${1}$$'.formatUnicorn(
typeof first,
...