mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-04 00:28:33 +00:00
feat(editor): enhance string comparison handling in eval.ts (#14233)
Refactors the compareString function to safely handle null and undefined
inputs and improves overall string comparison logic. This prevents
incorrect sort behavior and ensures consistent ordering when comparing
mixed or missing values, particularly in table view sorting scenarios.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved string comparison used for sorting: empty values are
consistently placed last, numeric parts sort numerically before
non-numeric parts, and mixed-type and case variations are handled more
predictably for stable, consistent ordering across data views.
<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -48,32 +48,41 @@ const compareList = <T>(
|
||||
return 0;
|
||||
};
|
||||
const compareString = (a: unknown, b: unknown): CompareType => {
|
||||
if (typeof a != 'string' || a === '') {
|
||||
return Compare.GT;
|
||||
const strA = String(a ?? '');
|
||||
const strB = String(b ?? '');
|
||||
|
||||
if (strA === '' && strB !== '') {
|
||||
return Compare.GT; // Empty strings come last
|
||||
}
|
||||
if (typeof b != 'string' || b === '') {
|
||||
return Compare.LT;
|
||||
if (strA !== '' && strB === '') {
|
||||
return Compare.LT; // Empty strings come last
|
||||
}
|
||||
const listA = a.split('.');
|
||||
const listB = b.split('.');
|
||||
if (strA === '' && strB === '') {
|
||||
return 0; // Both empty, equal
|
||||
}
|
||||
|
||||
const listA = strA.split('.');
|
||||
const listB = strB.split('.');
|
||||
return compareList(listA, listB, (a, b) => {
|
||||
const lowA = a.toLowerCase();
|
||||
const lowB = b.toLowerCase();
|
||||
const lowA = String(a).toLowerCase(); // Ensure 'a' and 'b' from split are strings too
|
||||
const lowB = String(b).toLowerCase();
|
||||
|
||||
const numberA = Number.parseInt(lowA);
|
||||
const numberB = Number.parseInt(lowB);
|
||||
const aIsNaN = Number.isNaN(numberA);
|
||||
const bIsNaN = Number.isNaN(numberB);
|
||||
|
||||
if (aIsNaN && !bIsNaN) {
|
||||
return 1;
|
||||
return 1; // Non-numeric part comes after numeric part
|
||||
}
|
||||
if (!aIsNaN && bIsNaN) {
|
||||
return -1;
|
||||
return -1; // Numeric part comes before non-numeric part
|
||||
}
|
||||
if (!aIsNaN && !bIsNaN && numberA !== numberB) {
|
||||
return numberA - numberB;
|
||||
return numberA - numberB; // Numeric comparison for numeric parts
|
||||
}
|
||||
|
||||
return lowA.localeCompare(lowB);
|
||||
return lowA.localeCompare(lowB); // Lexicographical comparison for string parts
|
||||
});
|
||||
};
|
||||
const compareNumber = (a: unknown, b: unknown) => {
|
||||
|
||||
Reference in New Issue
Block a user