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

[typeid-js] Enforce prefix (optionally) #344

Merged
merged 4 commits into from
Jul 2, 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
10 changes: 4 additions & 6 deletions typeid/typeid-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ For example, to parse an existing typeid from a string:
```typescript
import { TypeID } from 'typeid-js';

// The asType() call is optional, but it converts to type TypeID<"prefix"> instead
// of TypeID<string>
const tid = TypeID.fromString('prefix_00041061050r3gg28a1c60t3gf').asType(
'prefix'
);
// The prefix is optional, but it enforces the prefix and returns a
// TypeID<"prefix"> instead of TypeID<string>
const tid = TypeID.fromString('prefix_00041061050r3gg28a1c60t3gf', 'prefix');
```

To encode an existing UUID as a TypeID:
Expand All @@ -98,6 +96,6 @@ The full list of methods includes:
- `toString()`: Encodes the object as a string, using the canonical format
- `toUUID()`: Decodes the TypeID into a UUID string in hex format. The type prefix is ignored
- `toUUIDBytes()`: Decodes the TypeID into a UUID byte array. The type prefix is ignored
- `fromString(str)`: Parses a TypeID from a string
- `fromString(str, prefix?)`: Parses a TypeID from a string, optionally checking the prefix
- `fromUUID(prefix, uuid)`: Creates a TypeID from a prefix and a UUID in hex format
- `fromUUIDBytes(prefix, bytes)`: Creates a TypeID from a prefix and a UUID in byte array format
4 changes: 2 additions & 2 deletions typeid/typeid-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeid-js",
"version": "0.7.0",
"version": "0.8.0",
"description": "Official implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs",
"keywords": [
"typeid",
Expand Down Expand Up @@ -51,4 +51,4 @@
"dependencies": {
"uuidv7": "^0.6.2"
}
}
}
4 changes: 2 additions & 2 deletions typeid/typeid-js/src/typeid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export class TypeID<const T extends string> {
return `${this.prefix}_${this.suffix}`;
}

static fromString<const T extends string>(str: string): TypeID<T> {
const typeIdRaw = fromString(str);
static fromString<const T extends string>(str: string, prefix?: T): TypeID<T> {
const typeIdRaw = fromString(str, prefix);

return new TypeID<T>(getType(typeIdRaw) as T, getSuffix(typeIdRaw));
}
Expand Down
16 changes: 16 additions & 0 deletions typeid/typeid-js/test/typeid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ describe("TypeID", () => {
expect(tid.getType()).toBe("prefix");
});

it("should construct TypeID from a string with prefix and specified prefix", () => {
const str = "prefix_00041061050r3gg28a1c60t3gf";
const tid = TypeID.fromString(str, "prefix");

expect(tid.getSuffix()).toBe("00041061050r3gg28a1c60t3gf");
expect(tid.getType()).toBe("prefix");
});

it("should throw an error for invalid TypeID string", () => {
const invalidStr = "invalid_string_with_underscore0000000000000000";

Expand All @@ -107,6 +115,14 @@ describe("TypeID", () => {
new Error(`Invalid suffix. First character must be in the range [0-7]`)
);
});
it("should throw an error with wrong prefix", () => {
const str = "prefix_00041061050r3gg28a1c60t3gf";
expect(() => {
TypeID.fromString(str, "wrong");
}).toThrowError(
new Error(`Invalid TypeId. Prefix mismatch. Expected wrong, got prefix`)
);
});
});

describe("fromUUIDBytes", () => {
Expand Down