Skip to content

Commit

Permalink
Reapply "revert: changes on identifier (#11441)" (#11661)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works authored Jun 13, 2024
1 parent 3b25bf9 commit a87a967
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions packages/base/src/Identifier/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ export class ECKeyIdentifier extends Identifier {
)
}
declare [Symbol.toStringTag]: string
#ec: undefined
static [Symbol.hasInstance](x: any): boolean {
return toText(x)?.startsWith('ec_key:') ?? false
return typeof x === 'object' && x !== null && #ec in x
}
static {
ECKeyIdentifier.prototype[Symbol.toStringTag] = 'ECKeyIdentifier'
Expand Down Expand Up @@ -188,8 +189,9 @@ export class PostIVIdentifier extends Identifier {
return new Uint8Array(decodeArrayBuffer(x))
}
declare [Symbol.toStringTag]: string
#post_iv: undefined
static [Symbol.hasInstance](x: any): boolean {
return toText(x)?.startsWith('post_iv:') ?? false
return typeof x === 'object' && x !== null && #post_iv in x
}
static {
PostIVIdentifier.prototype[Symbol.toStringTag] = 'PostIVIdentifier'
Expand Down Expand Up @@ -237,8 +239,9 @@ export class PostIdentifier extends Identifier {
return this.postID
}
declare [Symbol.toStringTag]: string
#post: undefined
static [Symbol.hasInstance](x: any): boolean {
return toText(x)?.startsWith('post:') ?? false
return typeof x === 'object' && x !== null && #post in x
}
static {
PostIdentifier.prototype[Symbol.toStringTag] = 'PostIdentifier'
Expand Down Expand Up @@ -300,8 +303,9 @@ export class ProfileIdentifier extends Identifier {
return `person:${this.network}/${this.userId}`
}
declare [Symbol.toStringTag]: string
#profile: undefined
static [Symbol.hasInstance](x: any): boolean {
return toText(x)?.startsWith('person:') ?? false
return typeof x === 'object' && x !== null && #profile in x
}
static {
ProfileIdentifier.prototype[Symbol.toStringTag] = 'ProfileIdentifier'
Expand All @@ -321,13 +325,3 @@ function banSlash(input: string | undefined | null, source?: string) {
if (!input) return
if (input.includes('/')) throw new TypeError(`Cannot contain / in a part of identifier (${source}): ${input}`)
}

function toText(x: any) {
try {
const text = x.toText()
if (typeof text === 'string') return text
return
} catch {
return
}
}

0 comments on commit a87a967

Please sign in to comment.