-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use uuid25 format; fix sorting (#288)
- convert uuidv7 to uuid25 for a more compact format - centralize id generation within a util, test it - fix importer / create not generating ids using the existing timestamp; ids should now sort by note creation time (only lightly tested)
- Loading branch information
Showing
9 changed files
with
90 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { assert } from "chai"; | ||
import { suite, test } from "mocha"; | ||
import { createId } from "./util"; | ||
|
||
suite("id generation", () => { | ||
test("it generates ids in order", () => { | ||
const ids = [createId(), createId(), createId()]; | ||
|
||
assert.sameOrderedMembers(ids, ids.slice().sort()); | ||
}); | ||
|
||
test("it generates ids in order when timestamp provided", () => { | ||
const backwards = [ | ||
createId(Date.parse("2024-01-01")), | ||
createId(Date.parse("2023-01-01")), | ||
createId(Date.parse("2022-01-01")), | ||
]; | ||
|
||
assert.sameOrderedMembers( | ||
backwards.slice().reverse(), | ||
backwards.slice().sort(), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Uuid25 } from "uuid25"; | ||
import { UUID, V7Generator, uuidv7obj } from "uuidv7"; | ||
|
||
const uuidV7Generator = new V7Generator(); | ||
|
||
/** | ||
* Generates a time-sortable chronicles id, optionally incorporating the | ||
* document / files timestamp so it sorts by creation date | ||
* | ||
* @param unixTsMs - e.g. from Date.parse or new Date().getTime() | ||
* @returns A uuid25 formatted string | ||
*/ | ||
export function createId(unixTsMs?: number): string { | ||
const uuid = unixTsMs | ||
? uuidV7Generator.generateOrResetCore(unixTsMs, 10_000) | ||
: uuidv7obj(); | ||
const id = Uuid25.fromBytes(uuid.bytes); | ||
return id.value; | ||
} | ||
|
||
/** | ||
* Convert (legacy) uuidv7 str to uuid25 | ||
*/ | ||
export function convertId(uuidV7Str: string): string { | ||
const uuid = UUID.parse(uuidV7Str); | ||
const id = Uuid25.fromBytes(uuid.bytes); | ||
return id.value; | ||
} | ||
|
||
/** | ||
* Throw if uuid string is invalid | ||
*/ | ||
export function checkId(uuid25Str: string): void { | ||
Uuid25.parseUuid25(uuid25Str); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters