( object: any, subset: any, customTesters: Array<Tester> = [], seenReferences: WeakMap<object, boolean> = new WeakMap(), )
| 114 | // printing the diff for toMatchObject() without adding unrelated noise. |
| 115 | /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ |
| 116 | export 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 | |
| 169 | const IteratorSymbol = Symbol.iterator; |
| 170 |
no test coverage detected