-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add split by separator + order by numeric + no sort
- Loading branch information
Showing
7 changed files
with
157 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
export type SortOrder = 'asc' | 'desc' | null; | ||
export type SortOrder = null | 'asc' | 'desc' | 'asc-num' | 'desc-num' | 'asc-bin' | 'desc-bin' | 'asc-upper' | 'desc-upper'; | ||
|
||
export interface ConvertOptions { | ||
lowerCase: boolean | ||
trimItems: boolean | ||
itemPrefix: string | ||
itemSuffix: string | ||
removeItemPrefix: string | ||
removeItemSuffix: string | ||
listPrefix: string | ||
listSuffix: string | ||
reverseList: boolean | ||
sortList: SortOrder | ||
removeDuplicates: boolean | ||
separator: string | ||
keepLineBreaks: boolean | ||
lowerCase?: boolean | ||
trimItems?: boolean | ||
itemPrefix?: string | ||
itemSuffix?: string | ||
removeItemPrefix?: string | ||
removeItemSuffix?: string | ||
listPrefix?: string | ||
listSuffix?: string | ||
reverseList?: boolean | ||
sortList?: SortOrder | ||
removeDuplicates?: boolean | ||
itemsSeparator?: string | ||
splitBySeparator?: string | ||
keepLineBreaks?: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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) => 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']); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
export { byOrder }; | ||
export type SortOrder = 'asc' | 'desc' | 'asc-num' | 'desc-num' | 'asc-bin' | 'desc-bin' | 'asc-upper' | 'desc-upper' | null | undefined; | ||
|
||
function byOrder({ order }: { order: 'asc' | 'desc' | null | undefined }) { | ||
export function byOrder({ order }: { order: SortOrder }) { | ||
return (a: string, b: string) => { | ||
if (order === 'asc-bin' || order === 'desc-bin') { | ||
const compare = a > b ? 1 : a < b ? -1 : 0; | ||
return order === 'asc-bin' ? compare : -compare; | ||
} | ||
if (order === 'asc-num' || order === 'desc-num') { | ||
const compare = a.localeCompare(b, undefined, { | ||
numeric: true, | ||
}); | ||
return order === 'asc-num' ? compare : -compare; | ||
} | ||
if (order === 'asc-upper' || order === 'desc-upper') { | ||
const compare = a.localeCompare(b, undefined, { | ||
caseFirst: 'upper', | ||
}); | ||
return order === 'asc-upper' ? compare : -compare; | ||
} | ||
return order === 'asc' ? a.localeCompare(b) : b.localeCompare(a); | ||
}; | ||
} |