Comparing version 0.2.1 to 0.3.0
@@ -551,3 +551,3 @@ "use strict"; | ||
var TypeID = class { | ||
constructor(prefix = "", suffix = "") { | ||
constructor(prefix, suffix = "") { | ||
this.prefix = prefix; | ||
@@ -579,2 +579,9 @@ this.suffix = suffix; | ||
} | ||
asType(prefix) { | ||
const self = this; | ||
if (self.prefix !== prefix) { | ||
throw new Error(`Cannot convert TypeID of type ${self.prefix} to type ${prefix}`); | ||
} | ||
return self; | ||
} | ||
toUUIDBytes() { | ||
@@ -606,7 +613,7 @@ return decode(this.suffix); | ||
} | ||
static fromUUIDBytes(prefix = "", bytes = new Uint8Array(16)) { | ||
static fromUUIDBytes(prefix, bytes) { | ||
const suffix = encode(bytes); | ||
return new TypeID(prefix, suffix); | ||
} | ||
static fromUUID(prefix = "", uuid = "") { | ||
static fromUUID(prefix, uuid) { | ||
const suffix = encode(parseUUID(uuid)); | ||
@@ -616,3 +623,5 @@ return new TypeID(prefix, suffix); | ||
}; | ||
var typeid = (prefix = "", suffix = "") => new TypeID(prefix, suffix); | ||
function typeid(prefix = "", suffix = "") { | ||
return new TypeID(prefix, suffix); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -619,0 +628,0 @@ 0 && (module.exports = { |
@@ -551,3 +551,3 @@ "use strict"; | ||
var TypeID = class { | ||
constructor(prefix = "", suffix = "") { | ||
constructor(prefix, suffix = "") { | ||
this.prefix = prefix; | ||
@@ -579,2 +579,9 @@ this.suffix = suffix; | ||
} | ||
asType(prefix) { | ||
const self = this; | ||
if (self.prefix !== prefix) { | ||
throw new Error(`Cannot convert TypeID of type ${self.prefix} to type ${prefix}`); | ||
} | ||
return self; | ||
} | ||
toUUIDBytes() { | ||
@@ -606,7 +613,7 @@ return decode(this.suffix); | ||
} | ||
static fromUUIDBytes(prefix = "", bytes = new Uint8Array(16)) { | ||
static fromUUIDBytes(prefix, bytes) { | ||
const suffix = encode(bytes); | ||
return new TypeID(prefix, suffix); | ||
} | ||
static fromUUID(prefix = "", uuid = "") { | ||
static fromUUID(prefix, uuid) { | ||
const suffix = encode(parseUUID(uuid)); | ||
@@ -616,3 +623,5 @@ return new TypeID(prefix, suffix); | ||
}; | ||
var typeid = (prefix = "", suffix = "") => new TypeID(prefix, suffix); | ||
function typeid(prefix = "", suffix = "") { | ||
return new TypeID(prefix, suffix); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -619,0 +628,0 @@ 0 && (module.exports = { |
@@ -1,16 +0,19 @@ | ||
declare class TypeID { | ||
declare class TypeID<const T extends string> { | ||
private prefix; | ||
private suffix; | ||
constructor(prefix?: string, suffix?: string); | ||
getType(): string; | ||
constructor(prefix: T, suffix?: string); | ||
getType(): T; | ||
getSuffix(): string; | ||
asType<const U extends string>(prefix: U): TypeID<U>; | ||
toUUIDBytes(): Uint8Array; | ||
toUUID(): string; | ||
toString(): string; | ||
static fromString(str: string): TypeID; | ||
static fromUUIDBytes(prefix?: string, bytes?: Uint8Array): TypeID; | ||
static fromUUID(prefix?: string, uuid?: string): TypeID; | ||
static fromString<const T extends string>(str: string): TypeID<T>; | ||
static fromUUIDBytes<const T extends string>(prefix: T, bytes: Uint8Array): TypeID<T>; | ||
static fromUUID<const T extends string>(prefix: T, uuid: string): TypeID<T>; | ||
} | ||
declare const typeid: (prefix?: string, suffix?: string) => TypeID; | ||
declare function typeid<T extends string>(): TypeID<''>; | ||
declare function typeid<T extends string>(prefix: T): TypeID<T>; | ||
declare function typeid<T extends string>(prefix: T, suffix: string): TypeID<T>; | ||
export { TypeID, typeid }; |
@@ -551,3 +551,3 @@ "use strict"; | ||
var TypeID = class { | ||
constructor(prefix = "", suffix = "") { | ||
constructor(prefix, suffix = "") { | ||
this.prefix = prefix; | ||
@@ -579,2 +579,9 @@ this.suffix = suffix; | ||
} | ||
asType(prefix) { | ||
const self = this; | ||
if (self.prefix !== prefix) { | ||
throw new Error(`Cannot convert TypeID of type ${self.prefix} to type ${prefix}`); | ||
} | ||
return self; | ||
} | ||
toUUIDBytes() { | ||
@@ -606,7 +613,7 @@ return decode(this.suffix); | ||
} | ||
static fromUUIDBytes(prefix = "", bytes = new Uint8Array(16)) { | ||
static fromUUIDBytes(prefix, bytes) { | ||
const suffix = encode(bytes); | ||
return new TypeID(prefix, suffix); | ||
} | ||
static fromUUID(prefix = "", uuid = "") { | ||
static fromUUID(prefix, uuid) { | ||
const suffix = encode(parseUUID(uuid)); | ||
@@ -616,3 +623,5 @@ return new TypeID(prefix, suffix); | ||
}; | ||
var typeid = (prefix = "", suffix = "") => new TypeID(prefix, suffix); | ||
function typeid(prefix = "", suffix = "") { | ||
return new TypeID(prefix, suffix); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -619,0 +628,0 @@ 0 && (module.exports = { |
{ | ||
"name": "typeid-js", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Official implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -38,3 +38,3 @@ # Official TypeID-JS Package | ||
The prefix is optional, so if you need to create an id with a type prefix, you | ||
The prefix is optional, so if you need to create an id without a type prefix, you | ||
can do that too: | ||
@@ -47,5 +47,18 @@ | ||
In addition to the `typeid()` function, there's also a `TypeID` class that can | ||
be used to encode/decode from other formats. | ||
The return type of `typeid("prefix")` is `TypeID<"prefix">`, which lets you use | ||
TypeScript's type checking to ensure you are passing the correct type prefix to | ||
functions that expect it. | ||
For example, you can create a function that only accepts TypeIDs of type `user`: | ||
```typescript | ||
import { typeid, TypeID } from 'typeid-js'; | ||
function doSomethingWithUserID(id: TypeID<"user">) { | ||
// ... | ||
} | ||
``` | ||
In addition to the `typeid()` function, the `TypeID` class has additional methods | ||
to encode/decode from other formats. | ||
For example, to parse an existing typeid from a string: | ||
@@ -55,3 +68,5 @@ ```typescript | ||
const tid = TypeID.fromString("prefix_00041061050r3gg28a1c60t3gf); | ||
// The asType() call is optional, but it converts to type TypeID<"prefix"> instead | ||
// of TypeID<string> | ||
const tid = TypeID.fromString("prefix_00041061050r3gg28a1c60t3gf").asType("prefix"); | ||
``` | ||
@@ -63,2 +78,3 @@ | ||
// In this case TypeID<"prefix"> is inferred from the first argument | ||
const tid = TypeID.fromUUID("prefix", "00000000-0000-0000-0000-000000000000"); | ||
@@ -65,0 +81,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
190070
2988
88