From e32ee96b73f371e34bdd9406b298e05b59b1b904 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Wed, 2 Jul 2025 19:01:31 -0700 Subject: [PATCH] feature: default sorts --- deno.json | 2 +- fsdb.ts | 18 +++++++++ tests/06_test_all.test.ts | 78 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/deno.json b/deno.json index 3540e04..acba4b9 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@andyburke/fsdb", - "version": "0.8.0", + "version": "0.9.0", "license": "MIT", "exports": { ".": "./fsdb.ts", diff --git a/fsdb.ts b/fsdb.ts index b0a0a39..694256c 100644 --- a/fsdb.ts +++ b/fsdb.ts @@ -427,4 +427,22 @@ export class FSDB_COLLECTION> { listener(event_data); } } + + public sorts = { + newest: (a: WALK_ENTRY, b: WALK_ENTRY) => + ((b.info.birthtime ?? b.info.ctime)?.toISOString() ?? '').localeCompare( + (a.info.birthtime ?? a.info.ctime)?.toISOString() ?? '' + ), + + oldest: (a: WALK_ENTRY, b: WALK_ENTRY) => + ((a.info.birthtime ?? a.info.ctime)?.toISOString() ?? '').localeCompare( + (b.info.birthtime ?? b.info.ctime)?.toISOString() ?? '' + ), + + latest: (a: WALK_ENTRY, b: WALK_ENTRY) => + ((b.info.mtime ?? b.info.ctime)?.toISOString() ?? '').localeCompare((a.info.mtime ?? a.info.ctime)?.toISOString() ?? ''), + + stalest: (a: WALK_ENTRY, b: WALK_ENTRY) => + ((a.info.mtime ?? a.info.ctime)?.toISOString() ?? '').localeCompare((b.info.mtime ?? b.info.ctime)?.toISOString() ?? '') + }; } diff --git a/tests/06_test_all.test.ts b/tests/06_test_all.test.ts index 43e63a5..67066ef 100644 --- a/tests/06_test_all.test.ts +++ b/tests/06_test_all.test.ts @@ -17,7 +17,7 @@ const item_collection: fsdb.FSDB_COLLECTION = new fsdb.FSDB_COLLECTION lhs.created.localeCompare(rhs.created)).map((item) => { + by_created_newest: items.sort((lhs, rhs) => rhs.created.localeCompare(lhs.created)).map((item) => { + return { id: item.id, value: item.value, created: item.created }; + }), + + by_created_oldest: items.sort((lhs, rhs) => lhs.created.localeCompare(rhs.created)).map((item) => { return { id: item.id, value: item.value, created: item.created }; }) }; @@ -121,7 +125,7 @@ Deno.test({ limit, offset: offset, sort: (a: fsdb.WALK_ENTRY, b: fsdb.WALK_ENTRY) => - (a.info.birthtime?.toISOString() ?? '').localeCompare(b.info.birthtime?.toISOString() ?? '') + (b.info.birthtime?.toISOString() ?? '').localeCompare(a.info.birthtime?.toISOString() ?? '') }); fetched.push(...(fetched_items.map((item) => item.load()))); @@ -129,7 +133,73 @@ Deno.test({ more = fetched_items.length === limit; } while (more); - asserts.assertEquals(fetched, sorted_items.by_created); + asserts.assertEquals(fetched, sorted_items.by_created_newest); + } +}); + +Deno.test({ + name: 'sort all() by default `oldest` sort', + permissions: { + env: true, + // https://github.com/denoland/deno/discussions/17258 + read: true, + write: true + }, + fn: async () => { + asserts.assert(item_collection); + + let offset = 0; + const fetched = []; + let more = true; + do { + // fuzz the limit + const limit = Math.floor(Math.random() * (LIMIT_MAX - LIMIT_MIN + 1)) + LIMIT_MIN; + + const fetched_items = await item_collection.all({ + limit, + offset: offset, + sort: item_collection.sorts.oldest + }); + + fetched.push(...(fetched_items.map((item) => item.load()))); + offset += fetched_items.length; + more = fetched_items.length === limit; + } while (more); + + asserts.assertEquals(fetched, sorted_items.by_created_oldest); + } +}); + +Deno.test({ + name: 'sort all() by default `newest` sort', + permissions: { + env: true, + // https://github.com/denoland/deno/discussions/17258 + read: true, + write: true + }, + fn: async () => { + asserts.assert(item_collection); + + let offset = 0; + const fetched = []; + let more = true; + do { + // fuzz the limit + const limit = Math.floor(Math.random() * (LIMIT_MAX - LIMIT_MIN + 1)) + LIMIT_MIN; + + const fetched_items = await item_collection.all({ + limit, + offset: offset, + sort: item_collection.sorts.newest + }); + + fetched.push(...(fetched_items.map((item) => item.load()))); + offset += fetched_items.length; + more = fetched_items.length === limit; + } while (more); + + asserts.assertEquals(fetched, sorted_items.by_created_newest); } });