- Unify test data for nonexistence of an object/string and collection. - Introduce more test through adding missing test data to existing tests. - Improve logic for checking absence of values to match tests. - Add missing tests for absent value validation. - Update documentation to include shared test functionality.
22 lines
849 B
TypeScript
22 lines
849 B
TypeScript
// Compares to Array<T> objects for equality, ignoring order
|
|
export function scrambledEqual<T>(array1: readonly T[], array2: readonly T[]) {
|
|
if (!array1) { throw new Error('missing first array'); }
|
|
if (!array2) { throw new Error('missing second array'); }
|
|
const sortedArray1 = sort(array1);
|
|
const sortedArray2 = sort(array2);
|
|
return sequenceEqual(sortedArray1, sortedArray2);
|
|
function sort(array: readonly T[]) {
|
|
return array.slice().sort();
|
|
}
|
|
}
|
|
|
|
// Compares to Array<T> objects for equality in same order
|
|
export function sequenceEqual<T>(array1: readonly T[], array2: readonly T[]) {
|
|
if (!array1) { throw new Error('missing first array'); }
|
|
if (!array2) { throw new Error('missing second array'); }
|
|
if (array1.length !== array2.length) {
|
|
return false;
|
|
}
|
|
return array1.every((val, index) => val === array2[index]);
|
|
}
|