blob: 15a00d0b51696d87726bb57b4c367a80cfa13a59 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/*!
2 * make-iterator <https://github.com/jonschlinkert/make-iterator>
3 *
4 * Copyright (c) 2014-2018, Jon Schlinkert.
5 * Released under the MIT License.
6 */
7
8'use strict';
9
10var typeOf = require('kind-of');
11
12module.exports = function makeIterator(target, thisArg) {
13 switch (typeOf(target)) {
14 case 'undefined':
15 case 'null':
16 return noop;
17 case 'function':
18 // function is the first to improve perf (most common case)
19 // also avoid using `Function#call` if not needed, which boosts
20 // perf a lot in some cases
21 return (typeof thisArg !== 'undefined') ? function(val, i, arr) {
22 return target.call(thisArg, val, i, arr);
23 } : target;
24 case 'object':
25 return function(val) {
26 return deepMatches(val, target);
27 };
28 case 'regexp':
29 return function(str) {
30 return target.test(str);
31 };
32 case 'string':
33 case 'number':
34 default: {
35 return prop(target);
36 }
37 }
38};
39
40function containsMatch(array, value) {
41 var len = array.length;
42 var i = -1;
43
44 while (++i < len) {
45 if (deepMatches(array[i], value)) {
46 return true;
47 }
48 }
49 return false;
50}
51
52function matchArray(arr, value) {
53 var len = value.length;
54 var i = -1;
55
56 while (++i < len) {
57 if (!containsMatch(arr, value[i])) {
58 return false;
59 }
60 }
61 return true;
62}
63
64function matchObject(obj, value) {
65 for (var key in value) {
66 if (value.hasOwnProperty(key)) {
67 if (deepMatches(obj[key], value[key]) === false) {
68 return false;
69 }
70 }
71 }
72 return true;
73}
74
75/**
76 * Recursively compare objects
77 */
78
79function deepMatches(val, value) {
80 if (typeOf(val) === 'object') {
81 if (Array.isArray(val) && Array.isArray(value)) {
82 return matchArray(val, value);
83 } else {
84 return matchObject(val, value);
85 }
86 } else {
87 return val === value;
88 }
89}
90
91function prop(name) {
92 return function(obj) {
93 return obj[name];
94 };
95}
96
97function noop(val) {
98 return val;
99}