feat: add split by separator + order by numeric + no sort

Fix #764 #1279 #1090
Small screen UI Fix
This commit is contained in:
ShareVB 2024-09-21 16:18:05 +02:00
parent 327ff11a59
commit 7bafd4790e
7 changed files with 168 additions and 72 deletions

25
src/utils/array.test.ts Normal file
View file

@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest';
import { type SortOrder, byOrder } from './array';
describe('array utils', () => {
describe('byOrder', () => {
it('should sort correctly', () => {
const sortBy = (array: string[], order: SortOrder) => {
return array.sort(byOrder({ order }));
};
const strings = ['a', 'A', 'b', 'B', 'á', '1', '2', '10', '一', '阿'];
expect(sortBy(strings, null)).to.eql(strings);
expect(sortBy(strings, undefined)).to.eql(strings);
expect(sortBy(strings, 'asc')).to.eql(['1', '10', '2', 'a', 'A', 'á', 'b', 'B', '一', '阿']);
expect(sortBy(strings, 'asc-num')).to.eql(['1', '2', '10', 'a', 'A', 'á', 'b', 'B', '一', '阿']);
expect(sortBy(strings, 'asc-bin')).to.eql(['1', '10', '2', 'A', 'B', 'a', 'b', 'á', '一', '阿']);
expect(sortBy(strings, 'asc-upper')).to.eql(['1', '10', '2', 'A', 'a', 'á', 'B', 'b', '一', '阿']);
expect(sortBy(strings, 'desc')).to.eql(['阿', '一', 'B', 'b', 'á', 'A', 'a', '2', '10', '1']);
expect(sortBy(strings, 'desc-num')).to.eql(['阿', '一', 'B', 'b', 'á', 'A', 'a', '10', '2', '1']);
expect(sortBy(strings, 'desc-bin')).to.eql(['阿', '一', 'á', 'b', 'a', 'B', 'A', '2', '10', '1']);
expect(sortBy(strings, 'desc-upper')).to.eql(['阿', '一', 'b', 'B', 'á', 'a', 'A', '2', '10', '1']);
});
});
});

View file

@ -1,6 +1,29 @@
export { byOrder };
export type SortOrder = 'asc' | 'desc' | 'asc-num' | 'desc-num' | 'asc-bin' | 'desc-bin' | 'asc-upper' | 'desc-upper' | null | undefined;
export function byOrder({ order }: { order: SortOrder }) {
if (order === 'asc-bin' || order === 'desc-bin') {
return (a: string, b: string) => {
const compare = a > b ? 1 : (a < b ? -1 : 0); // NOSONAR
return order === 'asc-bin' ? compare : -compare;
};
}
if (order === 'asc-num' || order === 'desc-num') {
return (a: string, b: string) => {
const compare = a.localeCompare(b, undefined, {
numeric: true,
});
return order === 'asc-num' ? compare : -compare;
};
}
if (order === 'asc-upper' || order === 'desc-upper') {
return (a: string, b: string) => {
const compare = a.localeCompare(b, undefined, {
caseFirst: 'upper',
});
return order === 'asc-upper' ? compare : -compare;
};
}
function byOrder({ order }: { order: 'asc' | 'desc' | null | undefined }) {
return (a: string, b: string) => {
return order === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
};