UNPKG

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