feature: default sorts
This commit is contained in:
parent
05178c924f
commit
e32ee96b73
3 changed files with 93 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@andyburke/fsdb",
|
||||
"version": "0.8.0",
|
||||
"version": "0.9.0",
|
||||
"license": "MIT",
|
||||
"exports": {
|
||||
".": "./fsdb.ts",
|
||||
|
|
18
fsdb.ts
18
fsdb.ts
|
@ -427,4 +427,22 @@ export class FSDB_COLLECTION<T extends Record<string, any>> {
|
|||
listener(event_data);
|
||||
}
|
||||
}
|
||||
|
||||
public sorts = {
|
||||
newest: (a: WALK_ENTRY<T>, b: WALK_ENTRY<T>) =>
|
||||
((b.info.birthtime ?? b.info.ctime)?.toISOString() ?? '').localeCompare(
|
||||
(a.info.birthtime ?? a.info.ctime)?.toISOString() ?? ''
|
||||
),
|
||||
|
||||
oldest: (a: WALK_ENTRY<T>, b: WALK_ENTRY<T>) =>
|
||||
((a.info.birthtime ?? a.info.ctime)?.toISOString() ?? '').localeCompare(
|
||||
(b.info.birthtime ?? b.info.ctime)?.toISOString() ?? ''
|
||||
),
|
||||
|
||||
latest: (a: WALK_ENTRY<T>, b: WALK_ENTRY<T>) =>
|
||||
((b.info.mtime ?? b.info.ctime)?.toISOString() ?? '').localeCompare((a.info.mtime ?? a.info.ctime)?.toISOString() ?? ''),
|
||||
|
||||
stalest: (a: WALK_ENTRY<T>, b: WALK_ENTRY<T>) =>
|
||||
((a.info.mtime ?? a.info.ctime)?.toISOString() ?? '').localeCompare((b.info.mtime ?? b.info.ctime)?.toISOString() ?? '')
|
||||
};
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ const item_collection: fsdb.FSDB_COLLECTION<ITEM> = new fsdb.FSDB_COLLECTION<ITE
|
|||
});
|
||||
|
||||
const items: ITEM[] = [];
|
||||
const item_count: number = 1_000;
|
||||
const item_count: number = 1_00;
|
||||
const midpoint: number = Math.floor(item_count / 2);
|
||||
let first_id = null;
|
||||
let time_mid = null;
|
||||
|
@ -57,7 +57,11 @@ const sorted_items = {
|
|||
return { id: item.id, value: item.value, created: item.created };
|
||||
}),
|
||||
|
||||
by_created: items.sort((lhs, rhs) => 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<ITEM>, b: fsdb.WALK_ENTRY<ITEM>) =>
|
||||
(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);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue