diffOps.js 1.5 KB

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