forked from myndzi/schema-compare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (31 loc) · 1.03 KB
/
Copy pathindex.js
File metadata and controls
33 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
function cmp(schema, oldVal, newVal) {
return (
typeof schema !== 'object' || schema === null || oldVal === null || newVal === null || oldVal === undefined || newVal === undefined?
oldVal === newVal
:
Array.isArray(schema)?
oldVal.length === newVal.length && (
typeof schema[0] !== 'object'?
oldVal.reduce(function (acc, cur, idx) {
return acc && cur === newVal[idx];
}, true)
:
oldVal.reduce(function (acc, cur, idx) {
return acc && cmp(schema[0], cur, newVal[idx]);
}, true)
)
:
Object.keys(schema).reduce(function (acc, key) {
return acc && (
typeof schema[key] !== 'object' || schema[key] === null?
oldVal.hasOwnProperty(key) &&
newVal.hasOwnProperty(key) &&
oldVal[key] === newVal[key]
:
cmp(schema[key], oldVal[key], newVal[key])
);
}, true)
);
}
module.exports = cmp;