isPrincipalText
isPrincipalText(
value):boolean
Defined in: core/src/utils/helper.ts:426
Whether value is the text of a principal: a user, canister or the
anonymous principal, as a person pastes it into a form.
It is true exactly when value is the canonical text of a principal of
at most 29 bytes, the most the Internet Computer accepts: lowercase, grouped
by dashes, with a matching checksum and no whitespace, so trim what a person
typed first. Principal.fromText also reads the JSON form
{"__principal__":"aaaaa-aa"}, with whitespace around it; this refuses it,
so text that passes is the principal’s own text, fit to show, compare or put
in a URL. Use it where an input is validated, instead of a try around
Principal.fromText. A DisplayReactor then takes the text as it is; a
Reactor takes Principal.fromText(text).
It returns a boolean, not a type guard, so a string it refuses is still
typed a string afterwards.
Parameters
Section titled “Parameters”unknown
Anything; only a string can pass.
Returns
Section titled “Returns”boolean
true for the text of a principal.
Example
Section titled “Example”import { isPrincipalText } from "@ic-reactor/core"
isPrincipalText("ryjl3-tyaaa-aaaaa-aaaba-cai") // true: a canisterisPrincipalText("aaaaa-aa") // true: the management canisterisPrincipalText("2vxsx-fae") // true: the anonymous principalisPrincipalText("ryjl3-tyaaa") // false: the checksum does not matchisPrincipalText(" aaaaa-aa") // false: trim firstisPrincipalText("RYJL3-TYAAA-AAAAA-AAABA-CAI") // false: not canonicalisPrincipalText('{"__principal__":"aaaaa-aa"}') // false: JSON, not text