mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
fix(collection)!: default sort comparison algorithm (#10412)
BREAKING CHANGE: This replaces the previously inaccurate default sort algorithm, which may alter sort results where a user-defined comparison function is not provided.
This commit is contained in:
@@ -829,6 +829,33 @@ describe('sort() tests', () => {
|
||||
const coll = createCollectionFrom(['a', 5], ['b', 3], ['c', 1]);
|
||||
expect(coll.sort()).toStrictEqual(createCollectionFrom(['c', 1], ['b', 3], ['a', 5]));
|
||||
});
|
||||
|
||||
describe('returns correct sort order for test values', () => {
|
||||
const defaultSort = Collection['defaultSort']; // eslint-disable-line @typescript-eslint/dot-notation
|
||||
const testDefaultSortOrder = (firstValue: any, secondValue: any, result: number) => {
|
||||
expect(defaultSort(firstValue, secondValue)).toStrictEqual(result);
|
||||
expect(defaultSort(secondValue, firstValue)).toStrictEqual(result ? result * -1 : 0);
|
||||
};
|
||||
|
||||
test('correctly evaluates sort order of undefined', () => {
|
||||
testDefaultSortOrder(undefined, undefined, 0);
|
||||
testDefaultSortOrder(0, undefined, -1);
|
||||
});
|
||||
|
||||
test('correctly evaluates numeric values stringwise', () => {
|
||||
testDefaultSortOrder(-1, -2, -1); // "-1" before "-2"
|
||||
testDefaultSortOrder(1, '1', 0); // "1" equal to "1"
|
||||
testDefaultSortOrder(1, '1.0', -1); // "1" before "1.0"
|
||||
testDefaultSortOrder(1.1, '1.1', 0); // "1.1" equal to "1.1"
|
||||
testDefaultSortOrder('01', 1, -1); // "01" before "1"
|
||||
testDefaultSortOrder(1, 1n, 0); // "1" equal to "1"
|
||||
testDefaultSortOrder(Number.NaN, 'NaN', 0); // "NaN" equal to "NaN"
|
||||
});
|
||||
|
||||
test('evaluates object literals as equal', () => {
|
||||
testDefaultSortOrder({ a: 1 }, { b: 2 }, 0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user