diff options
author | EuAndreh <eu@euandre.org> | 2024-02-28 11:30:33 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-02-28 11:30:33 -0300 |
commit | ba13007dce63d211eb6a168f502f58af01a6ca1f (patch) | |
tree | 9bfbc3da317ed3cf8ec7e75e9e07b8e68acdafef | |
parent | tests/runner.mjs: Use process.stderr.write() to avoid printing newlines when ... (diff) | |
download | papod-ba13007dce63d211eb6a168f502f58af01a6ca1f.tar.gz papod-ba13007dce63d211eb6a168f502f58af01a6ca1f.tar.xz |
src/utils.mjs: Define strSortFn() and use it on all files
-rw-r--r-- | src/accretion.mjs | 3 | ||||
-rw-r--r-- | src/hero.mjs | 4 | ||||
-rw-r--r-- | src/utils.mjs | 2 | ||||
-rw-r--r-- | tests/js/accretion.mjs | 3 | ||||
-rw-r--r-- | tests/js/utils.mjs | 18 |
5 files changed, 25 insertions, 5 deletions
diff --git a/src/accretion.mjs b/src/accretion.mjs index 1c70162..dd3372c 100644 --- a/src/accretion.mjs +++ b/src/accretion.mjs @@ -26,8 +26,7 @@ export const runMigrations = async (logFn, db) => { .map(row => row.filename); const allFiles = fs.readdirSync(MIGRATIONS_DIR, "UTF-8"); const pending = u.difference(new Set(allFiles), new Set(done)); - const sortedPending = [...pending] - .sort((a, b) => a.localeCompare(b, "POSIX")); + const sortedPending = [...pending].sort(u.strSortFn); for (const filename of sortedPending) { logFn({ log: "exec-migration", filename }); diff --git a/src/hero.mjs b/src/hero.mjs index 268136f..c249d4f 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -31,7 +31,7 @@ const HTTP_METHODS = new Set([ "OPTIONS", ]); -const HTTP_METHODS_ARR = [...HTTP_METHODS.keys()].sort(); +const HTTP_METHODS_ARR = [...HTTP_METHODS.keys()].sort(u.strSortFn); export const addRoute = (table, methods, path, handlerFn) => { if (methods === "*") { @@ -88,7 +88,7 @@ export const firstParamMatch = (tree, segments, params) => { // literal matching failed, we now look for patterns that might match const paramOptions = Object.keys(tree) .filter(s => s.startsWith(":")) - .sort(); + .sort(u.strSortFn); return u.first(paramOptions, param => firstParamMatch(tree[param], nextSegments, { ...params, [param.slice(1)]: seg diff --git a/src/utils.mjs b/src/utils.mjs index c671f0f..cde6934 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -80,3 +80,5 @@ export const promisify = fn => (...args) => export const partial = (fn, ...startArgs) => (...endArgs) => fn(...startArgs, ...endArgs); + +export const strSortFn = (a, b) => a.localeCompare(b, "POSIX"); diff --git a/tests/js/accretion.mjs b/tests/js/accretion.mjs index 73c93c8..14c86d3 100644 --- a/tests/js/accretion.mjs +++ b/tests/js/accretion.mjs @@ -5,6 +5,7 @@ import sqlite from "../../src/sqlite.cjs"; import * as runner from "../runner.mjs"; import * as db from "../../src/db.mjs"; +import * as u from "../../src/utils.mjs"; import { MIGRATIONS_DIR, runMigrations, @@ -35,7 +36,7 @@ const test_runMigrations = t => { const migrationLogs = fs .readdirSync(MIGRATIONS_DIR) - .sort((a, b) => a.localeCompare(b, "POSIX")) + .sort(u.strSortFn) .map(filename => ({ log: "exec-migration", filename })); assert.deepEqual(contents, migrationLogs); }); diff --git a/tests/js/utils.mjs b/tests/js/utils.mjs index 004af95..178f6c8 100644 --- a/tests/js/utils.mjs +++ b/tests/js/utils.mjs @@ -10,6 +10,7 @@ import { first, promisify, partial, + strSortFn, } from "../../src/utils.mjs"; const test_eq = t => { @@ -344,6 +345,22 @@ const test_partial = t => { }); }; +const test_strSortFn = t => { + t.start("strSortFn()"); + + t.test("empty value", () => { + assert.equal(strSortFn("", ""), 0); + }); + + t.test("default sort", () => { + const arr = [ "a", "Z" ]; + assert.deepEqual( + [...arr].sort(strSortFn), + [...arr].sort().reverse(), + ); + }); +}; + await runner.runTests([ test_eq, @@ -354,4 +371,5 @@ await runner.runTests([ test_first, test_promisify, test_partial, + test_strSortFn, ]); |