Skip to content
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
20 changes: 20 additions & 0 deletions scripts/browser-smoke.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
getTimestamp,
id,
isId,
parseId,
sortableId,
template,
} from "../dist/index.js";
Expand All @@ -56,6 +57,25 @@
if (!isId(uid, "user")) throw new Error("id() failed");
lines.push(`id("user") -> ${uid}`);
lines.push(`getPrefix(...) -> ${getPrefix(uid)}`);

const parsed = parseId(uid);
if (
!parsed ||
parsed.prefix !== "user" ||
parsed.id !== uid.substring(5)
) {
throw new Error("parseId() round-trip failed");
}
if (parseId("nosep") !== undefined) {
throw new Error("parseId() nosep failed");
}
if (parseId(null) !== undefined) {
throw new Error("parseId() null failed");
}
lines.push(
`parseId(...) -> { prefix: "${parsed.prefix}", id: "${parsed.id}" }`,
);

lines.push(
`createId({ size: 8 }) -> ${createId({ size: 8 })("order")}`,
);
Expand Down
19 changes: 19 additions & 0 deletions scripts/smoke.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
id,
isId,
parseId,
getPrefix,
sortableId,
getTimestamp,
Expand All @@ -15,6 +16,24 @@ if (!isId(uid, "user") || getPrefix(uid) !== "user") {
process.exit(1);
}

const parsed = parseId(uid);
if (!parsed || parsed.prefix !== "user" || parsed.id !== uid.substring(5)) {
console.error("smoke failed: parseId() should round-trip a generated id");
process.exit(1);
}
if (parseId("nosep") !== undefined) {
console.error(
"smoke failed: parseId() should return undefined when separator is missing",
);
process.exit(1);
}
if (parseId(null) !== undefined) {
console.error(
"smoke failed: parseId() should return undefined for non-string input",
);
process.exit(1);
}

const sid = sortableId("evt");
if (
!isId(sid, "evt") ||
Expand Down
17 changes: 17 additions & 0 deletions scripts/smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getTimestampOrThrow,
id,
isId,
parseId,
sortableId,
template,
} from "prefid";
Expand All @@ -30,6 +31,22 @@ const uid = id("user");
assert(isId(uid, "user"), "id() should produce a user_ id");
assert(getPrefix(uid) === "user", "getPrefix() should return the prefix");

const parsed = parseId(uid);
assert(
parsed !== undefined &&
parsed.prefix === "user" &&
parsed.id === uid.substring(5),
"parseId() should round-trip a generated id",
);
assert(
parseId("nosep") === undefined,
"parseId() should return undefined when separator is missing",
);
assert(
parseId(null) === undefined,
"parseId() should return undefined for non-string input",
);

const short = createId({ size: 8 })("order");
assert(short.length === "order_".length + 8, "createId({ size }) length");

Expand Down
Loading