matchNode.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. 'use strict';
  11. const hasOwn =
  12. Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
  13. /**
  14. * Checks whether needle is a strict subset of haystack.
  15. *
  16. * @param {*} haystack Value to test.
  17. * @param {*} needle Test function or value to look for in `haystack`.
  18. * @return {bool}
  19. */
  20. function matchNode(haystack, needle) {
  21. if (typeof needle === 'function') {
  22. return needle(haystack);
  23. }
  24. if (isNode(needle) && isNode(haystack)) {
  25. return Object.keys(needle).every(function(property) {
  26. return (
  27. hasOwn(haystack, property) &&
  28. matchNode(haystack[property], needle[property])
  29. );
  30. });
  31. }
  32. return haystack === needle;
  33. }
  34. function isNode(value) {
  35. return typeof value === 'object' && value;
  36. }
  37. module.exports = matchNode;