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