@dfinity/principal
Advanced tools
Comparing version 0.13.2 to 1.0.0-beta.0
@@ -1,2 +0,11 @@ | ||
export declare class Principal { | ||
/** | ||
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's | ||
*/ | ||
export declare abstract class PrincipalLike { | ||
abstract _isPrincipal: boolean; | ||
abstract toString(): string; | ||
abstract toText(): string; | ||
abstract toUint8Array(): Uint8Array; | ||
} | ||
export declare class Principal implements PrincipalLike { | ||
private _arr; | ||
@@ -14,2 +23,14 @@ static anonymous(): Principal; | ||
static fromUint8Array(arr: Uint8Array): Principal; | ||
/** | ||
* Utility to check if a provided value is a Principal | ||
* @param maybePrincipal unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipal(maybePrincipal: unknown): maybePrincipal is Principal; | ||
/** | ||
* Utility to check if a provided value is a Principal-encoded string | ||
* @param maybeValid unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipalString(maybeValid: unknown): boolean; | ||
readonly _isPrincipal = true; | ||
@@ -27,3 +48,4 @@ protected constructor(_arr: Uint8Array); | ||
*/ | ||
compareTo(other: Principal): 'lt' | 'eq' | 'gt'; | ||
compareTo(other: PrincipalLike): 'lt' | 'eq' | 'gt'; | ||
static compare(p1: PrincipalLike, p2: PrincipalLike): 'lt' | 'eq' | 'gt'; | ||
/** | ||
@@ -40,3 +62,3 @@ * Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method | ||
*/ | ||
gtEq(other: Principal): boolean; | ||
gtEq(other: PrincipalLike): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Principal = void 0; | ||
exports.Principal = exports.PrincipalLike = void 0; | ||
const base32_1 = require("./utils/base32"); | ||
@@ -12,2 +12,8 @@ const getCrc_1 = require("./utils/getCrc"); | ||
const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); | ||
/** | ||
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's | ||
*/ | ||
class PrincipalLike { | ||
} | ||
exports.PrincipalLike = PrincipalLike; | ||
class Principal { | ||
@@ -36,6 +42,4 @@ constructor(_arr) { | ||
} | ||
else if (typeof other === 'object' && | ||
other !== null && | ||
other._isPrincipal === true) { | ||
return new Principal(other._arr); | ||
else if (Principal.isPrincipal(other)) { | ||
return new Principal(other.toUint8Array()); | ||
} | ||
@@ -60,2 +64,38 @@ throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`); | ||
} | ||
/** | ||
* Utility to check if a provided value is a Principal | ||
* @param maybePrincipal unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipal(maybePrincipal) { | ||
if (maybePrincipal instanceof Principal) | ||
return true; | ||
if (typeof maybePrincipal === 'object' && maybePrincipal !== null) { | ||
if ('toText' in maybePrincipal && | ||
'toString' in maybePrincipal && | ||
'toUint8Array' in maybePrincipal && | ||
'_isPrincipal' in maybePrincipal) { | ||
return maybePrincipal['_isPrincipal']; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Utility to check if a provided value is a Principal-encoded string | ||
* @param maybeValid unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipalString(maybeValid) { | ||
if (typeof maybeValid === 'string') { | ||
try { | ||
Principal.fromText(maybeValid); | ||
return true; | ||
} | ||
catch (_) { | ||
// errors are expected and deliberately suppressed | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
isAnonymous() { | ||
@@ -94,12 +134,17 @@ return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX; | ||
compareTo(other) { | ||
for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) { | ||
if (this._arr[i] < other._arr[i]) | ||
return Principal.compare(this, other); | ||
} | ||
static compare(p1, p2) { | ||
const p1Arr = p1.toUint8Array(); | ||
const p2Arr = p2.toUint8Array(); | ||
for (let i = 0; i < Math.min(p1Arr.length, p2Arr.length); i++) { | ||
if (p1Arr[i] < p2Arr[i]) | ||
return 'lt'; | ||
else if (this._arr[i] > other._arr[i]) | ||
else if (p1Arr[i] > p2Arr[i]) | ||
return 'gt'; | ||
} | ||
// Here, at least one principal is a prefix of the other principal (they could be the same) | ||
if (this._arr.length < other._arr.length) | ||
// Here, at least one principal is a prefix of the p2 principal (they could be the same) | ||
if (p1Arr.length < p2Arr.length) | ||
return 'lt'; | ||
if (this._arr.length > other._arr.length) | ||
if (p1Arr.length > p2Arr.length) | ||
return 'gt'; | ||
@@ -106,0 +151,0 @@ return 'eq'; |
@@ -1,2 +0,11 @@ | ||
export declare class Principal { | ||
/** | ||
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's | ||
*/ | ||
export declare abstract class PrincipalLike { | ||
abstract _isPrincipal: boolean; | ||
abstract toString(): string; | ||
abstract toText(): string; | ||
abstract toUint8Array(): Uint8Array; | ||
} | ||
export declare class Principal implements PrincipalLike { | ||
private _arr; | ||
@@ -14,2 +23,14 @@ static anonymous(): Principal; | ||
static fromUint8Array(arr: Uint8Array): Principal; | ||
/** | ||
* Utility to check if a provided value is a Principal | ||
* @param maybePrincipal unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipal(maybePrincipal: unknown): maybePrincipal is Principal; | ||
/** | ||
* Utility to check if a provided value is a Principal-encoded string | ||
* @param maybeValid unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipalString(maybeValid: unknown): boolean; | ||
readonly _isPrincipal = true; | ||
@@ -27,3 +48,4 @@ protected constructor(_arr: Uint8Array); | ||
*/ | ||
compareTo(other: Principal): 'lt' | 'eq' | 'gt'; | ||
compareTo(other: PrincipalLike): 'lt' | 'eq' | 'gt'; | ||
static compare(p1: PrincipalLike, p2: PrincipalLike): 'lt' | 'eq' | 'gt'; | ||
/** | ||
@@ -40,3 +62,3 @@ * Utility method checking whether a provided Principal is less than or equal to the current one using the {@link Principal.compareTo} method | ||
*/ | ||
gtEq(other: Principal): boolean; | ||
gtEq(other: PrincipalLike): boolean; | ||
} |
@@ -9,2 +9,7 @@ import { decode, encode } from './utils/base32'; | ||
const toHexString = (bytes) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); | ||
/** | ||
* Base class for a Principal. Use this as the signature to ensure compatibility in your API's | ||
*/ | ||
export class PrincipalLike { | ||
} | ||
export class Principal { | ||
@@ -33,6 +38,4 @@ constructor(_arr) { | ||
} | ||
else if (typeof other === 'object' && | ||
other !== null && | ||
other._isPrincipal === true) { | ||
return new Principal(other._arr); | ||
else if (Principal.isPrincipal(other)) { | ||
return new Principal(other.toUint8Array()); | ||
} | ||
@@ -57,2 +60,38 @@ throw new Error(`Impossible to convert ${JSON.stringify(other)} to Principal.`); | ||
} | ||
/** | ||
* Utility to check if a provided value is a Principal | ||
* @param maybePrincipal unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipal(maybePrincipal) { | ||
if (maybePrincipal instanceof Principal) | ||
return true; | ||
if (typeof maybePrincipal === 'object' && maybePrincipal !== null) { | ||
if ('toText' in maybePrincipal && | ||
'toString' in maybePrincipal && | ||
'toUint8Array' in maybePrincipal && | ||
'_isPrincipal' in maybePrincipal) { | ||
return maybePrincipal['_isPrincipal']; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Utility to check if a provided value is a Principal-encoded string | ||
* @param maybeValid unknown type | ||
* @returns boolean | ||
*/ | ||
static isPrincipalString(maybeValid) { | ||
if (typeof maybeValid === 'string') { | ||
try { | ||
Principal.fromText(maybeValid); | ||
return true; | ||
} | ||
catch (_) { | ||
// errors are expected and deliberately suppressed | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
isAnonymous() { | ||
@@ -91,12 +130,17 @@ return this._arr.byteLength === 1 && this._arr[0] === ANONYMOUS_SUFFIX; | ||
compareTo(other) { | ||
for (let i = 0; i < Math.min(this._arr.length, other._arr.length); i++) { | ||
if (this._arr[i] < other._arr[i]) | ||
return Principal.compare(this, other); | ||
} | ||
static compare(p1, p2) { | ||
const p1Arr = p1.toUint8Array(); | ||
const p2Arr = p2.toUint8Array(); | ||
for (let i = 0; i < Math.min(p1Arr.length, p2Arr.length); i++) { | ||
if (p1Arr[i] < p2Arr[i]) | ||
return 'lt'; | ||
else if (this._arr[i] > other._arr[i]) | ||
else if (p1Arr[i] > p2Arr[i]) | ||
return 'gt'; | ||
} | ||
// Here, at least one principal is a prefix of the other principal (they could be the same) | ||
if (this._arr.length < other._arr.length) | ||
// Here, at least one principal is a prefix of the p2 principal (they could be the same) | ||
if (p1Arr.length < p2Arr.length) | ||
return 'lt'; | ||
if (this._arr.length > other._arr.length) | ||
if (p1Arr.length > p2Arr.length) | ||
return 'gt'; | ||
@@ -103,0 +147,0 @@ return 'eq'; |
{ | ||
"name": "@dfinity/principal", | ||
"version": "0.13.2", | ||
"version": "1.0.0-beta.0", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -47,2 +47,3 @@ "license": "Apache-2.0", | ||
"devDependencies": { | ||
"@dfinity/principal-legacy": "npm:@dfinity/principal@^0.9.3", | ||
"@types/jest": "^28.1.4", | ||
@@ -49,0 +50,0 @@ "@typescript-eslint/eslint-plugin": "^5.30.5", |
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
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
149700
800
13