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

Function findCommonItems

packages/diff-sequences/src/__tests__/index.test.ts:200–236  ·  view source on GitHub ↗
(
  a: Array<unknown> | string,
  b: Array<unknown> | string,
)

Source from the content-addressed store, hash-verified

198
199// Return array of items in a longest common subsequence of array-like objects.
200const findCommonItems = (
201 a: Array<unknown> | string,
202 b: Array<unknown> | string,
203): Array<unknown> => {
204 const aLength = a.length;
205 const bLength = b.length;
206 const isCommon = (aIndex: number, bIndex: number) => {
207 assertMin('input aIndex', aIndex, 0);
208 assertEnd('input aIndex', aIndex, aLength);
209 assertMin('input bIndex', bIndex, 0);
210 assertEnd('input bIndex', bIndex, bLength);
211 return a[aIndex] === b[bIndex];
212 };
213
214 const array: Array<unknown> = [];
215 diff(
216 aLength,
217 bLength,
218 isCommon,
219 (nCommon: number, aCommon: number, bCommon: number) => {
220 assertMin('output nCommon', nCommon, 1);
221 assertMin('output aCommon', aCommon, 0);
222 assertMax('output aCommon + nCommon', aCommon + nCommon, aLength);
223 assertMin('output bCommon', bCommon, 0);
224 assertMax('output bCommon + nCommon', bCommon + nCommon, bLength);
225 assertCommonItems(a, b, nCommon, aCommon, bCommon);
226 for (; nCommon !== 0; nCommon -= 1, aCommon += 1) {
227 array.push(a[aCommon]);
228 }
229 },
230 );
231
232 const nDifferences = countDifferences(aLength, bLength, isCommon);
233 expect(aLength + bLength - 2 * array.length).toBe(nDifferences);
234
235 return array;
236};
237
238// Assert that array-like objects have the expected common items.
239const expectCommonItems = (

Callers 1

expectCommonItemsFunction · 0.70

Calls 7

assertMinFunction · 0.85
assertMaxFunction · 0.85
assertCommonItemsFunction · 0.85
countDifferencesFunction · 0.85
expectFunction · 0.85
toBeMethod · 0.80
diffFunction · 0.50

Tested by

no test coverage detected