Skip to content

Commit

Permalink
Improve performance of isValidTid
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieusieben committed Nov 14, 2024
1 parent 54c488c commit 8456b70
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-points-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@atproto/syntax": patch
---

Improve performance of isValidTid
16 changes: 5 additions & 11 deletions packages/syntax/src/tid.ts
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 {}

0 comments on commit 8456b70

Please sign in to comment.