-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54c488c
commit 8456b70
Showing
2 changed files
with
10 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@atproto/syntax": patch | ||
--- | ||
|
||
Improve performance of isValidTid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
const TID_LENGTH = 13 | ||
const TID_REGEX = /^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/ | ||
|
||
export const ensureValidTid = (tid: string): void => { | ||
if (tid.length !== 13) { | ||
throw new InvalidTidError('TID must be 13 characters') | ||
} | ||
// simple regex to enforce most constraints via just regex and length. | ||
if (!/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/.test(tid)) { | ||
if (!TID_REGEX.test(tid)) { | ||
throw new InvalidTidError('TID syntax not valid (regex)') | ||
} | ||
} | ||
|
||
export const isValidTid = (tid: string): boolean => { | ||
try { | ||
ensureValidTid(tid) | ||
} catch (err) { | ||
if (err instanceof InvalidTidError) { | ||
return false | ||
} | ||
throw err | ||
} | ||
|
||
return true | ||
return tid.length === TID_LENGTH && TID_REGEX.test(tid) | ||
} | ||
|
||
export class InvalidTidError extends Error {} |