source: node_modules/es-toolkit/dist/compat/predicate/isMatchWith.mjs@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 4.4 KB
Line 
1import { isObject } from './isObject.mjs';
2import { isPrimitive } from '../../predicate/isPrimitive.mjs';
3import { isEqualsSameValueZero } from '../../_internal/isEqualsSameValueZero.mjs';
4
5function isMatchWith(target, source, compare) {
6 if (typeof compare !== 'function') {
7 return isMatchWith(target, source, () => undefined);
8 }
9 return isMatchWithInternal(target, source, function doesMatch(objValue, srcValue, key, object, source, stack) {
10 const isEqual = compare(objValue, srcValue, key, object, source, stack);
11 if (isEqual !== undefined) {
12 return Boolean(isEqual);
13 }
14 return isMatchWithInternal(objValue, srcValue, doesMatch, stack);
15 }, new Map());
16}
17function isMatchWithInternal(target, source, compare, stack) {
18 if (source === target) {
19 return true;
20 }
21 switch (typeof source) {
22 case 'object': {
23 return isObjectMatch(target, source, compare, stack);
24 }
25 case 'function': {
26 const sourceKeys = Object.keys(source);
27 if (sourceKeys.length > 0) {
28 return isMatchWithInternal(target, { ...source }, compare, stack);
29 }
30 return isEqualsSameValueZero(target, source);
31 }
32 default: {
33 if (!isObject(target)) {
34 return isEqualsSameValueZero(target, source);
35 }
36 if (typeof source === 'string') {
37 return source === '';
38 }
39 return true;
40 }
41 }
42}
43function isObjectMatch(target, source, compare, stack) {
44 if (source == null) {
45 return true;
46 }
47 if (Array.isArray(source)) {
48 return isArrayMatch(target, source, compare, stack);
49 }
50 if (source instanceof Map) {
51 return isMapMatch(target, source, compare, stack);
52 }
53 if (source instanceof Set) {
54 return isSetMatch(target, source, compare, stack);
55 }
56 const keys = Object.keys(source);
57 if (target == null || isPrimitive(target)) {
58 return keys.length === 0;
59 }
60 if (keys.length === 0) {
61 return true;
62 }
63 if (stack?.has(source)) {
64 return stack.get(source) === target;
65 }
66 stack?.set(source, target);
67 try {
68 for (let i = 0; i < keys.length; i++) {
69 const key = keys[i];
70 if (!isPrimitive(target) && !(key in target)) {
71 return false;
72 }
73 if (source[key] === undefined && target[key] !== undefined) {
74 return false;
75 }
76 if (source[key] === null && target[key] !== null) {
77 return false;
78 }
79 const isEqual = compare(target[key], source[key], key, target, source, stack);
80 if (!isEqual) {
81 return false;
82 }
83 }
84 return true;
85 }
86 finally {
87 stack?.delete(source);
88 }
89}
90function isMapMatch(target, source, compare, stack) {
91 if (source.size === 0) {
92 return true;
93 }
94 if (!(target instanceof Map)) {
95 return false;
96 }
97 for (const [key, sourceValue] of source.entries()) {
98 const targetValue = target.get(key);
99 const isEqual = compare(targetValue, sourceValue, key, target, source, stack);
100 if (isEqual === false) {
101 return false;
102 }
103 }
104 return true;
105}
106function isArrayMatch(target, source, compare, stack) {
107 if (source.length === 0) {
108 return true;
109 }
110 if (!Array.isArray(target)) {
111 return false;
112 }
113 const countedIndex = new Set();
114 for (let i = 0; i < source.length; i++) {
115 const sourceItem = source[i];
116 let found = false;
117 for (let j = 0; j < target.length; j++) {
118 if (countedIndex.has(j)) {
119 continue;
120 }
121 const targetItem = target[j];
122 let matches = false;
123 const isEqual = compare(targetItem, sourceItem, i, target, source, stack);
124 if (isEqual) {
125 matches = true;
126 }
127 if (matches) {
128 countedIndex.add(j);
129 found = true;
130 break;
131 }
132 }
133 if (!found) {
134 return false;
135 }
136 }
137 return true;
138}
139function isSetMatch(target, source, compare, stack) {
140 if (source.size === 0) {
141 return true;
142 }
143 if (!(target instanceof Set)) {
144 return false;
145 }
146 return isArrayMatch([...target], [...source], compare, stack);
147}
148
149export { isMatchWith, isSetMatch };
Note: See TracBrowser for help on using the repository browser.