Skip to content

fromBase64

Decodes Base64 text. Both alphabets are accepted (+/ and the URL-safe -_) and the = padding is optional, so it reads whatever toBase64 wrote, and most Base64 found in the wild.

Available since v3.1.8

import { fromBase64 } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { fromBase64 } from 'helpers4/string';
fromBase64(text: string, options: FromBase64Options & object): Uint8Array
ParameterTypeDescription
textstringThe Base64 text, without whitespace
optionsFromBase64Options & object`{ output: ‘bytes’ }` to get a `Uint8Array` instead of a string

Uint8Array — The decoded text (UTF-8), or the bytes

Either alphabet is accepted and the padding is optional.

fromBase64('aMOpbGxv')
// => 'héllo'

Use output: bytes when the payload is not text.

fromBase64('-_8', { output: 'bytes' })
// => Uint8Array [251, 255]

Options for fromBase64.

interface FromBase64Options {
  output?: "string" | "bytes";
}

View source on GitHub