( a: Array<unknown> | string, b: Array<unknown> | string, )
| 198 | |
| 199 | // Return array of items in a longest common subsequence of array-like objects. |
| 200 | const 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. |
| 239 | const expectCommonItems = ( |
no test coverage detected