From 99abda4ac1503b764e4cd7585d9a84c534254f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Tue, 19 Sep 2023 12:10:17 -0400 Subject: [PATCH] Sort ascending correctly --- .gitignore | 1 + index.js | 4 ++-- lib/sort.test.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 31 ++++++++++++++++++++++++++++++- package.json | 3 +++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 lib/sort.test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js index 093123f..8099576 100644 --- a/index.js +++ b/index.js @@ -13,8 +13,8 @@ const sortby = (properties) => (a, b) => { const collator = new Intl.Collator('en', { numeric: true, sensitivity: 'base' }) 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 } diff --git a/lib/sort.test.js b/lib/sort.test.js new file mode 100644 index 0000000..2289ea9 --- /dev/null +++ b/lib/sort.test.js @@ -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())) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9a7c780..529611b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,35 @@ { "name": "sortby", + "version": "1.0.0", "lockfileVersion": 3, "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" + } + } + } } diff --git a/package.json b/package.json index 7b98eb7..b25b4f6 100644 --- a/package.json +++ b/package.json @@ -9,5 +9,8 @@ "repository": { "type": "git", "url": "https://git.tranche.ca/fmatte/sortby" + }, + "dependencies": { + "metaltest": "git+https://git.tranche.ca/fmatte/metaltest" } }