mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 09:08:03 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base32, throwing exceptions if not
|
||||
*/
|
||||
export declare const base32Validate: (value?: unknown, ipfsCompat?: boolean) => value is string;
|
||||
/**
|
||||
* @name isBase32
|
||||
* @description Checks if the input is in base32, returning true/false
|
||||
*/
|
||||
export declare const isBase32: (value?: unknown, ipfsCompat?: boolean) => value is string;
|
||||
/**
|
||||
* @name base32Decode
|
||||
* @summary Delookup a base32 value.
|
||||
* @description
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export declare const base32Decode: (value: string, ipfsCompat?: boolean) => Uint8Array;
|
||||
/**
|
||||
* @name base32Encode
|
||||
* @summary Creates a base32 value.
|
||||
* @description
|
||||
* From the provided input, create the base32 and return the result as a string.
|
||||
*/
|
||||
export declare const base32Encode: (value: import("@pezkuwi/util/types").U8aLike, ipfsCompat?: boolean) => string;
|
||||
@@ -0,0 +1,40 @@
|
||||
import { utils } from '@scure/base';
|
||||
import { createDecode, createEncode, createIs, createValidate } from './helpers.js';
|
||||
const chars = 'abcdefghijklmnopqrstuvwxyz234567';
|
||||
const config = {
|
||||
chars,
|
||||
coder: utils.chain(
|
||||
// We define our own chain, the default base32 has padding
|
||||
utils.radix2(5), utils.alphabet(chars), {
|
||||
decode: (input) => input.split(''),
|
||||
encode: (input) => input.join('')
|
||||
}),
|
||||
ipfs: 'b',
|
||||
type: 'base32'
|
||||
};
|
||||
/**
|
||||
* @name base32Validate
|
||||
* @summary Validates a base32 value.
|
||||
* @description
|
||||
* Validates that the supplied value is valid base32, throwing exceptions if not
|
||||
*/
|
||||
export const base32Validate = /*#__PURE__*/ createValidate(config);
|
||||
/**
|
||||
* @name isBase32
|
||||
* @description Checks if the input is in base32, returning true/false
|
||||
*/
|
||||
export const isBase32 = /*#__PURE__*/ createIs(base32Validate);
|
||||
/**
|
||||
* @name base32Decode
|
||||
* @summary Delookup a base32 value.
|
||||
* @description
|
||||
* From the provided input, decode the base32 and return the result as an `Uint8Array`.
|
||||
*/
|
||||
export const base32Decode = /*#__PURE__*/ createDecode(config, base32Validate);
|
||||
/**
|
||||
* @name base32Encode
|
||||
* @summary Creates a base32 value.
|
||||
* @description
|
||||
* From the provided input, create the base32 and return the result as a string.
|
||||
*/
|
||||
export const base32Encode = /*#__PURE__*/ createEncode(config);
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import type { U8aLike } from '@pezkuwi/util/types';
|
||||
export type { U8aLike } from '@pezkuwi/util/types';
|
||||
interface Coder {
|
||||
decode: (value: string) => Uint8Array;
|
||||
encode: (value: Uint8Array) => string;
|
||||
}
|
||||
interface Config {
|
||||
chars: string;
|
||||
coder: Coder;
|
||||
ipfs?: string;
|
||||
regex?: RegExp;
|
||||
type: string;
|
||||
withPadding?: boolean;
|
||||
}
|
||||
type DecodeFn = (value: string, ipfsCompat?: boolean) => Uint8Array;
|
||||
type EncodeFn = (value: U8aLike, ipfsCompat?: boolean) => string;
|
||||
type ValidateFn = (value?: unknown, ipfsCompat?: boolean) => value is string;
|
||||
/** @internal */
|
||||
export declare function createDecode({ coder, ipfs }: Config, validate: ValidateFn): DecodeFn;
|
||||
/** @internal */
|
||||
export declare function createEncode({ coder, ipfs }: Config): EncodeFn;
|
||||
/** @internal */
|
||||
export declare function createIs(validate: ValidateFn): ValidateFn;
|
||||
/** @internal */
|
||||
export declare function createValidate({ chars, ipfs, type, withPadding }: Config): ValidateFn;
|
||||
@@ -0,0 +1,61 @@
|
||||
import { u8aToU8a } from '@pezkuwi/util';
|
||||
/** @internal */
|
||||
export function createDecode({ coder, ipfs }, validate) {
|
||||
return (value, ipfsCompat) => {
|
||||
validate(value, ipfsCompat);
|
||||
return coder.decode(ipfs && ipfsCompat
|
||||
? value.substring(1)
|
||||
: value);
|
||||
};
|
||||
}
|
||||
/** @internal */
|
||||
export function createEncode({ coder, ipfs }) {
|
||||
return (value, ipfsCompat) => {
|
||||
const out = coder.encode(u8aToU8a(value));
|
||||
return ipfs && ipfsCompat
|
||||
? `${ipfs}${out}`
|
||||
: out;
|
||||
};
|
||||
}
|
||||
/** @internal */
|
||||
export function createIs(validate) {
|
||||
return (value, ipfsCompat) => {
|
||||
try {
|
||||
return validate(value, ipfsCompat);
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
/** @internal */
|
||||
export function createValidate({ chars, ipfs, type, withPadding }) {
|
||||
return (value, ipfsCompat) => {
|
||||
if (typeof value !== 'string') {
|
||||
throw new Error(`Expected ${type} string input`);
|
||||
}
|
||||
else if (ipfs && ipfsCompat && !value.startsWith(ipfs)) {
|
||||
throw new Error(`Expected ipfs-compatible ${type} to start with '${ipfs}'`);
|
||||
}
|
||||
for (let i = (ipfsCompat ? 1 : 0), count = value.length; i < count; i++) {
|
||||
if (chars.includes(value[i])) {
|
||||
// all ok, character found
|
||||
}
|
||||
else if (withPadding && value[i] === '=') {
|
||||
if (i === count - 1) {
|
||||
// last character, everything ok
|
||||
}
|
||||
else if (value[i + 1] === '=') {
|
||||
// next one is also padding, sequence ok
|
||||
}
|
||||
else {
|
||||
throw new Error(`Invalid ${type} padding sequence "${value[i]}${value[i + 1]}" at index ${i}`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw new Error(`Invalid ${type} character "${value[i]}" (0x${value.charCodeAt(i).toString(16)}) at index ${i}`);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* @summary Encode and decode base32 values
|
||||
*/
|
||||
export { base32Decode, base32Encode, base32Validate, isBase32 } from './bs32.js';
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* @summary Encode and decode base32 values
|
||||
*/
|
||||
export { base32Decode, base32Encode, base32Validate, isBase32 } from './bs32.js';
|
||||
Reference in New Issue
Block a user