MCPcopy
hub / github.com/jestjs/jest / getObjectSubset

Function getObjectSubset

packages/expect-utils/src/utils.ts:116–167  ·  view source on GitHub ↗
(
  object: any,
  subset: any,
  customTesters: Array<Tester> = [],
  seenReferences: WeakMap<object, boolean> = new WeakMap(),
)

Source from the content-addressed store, hash-verified

114// printing the diff for toMatchObject() without adding unrelated noise.
115/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
116export const getObjectSubset = (
117 object: any,
118 subset: any,
119 customTesters: Array<Tester> = [],
120 seenReferences: WeakMap<object, boolean> = new WeakMap(),
121): any => {
122 /* eslint-enable @typescript-eslint/explicit-module-boundary-types */
123 if (Array.isArray(object)) {
124 if (Array.isArray(subset) && subset.length === object.length) {
125 // The map method returns correct subclass of subset.
126 return subset.map((sub: any, i: number) =>
127 getObjectSubset(object[i], sub, customTesters),
128 );
129 }
130 } else if (object instanceof Date) {
131 return object;
132 } else if (isObject(object) && isObject(subset)) {
133 if (
134 equals(object, subset, [
135 ...customTesters,
136 iterableEquality,
137 subsetEquality,
138 ])
139 ) {
140 // Avoid unnecessary copy which might return Object instead of subclass.
141 return subset;
142 }
143
144 const trimmed: any = {};
145 seenReferences.set(object, trimmed);
146
147 for (const key of getObjectKeys(object)) {
148 if (!hasPropertyInObject(subset, key)) {
149 continue;
150 }
151
152 trimmed[key] = seenReferences.has(object[key])
153 ? seenReferences.get(object[key])
154 : getObjectSubset(
155 object[key],
156 subset[key],
157 customTesters,
158 seenReferences,
159 );
160 }
161
162 if (getObjectKeys(trimmed).length > 0) {
163 return trimmed;
164 }
165 }
166 return object;
167};
168
169const IteratorSymbol = Symbol.iterator;
170

Callers 3

toMatchObjectFunction · 0.90
utils.test.tsFile · 0.90

Calls 6

equalsFunction · 0.90
getObjectKeysFunction · 0.85
hasPropertyInObjectFunction · 0.85
isObjectFunction · 0.70
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected