Compare commits
7 Commits
d23e5ce91a
...
1022837ec9
Author | SHA1 | Date |
---|---|---|
Frédéric Matte | 1022837ec9 | |
Frédéric Matte | 04892f1fc5 | |
Frédéric Matte | df851affda | |
Frédéric Matte | 4399839883 | |
Frédéric Matte | f95cd16cf4 | |
Frédéric Matte | 7e950a12f5 | |
Frédéric Matte | 7a6562e44e |
18
index.js
18
index.js
|
@ -1,17 +1,25 @@
|
||||||
const sortby = (properties) => (a, b) => {
|
const sortby = (...properties) => (a, b) => {
|
||||||
|
// 'base' treat accent as there base character
|
||||||
|
const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' })
|
||||||
|
|
||||||
// if items is {prop1:value1, prop2:value2}
|
// if items is {prop1:value1, prop2:value2}
|
||||||
// then sort(['prop1', 'desc', 'prop2'])
|
// then sort(['prop1', 'desc', 'prop2'])
|
||||||
// will sort by prop1 desc and prop2
|
// will sort by prop1 desc and prop2
|
||||||
for (var i = 0; i < properties.length; i++) {
|
for (let i = 0; i < properties.length; i++) {
|
||||||
var property = properties[i]
|
const property = properties[i]
|
||||||
|
|
||||||
let order = -1
|
let order = -1
|
||||||
if (i + 1 < properties.length && properties[i + 1] === 'desc') {
|
if (i + 1 < properties.length) {
|
||||||
|
const next = properties[i + 1]
|
||||||
|
|
||||||
|
if (next === 'desc') {
|
||||||
order = 1
|
order = 1
|
||||||
i++
|
i++
|
||||||
|
} else if (next === 'asc') {
|
||||||
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { deepEqual as equal } from 'node:assert/strict'
|
import { deepEqual as equal } from 'node:assert/strict'
|
||||||
import { metaltest } from 'metaltest'
|
import { metaltest } from 'metaltest'
|
||||||
const test = metaltest('Sort')
|
const test = metaltest('Sort by')
|
||||||
export { test }
|
export { test }
|
||||||
|
|
||||||
import { sortby } from '../index.js'
|
import { sortby } from '../index.js'
|
||||||
|
@ -11,8 +11,7 @@ const data = [
|
||||||
]
|
]
|
||||||
|
|
||||||
test('sort by name ascending', () => {
|
test('sort by name ascending', () => {
|
||||||
const result = data.sort(sortby(['name']))
|
const result = data.sort(sortby('name'))
|
||||||
|
|
||||||
|
|
||||||
equal(result, [
|
equal(result, [
|
||||||
{ age: 40, name: "Persone 1", new: true },
|
{ age: 40, name: "Persone 1", new: true },
|
||||||
|
@ -21,8 +20,7 @@ test('sort by name ascending', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sort by age ascending', () => {
|
test('sort by age ascending', () => {
|
||||||
const result = data.sort(sortby(['age']))
|
const result = data.sort(sortby('age'))
|
||||||
|
|
||||||
|
|
||||||
equal(result, [
|
equal(result, [
|
||||||
{ age: 17, name: "Persone 2", new: false },
|
{ age: 17, name: "Persone 2", new: false },
|
||||||
|
@ -31,8 +29,7 @@ test('sort by age ascending', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sort by age descending', () => {
|
test('sort by age descending', () => {
|
||||||
const result = data.sort(sortby(['age', 'desc']))
|
const result = data.sort(sortby('age', 'desc'))
|
||||||
|
|
||||||
|
|
||||||
equal(result, [
|
equal(result, [
|
||||||
{ age: 40, name: "Persone 1", new: true },
|
{ age: 40, name: "Persone 1", new: true },
|
||||||
|
@ -41,8 +38,7 @@ test('sort by age descending', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sort by new descending', () => {
|
test('sort by new descending', () => {
|
||||||
const result = data.sort(sortby(['new', 'desc']))
|
const result = data.sort(sortby('new', 'desc'))
|
||||||
|
|
||||||
|
|
||||||
equal(result, [
|
equal(result, [
|
||||||
{ age: 40, name: "Persone 1", new: true },
|
{ age: 40, name: "Persone 1", new: true },
|
||||||
|
@ -50,6 +46,13 @@ test('sort by new descending', () => {
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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' }])
|
||||||
|
})
|
||||||
|
|
||||||
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
|
import { runifmain, summaryreporter, errorreporter } from 'metaltest'
|
||||||
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))
|
await runifmain(import.meta, () => test.run(summaryreporter(), errorreporter()))
|
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "sortby",
|
"name": "sortby",
|
||||||
"version": "1.0.0",
|
"version": "2.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "sortby",
|
"name": "sortby",
|
||||||
"version": "1.0.0",
|
"version": "2.0.0",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"metaltest": "git+https://git.tranche.ca/fmatte/metaltest"
|
"metaltest": "git+https://git.tranche.ca/fmatte/metaltest"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "sortby",
|
"name": "sortby",
|
||||||
"description": "Sort object by property name",
|
"description": "Sort object by property name",
|
||||||
"version": "1.0.0",
|
"version": "2.0.0",
|
||||||
"author": "Frédéric Matte <fmatte@gmail.com>",
|
"author": "Frédéric Matte <fmatte@gmail.com>",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
|
Loading…
Reference in New Issue