2023-09-19 12:10:17 -04:00
|
|
|
import { deepEqual as equal } from 'node:assert/strict'
|
|
|
|
import { metaltest } from 'metaltest'
|
2023-09-28 22:56:40 -04:00
|
|
|
const test = metaltest('Sort by')
|
2023-09-19 12:10:17 -04:00
|
|
|
export { test }
|
|
|
|
|
|
|
|
import { sortby } from '../index.js'
|
|
|
|
|
|
|
|
const data = [
|
|
|
|
{ age: 40, name: "Persone 1", new: true },
|
|
|
|
{ age: 17, name: "Persone 2", new: false },
|
|
|
|
]
|
|
|
|
|
|
|
|
test('sort by name ascending', () => {
|
|
|
|
const result = data.sort(sortby(['name']))
|
|
|
|
|
|
|
|
|
|
|
|
equal(result, [
|
|
|
|
{ age: 40, name: "Persone 1", new: true },
|
|
|
|
{ age: 17, name: "Persone 2", new: false },
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('sort by age ascending', () => {
|
|
|
|
const result = data.sort(sortby(['age']))
|
|
|
|
|
|
|
|
|
|
|
|
equal(result, [
|
|
|
|
{ age: 17, name: "Persone 2", new: false },
|
|
|
|
{ age: 40, name: "Persone 1", new: true },
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('sort by age descending', () => {
|
|
|
|
const result = data.sort(sortby(['age', 'desc']))
|
|
|
|
|
|
|
|
|
|
|
|
equal(result, [
|
|
|
|
{ age: 40, name: "Persone 1", new: true },
|
|
|
|
{ age: 17, name: "Persone 2", new: false },
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2023-09-19 14:20:25 -04:00
|
|
|
test('sort by new descending', () => {
|
|
|
|
const result = data.sort(sortby(['new', 'desc']))
|
|
|
|
|
|
|
|
|
|
|
|
equal(result, [
|
|
|
|
{ age: 40, name: "Persone 1", new: true },
|
|
|
|
{ age: 17, name: "Persone 2", new: false },
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2023-09-28 23:12:49 -04:00
|
|
|
test('sort with accent, should not change order', () => {
|
|
|
|
const data = [{ name: 'è' }, { name: 'é' }, { name: 'e' }]
|
|
|
|
|
|
|
|
const result = data.sort(sortby(['name']))
|
|
|
|
|
|
|
|
equal(result, [{ name: 'è' }, { name: 'é' }, { name: 'e' }])
|
|
|
|
})
|
2023-09-19 14:20:25 -04:00
|
|
|
|
2023-09-19 12:10:17 -04:00
|
|
|
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
|
|
|
|
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))
|