matchNode.js 971 B

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