Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecordId stringification fixes #355

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/data/types/recordid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ export function escape_ident(str: string): string {
!(code > 96 && code < 123) && // lower alpha (a-z)
!(code === 95) // underscore (_)
) {
return `⟨${str.replaceAll("⟩", "⟩")}⟩`;
return `⟨${str.replaceAll("⟩", "\\⟩")}⟩`;
}
}

return str;
}

export function isOnlyNumbers(str: string): boolean {
const parsed = Number.parseInt(str);
return !Number.isNaN(parsed) && parsed.toString() === str;
const stripped = str.replace("_", "");
const parsed = Number.parseInt(stripped);
return !Number.isNaN(parsed) && parsed.toString() === stripped;
}

export function isValidIdPart(v: unknown): v is RecordIdValue {
Expand All @@ -105,7 +106,7 @@ export function isValidIdPart(v: unknown): v is RecordIdValue {

export function escape_id_part(id: RecordIdValue): string {
return id instanceof Uuid
? `d"${id}"`
? `u"${id}"`
: typeof id === "string"
? escape_ident(id)
: typeof id === "bigint" || typeof id === "number"
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/recordid.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { describe, expect, test } from "bun:test";
import { RecordId } from "../../src";
import { RecordId, Uuid } from "../../src";

describe("record ids", () => {
test("toString()", () => {
expect(new RecordId("table", 123).toString()).toBe("table:123");
expect(new RecordId("table", "123").toString()).toBe("table:⟨123⟩");
expect(new RecordId("table", "123_456").toString()).toBe("table:⟨123_456⟩");
expect(new RecordId("table", "test").toString()).toBe("table:test");
expect(new RecordId("table", "complex-ident").toString()).toBe(
"table:⟨complex-ident⟩",
);
expect(new RecordId("table", "⟩").toString()).toBe("table:⟨\\⟩⟩");
expect(new RecordId("complex-table", "complex-ident").toString()).toBe(
"⟨complex-table⟩:⟨complex-ident⟩",
);

// UUID
expect(
new RecordId(
"table",
new Uuid("d2f72714-a387-487a-8eae-451330796ff4"),
).toString(),
).toBe('table:u"d2f72714-a387-487a-8eae-451330796ff4"');

// Bigint
expect(new RecordId("table", 9223372036854775807n).toString()).toBe(
"table:9223372036854775807",
Expand Down