Skip to content

Base64 API

encodeBase64Bytes Encode

Encodes arbitrary bytes as standard Base64.

Signature

ts
export function encodeBase64Bytes(bytes: Uint8Array): string;

Example

ts
import { encodeBase64Bytes } from "@fast-china/utils";

const result = encodeBase64Bytes(new Uint8Array([70, 97, 115, 116]));
console.log(result); // "RmFzdA=="

Input

InputTypeRequired / defaultDescription
bytesUint8ArrayRequiredByte sequence; not mutated.

Returns

ValueTypeDescription
resultstringBase64 text with standard = padding.

decodeBase64Bytes Decode

Decodes standard Base64.

Signature

ts
export function decodeBase64Bytes(value: string): Uint8Array;

Example

ts
import { decodeBase64Bytes } from "@fast-china/utils";

const result = decodeBase64Bytes("RmFzdA==");

Input

InputTypeRequired / defaultDescription
valuestringRequiredBase64 text.

Returns

ValueTypeDescription
resultUint8ArrayNew byte array.

encodeBase64 Encode

Encodes UTF-8 text as standard Base64.

Signature

ts
export function encodeBase64(value: string): string;

Example

ts
import { encodeBase64 } from "@fast-china/utils";

const result = encodeBase64("Fast 文档");

Input

InputTypeRequired / defaultDescription
valuestringRequiredAny Unicode string.

Returns

ValueTypeDescription
resultstringBase64 text with standard padding.

decodeBase64 Decode

Decodes standard Base64 into UTF-8 text.

Signature

ts
export function decodeBase64(value: string): DecodedText;

Example

ts
import { decodeBase64 } from "@fast-china/utils";

const result = decodeBase64("RmFzdA==");

Input

InputTypeRequired / defaultDescription
valuestringRequiredBase64 text.

Returns

ValueTypeDescription
resultDecodedTextRaw Unicode string usable directly or through explicit .parseJson<Value>().

encodeBase64UrlBytes Encode

Encodes arbitrary bytes as unpadded Base64URL.

Signature

ts
export function encodeBase64UrlBytes(bytes: Uint8Array): string;

Example

ts
import { encodeBase64UrlBytes } from "@fast-china/utils";

const result = encodeBase64UrlBytes(new Uint8Array([70, 97, 115, 116]));

Input

InputTypeRequired / defaultDescription
bytesUint8ArrayRequiredByte sequence; not mutated.

Returns

ValueTypeDescription
resultstringText using only the URL-safe alphabet.

decodeBase64UrlBytes Decode

Decodes Base64URL bytes.

Signature

ts
export function decodeBase64UrlBytes(value: string): Uint8Array;

Example

ts
import { decodeBase64UrlBytes } from "@fast-china/utils";

const result = decodeBase64UrlBytes("RmFzdA");

Input

InputTypeRequired / defaultDescription
valuestringRequiredBase64URL text.

Returns

ValueTypeDescription
resultUint8ArrayNew byte array.

encodeBase64Url Encode

Encodes UTF-8 text as unpadded Base64URL.

Signature

ts
export function encodeBase64Url(value: string): string;

Example

ts
import { encodeBase64Url } from "@fast-china/utils";

const result = encodeBase64Url("Fast 文档");

Input

InputTypeRequired / defaultDescription
valuestringRequiredAny Unicode string.

Returns

ValueTypeDescription
resultstringText using only the URL-safe alphabet.

decodeBase64Url Decode

Decodes Base64URL into UTF-8 text.

Signature

ts
export function decodeBase64Url(value: string): DecodedText;

Example

ts
import { decodeBase64Url } from "@fast-china/utils";

const result = decodeBase64Url("RmFzdA");

Input

InputTypeRequired / defaultDescription
valuestringRequiredPadded or unpadded Base64URL text.

Returns

ValueTypeDescription
resultDecodedTextRaw Unicode string usable directly or through explicit .parseJson<Value>().

encodeLatin1Base64 Encode

Encodes Latin-1 text as standard Base64.

Signature

ts
export function encodeLatin1Base64(value: string): string;

Example

ts
import { encodeLatin1Base64 } from "@fast-china/utils";

const result = encodeLatin1Base64("Fast");

Input

InputTypeRequired / defaultDescription
valuestringRequiredText whose UTF-16 code units are all between 0 and 255.

Returns

ValueTypeDescription
resultstringBase64 text with standard padding.

decodeLatin1Base64 Decode

Decodes standard Base64 into Latin-1 text.

Signature

ts
export function decodeLatin1Base64(value: string): DecodedText;

Example

ts
import { decodeLatin1Base64 } from "@fast-china/utils";

const result = decodeLatin1Base64("RmFzdA==");

Input

InputTypeRequired / defaultDescription
valuestringRequiredStandard Base64 text; ASCII whitespace and omitted trailing padding are accepted.

Returns

ValueTypeDescription
resultDecodedTextRaw Latin-1 string usable directly or through explicit .parseJson<Value>().

encodeSecureBase64 Encode

Encodes text with a fixed dictionary and random prefix.

Signature

ts
export function encodeSecureBase64(value: string, prefixLength: number = defaultRandomPrefixLength): string;

Example

ts
import { encodeSecureBase64 } from "@fast-china/utils";

const result = encodeSecureBase64("Fast");

Input

InputTypeRequired / defaultDescription
valuestringRequiredUnicode text accepted by encodeURIComponent.
prefixLengthnumberOptional; defaults to defaultRandomPrefixLengthRandom alphabetic prefix length; defaults to 6.

Returns

ValueTypeDescription
resultstringBase64 text with a random prefix and compatibility dictionary characters; empty input returns an empty string.

decodeSecureBase64 Decode

Decodes the SecureBase64 compatibility format.

Signature

ts
export function decodeSecureBase64(value: string, prefixLength: number = defaultRandomPrefixLength): DecodedText;

Example

ts
import { decodeSecureBase64, encodeSecureBase64 } from "@fast-china/utils";

const payload = encodeSecureBase64("Fast");
const result = decodeSecureBase64(payload);

Input

InputTypeRequired / defaultDescription
valuestringRequiredSecureBase64 text; use the same prefix length as encoding.
prefixLengthnumberOptional; defaults to defaultRandomPrefixLengthPrefix length to remove; defaults to 6. A value of 0 removes no dictionary characters.

Returns

ValueTypeDescription
resultDecodedTextRaw Unicode string usable directly or through explicit .parseJson<Value>(); empty input returns an empty string.

DecodedText.parseJson Parse

Explicitly attempts JSON parsing of a decoded or decrypted raw string.

Signature

ts
parseJson<Value = any>(): Value;

Example

ts
import { decodeBase64 } from "@fast-china/utils";

const text = decodeBase64("eyJpZCI6MX0=");
const result = text.parseJson<{ id: number }>();

Input

This method has no input parameters.

Returns

ValueTypeDescription
resultValueParsed result for valid JSON; otherwise the original string. The generic type performs no runtime validation.