Comparing version 0.6.0 to 0.7.0
@@ -714,5 +714,10 @@ "use strict"; | ||
code = str.charCodeAt(i); | ||
if (!(code > 96 && code < 123)) { | ||
const isLowerAtoZ = code > 96 && code < 123; | ||
const isUnderscore = code === 95; | ||
if ((i === 0 || i === len - 1) && !isLowerAtoZ) { | ||
return false; | ||
} | ||
if (!(isLowerAtoZ || isUnderscore)) { | ||
return false; | ||
} | ||
} | ||
@@ -725,3 +730,3 @@ return true; | ||
if (!isValidPrefix(prefix)) { | ||
throw new Error("Invalid prefix. Must be at most 63 ascii letters [a-z]"); | ||
throw new Error("Invalid prefix. Must be at most 63 ascii letters [a-z_]"); | ||
} | ||
@@ -753,7 +758,12 @@ let finalSuffix; | ||
function fromString(typeId, prefix) { | ||
const parts = typeId.split("_"); | ||
if (parts.length === 1) { | ||
return typeidUnboxed("", parts[0]); | ||
} else if (parts.length === 2) { | ||
if (parts[0] === "") { | ||
let p; | ||
let s; | ||
const underscoreIndex = typeId.lastIndexOf("_"); | ||
if (underscoreIndex === -1) { | ||
p = ""; | ||
s = typeId; | ||
} else { | ||
p = typeId.substring(0, underscoreIndex); | ||
s = typeId.substring(underscoreIndex + 1); | ||
if (!p) { | ||
throw new Error( | ||
@@ -763,11 +773,9 @@ `Invalid TypeId. Prefix cannot be empty when there's a separator: ${typeId}` | ||
} | ||
if (prefix && parts[0] !== prefix) { | ||
throw new Error( | ||
`Invalid TypeId. Prefix mismatch. Expected ${prefix}, got ${parts[0]}` | ||
); | ||
} | ||
return typeidUnboxed(parts[0], parts[1]); | ||
} else { | ||
throw new Error(`Invalid TypeId format: ${typeId}`); | ||
} | ||
if (prefix && p !== prefix) { | ||
throw new Error( | ||
`Invalid TypeId. Prefix mismatch. Expected ${prefix}, got ${p}` | ||
); | ||
} | ||
return typeidUnboxed(p, s); | ||
} | ||
@@ -778,3 +786,3 @@ function parseTypeId(typeId) { | ||
function getType(typeId) { | ||
const underscoreIndex = typeId.indexOf("_"); | ||
const underscoreIndex = typeId.lastIndexOf("_"); | ||
if (underscoreIndex === -1) { | ||
@@ -786,3 +794,3 @@ return ""; | ||
function getSuffix(typeId) { | ||
const underscoreIndex = typeId.indexOf("_"); | ||
const underscoreIndex = typeId.lastIndexOf("_"); | ||
if (underscoreIndex === -1) { | ||
@@ -789,0 +797,0 @@ return typeId; |
{ | ||
"name": "typeid-js", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "Official implementation of the TypeID specification in TypeScript. TypeIDs are type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs", | ||
@@ -38,3 +38,3 @@ "keywords": [ | ||
"fmt": "pnpm lint --fix", | ||
"lint": "prettier --write .", | ||
"lint": "prettier '(src|test)/**/*.ts' --write", | ||
"test": "jest" | ||
@@ -41,0 +41,0 @@ }, |
@@ -36,2 +36,4 @@ # Official TypeID-JS Package | ||
Note: this package requires Typescript > 5.0.0 | ||
# Usage | ||
@@ -38,0 +40,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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
158868
1808
103
0