blob: 06641b187daafb95da44e3c83a684095ede84974 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var keys = require('object-keys');
4var forEach = require('foreach');
5var indexOf = require('array.prototype.indexof');
6var has = require('has');
7
8module.exports = function diffOperations(actual, expected, expectedMissing) {
9 var actualKeys = keys(actual);
10 var expectedKeys = keys(expected);
11
12 var extra = [];
13 var missing = [];
14 var extraMissing = [];
15
16 forEach(actualKeys, function (op) {
17 if (!(op in expected)) {
18 if (actual[op] && typeof actual[op] === 'object') {
19 forEach(keys(actual[op]), function (nestedOp) {
20 var fullNestedOp = op + '::' + nestedOp;
21 if (!(fullNestedOp in expected)) {
22 extra.push(fullNestedOp);
23 } else if (indexOf(expectedMissing, fullNestedOp) !== -1) {
24 extra.push(fullNestedOp);
25 }
26 });
27 } else {
28 extra.push(op);
29 }
30 } else if (indexOf(expectedMissing, op) !== -1) {
31 extra.push(op);
32 }
33 });
34 var checkMissing = function checkMissing(op, actualValue) {
35 if (typeof actualValue !== 'function' && indexOf(expectedMissing, op) === -1) {
36 missing.push(op);
37 }
38 };
39 forEach(expectedKeys, function (op) {
40 if (op.indexOf('::') > -1) {
41 var parts = op.split('::');
42 var value = actual[parts[0]];
43 if (value && typeof value === 'object' && typeof value[parts[1]] === 'function') {
44 checkMissing(op, value[parts[1]]);
45 }
46 } else {
47 checkMissing(op, actual[op]);
48 }
49 });
50
51 forEach(expectedMissing, function (expectedOp) {
52 if (!has(expected, expectedOp)) {
53 extraMissing.push(expectedOp);
54 }
55 });
56
57 return { missing: missing, extra: extra, extraMissing: extraMissing };
58};