Skip to content

Commit 670909c

Browse files
authored
RecordId stringification fixes (#355)
1 parent 415ee26 commit 670909c

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/data/types/recordid.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,17 @@ export function escape_ident(str: string): string {
7676
!(code > 96 && code < 123) && // lower alpha (a-z)
7777
!(code === 95) // underscore (_)
7878
) {
79-
return `⟨${str.replaceAll("⟩", "⟩")}⟩`;
79+
return `⟨${str.replaceAll("⟩", "\\⟩")}⟩`;
8080
}
8181
}
8282

8383
return str;
8484
}
8585

8686
export function isOnlyNumbers(str: string): boolean {
87-
const parsed = Number.parseInt(str);
88-
return !Number.isNaN(parsed) && parsed.toString() === str;
87+
const stripped = str.replace("_", "");
88+
const parsed = Number.parseInt(stripped);
89+
return !Number.isNaN(parsed) && parsed.toString() === stripped;
8990
}
9091

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

106107
export function escape_id_part(id: RecordIdValue): string {
107108
return id instanceof Uuid
108-
? `d"${id}"`
109+
? `u"${id}"`
109110
: typeof id === "string"
110111
? escape_ident(id)
111112
: typeof id === "bigint" || typeof id === "number"

tests/unit/recordid.test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { describe, expect, test } from "bun:test";
2-
import { RecordId } from "../../src";
2+
import { RecordId, Uuid } from "../../src";
33

44
describe("record ids", () => {
55
test("toString()", () => {
66
expect(new RecordId("table", 123).toString()).toBe("table:123");
77
expect(new RecordId("table", "123").toString()).toBe("table:⟨123⟩");
8+
expect(new RecordId("table", "123_456").toString()).toBe("table:⟨123_456⟩");
89
expect(new RecordId("table", "test").toString()).toBe("table:test");
910
expect(new RecordId("table", "complex-ident").toString()).toBe(
1011
"table:⟨complex-ident⟩",
1112
);
13+
expect(new RecordId("table", "⟩").toString()).toBe("table:⟨\\⟩⟩");
1214
expect(new RecordId("complex-table", "complex-ident").toString()).toBe(
1315
"⟨complex-table⟩:⟨complex-ident⟩",
1416
);
1517

18+
// UUID
19+
expect(
20+
new RecordId(
21+
"table",
22+
new Uuid("d2f72714-a387-487a-8eae-451330796ff4"),
23+
).toString(),
24+
).toBe('table:u"d2f72714-a387-487a-8eae-451330796ff4"');
25+
1626
// Bigint
1727
expect(new RecordId("table", 9223372036854775807n).toString()).toBe(
1828
"table:9223372036854775807",

0 commit comments

Comments
 (0)