Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ const values = ['1_foo_bar', '12_foo_bar'].sort(naturalSort)
// Default sorting: ['1_foo_bar', '12_foo_bar']
```

The default comparator uses the English locale. Use `createNaturalSort` to sort with a different locale.

```ts
import { createNaturalSort } from '@poppinss/utils'

const naturalSort = createNaturalSort({ locale: 'cs' })
const values = ['children', 'groups'].sort(naturalSort)
// ['groups', 'children']
```

## getGitWorktree

Returns information about the linked Git worktree containing the current working directory. The method returns `null` when called from the primary worktree, outside a Git repository, or when Git is unavailable.
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { compose } from './src/compose.js'
export { flatten } from './src/flatten.js'
export { unique } from './src/unique.js'
export { safeEqual } from './src/safe_equal.js'
export { naturalSort } from './src/natural_sort.js'
export { naturalSort, createNaturalSort } from './src/natural_sort.js'
export { isScriptFile } from './src/is_script_file.js'
export { importDefault } from './src/import_default.js'
export { MessageBuilder } from './src/message_builder.js'
Expand Down
18 changes: 12 additions & 6 deletions src/natural_sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
* file that was distributed with this source code.
*/

let collator: Intl.Collator | undefined
/**
* Create a natural sort comparator for a specific locale
*/
export function createNaturalSort({ locale }: { locale: Intl.LocalesArgument }) {
let collator: Intl.Collator | undefined

return function naturalSort(current: string, next: string) {
collator ??= new Intl.Collator(locale, { numeric: true, sensitivity: 'base' })
return collator.compare(current, next)
}
}

/**
* Perform natural sorting with "Array.sort()" method
*/
export function naturalSort(current: string, next: string) {
// Create the collator only when natural sorting is used, not on package import.
collator ??= new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' })
return collator.compare(current, next)
}
export const naturalSort = createNaturalSort({ locale: 'en' })
16 changes: 13 additions & 3 deletions tests/natural_sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { test } from '@japa/runner'
import { naturalSort } from '../src/natural_sort.js'
import { createNaturalSort, naturalSort } from '../src/natural_sort.js'

test.group('natural sort', () => {
test('sort numbers naturally and preserve the order of equivalent strings', ({ assert }) => {
Expand All @@ -21,7 +21,7 @@ test.group('natural sort', () => {
])
})

test('match localeCompare ordering for the default locale', ({ assert }) => {
test('match localeCompare ordering for the English locale', ({ assert }) => {
const values = [
'',
'a',
Expand All @@ -47,7 +47,7 @@ test.group('natural sort', () => {
),
]
const previous = (current: string, next: string) =>
current.localeCompare(next, undefined, { numeric: true, sensitivity: 'base' })
current.localeCompare(next, 'en', { numeric: true, sensitivity: 'base' })

for (const current of values) {
for (const next of values) {
Expand All @@ -57,4 +57,14 @@ test.group('natural sort', () => {
assert.deepEqual([...values].sort(naturalSort), [...values].sort(previous))
assert.deepEqual([...values].reverse().sort(naturalSort), [...values].reverse().sort(previous))
})

test('use English ordering by default', ({ assert }) => {
assert.deepEqual(['groups', 'children'].sort(naturalSort), ['children', 'groups'])
})

test('create a comparator with an explicit locale', ({ assert }) => {
const czechNaturalSort = createNaturalSort({ locale: 'cs' })

assert.deepEqual(['children', 'groups'].sort(czechNaturalSort), ['groups', 'children'])
})
})
Loading