Sort ascending correctly
parent
3b0a56ba50
commit
99abda4ac1
|
@ -0,0 +1 @@
|
||||||
|
node_modules
|
4
index.js
4
index.js
|
@ -13,8 +13,8 @@ const sortby = (properties) => (a, b) => {
|
||||||
|
|
||||||
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
||||||
const compare = collator.compare(a[property], b[property])
|
const compare = collator.compare(a[property], b[property])
|
||||||
if (compare > 0) return order
|
if (compare > 0) return -order
|
||||||
if (compare < 0) return -order
|
if (compare < 0) return order
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
import { deepEqual as equal } from 'node:assert/strict'
|
||||||
|
import { metaltest } from 'metaltest'
|
||||||
|
const test = metaltest('Sort')
|
||||||
|
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 },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
|
||||||
|
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))
|
|
@ -1,6 +1,35 @@
|
||||||
{
|
{
|
||||||
"name": "sortby",
|
"name": "sortby",
|
||||||
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {}
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "sortby",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"metaltest": "git+https://git.tranche.ca/fmatte/metaltest"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/chalk": {
|
||||||
|
"version": "5.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz",
|
||||||
|
"integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==",
|
||||||
|
"engines": {
|
||||||
|
"node": "^12.17.0 || ^14.13 || >=16.0.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/chalk/chalk?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/metaltest": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "git+https://git.tranche.ca/fmatte/metaltest#90b20029c9dde21218e2464a1e8958435fd42286",
|
||||||
|
"license": "AGPL-3.0-or-later",
|
||||||
|
"dependencies": {
|
||||||
|
"chalk": "^5.1.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,5 +9,8 @@
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.tranche.ca/fmatte/sortby"
|
"url": "https://git.tranche.ca/fmatte/sortby"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"metaltest": "git+https://git.tranche.ca/fmatte/metaltest"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue