mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
96da26cd73
* Added base32hex support. * Refactored. * Tried fixing documentation examples. * Marked examples as no-eval. * Trying to fix `task ok`. * Reverted accidental change. * Fixed JSDoc examples. * fix * adjustments * refactor: add `@experimental` JSDoc tag * work * clean up tests * fixes * fix * compat --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
// Copyright (c) 2014 Jameson Little. MIT License.
|
|
// This module is browser compatible.
|
|
|
|
/**
|
|
* Utilities for
|
|
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6 | base32}
|
|
* encoding and decoding.
|
|
*
|
|
* Modified from {@link https://github.com/beatgammit/base64-js}.
|
|
*
|
|
* ```ts
|
|
* import { encodeBase32, decodeBase32 } from "@std/encoding/base32";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* assertEquals(encodeBase32("foobar"), "MZXW6YTBOI======");
|
|
*
|
|
* assertEquals(
|
|
* decodeBase32("MZXW6YTBOI======"),
|
|
* new TextEncoder().encode("foobar")
|
|
* );
|
|
* ```
|
|
*
|
|
* @module
|
|
*/
|
|
import { decode, encode } from "./_base32_common.ts";
|
|
|
|
const lookup: string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".split("");
|
|
const revLookup: number[] = [];
|
|
lookup.forEach((c, i) => (revLookup[c.charCodeAt(0)] = i));
|
|
|
|
/**
|
|
* Decodes a base32-encoded string.
|
|
*
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
|
|
*
|
|
* @param b32 The base32-encoded string to decode.
|
|
* @returns The decoded data.
|
|
*
|
|
* @example Usage
|
|
* ```ts
|
|
* import { decodeBase32 } from "@std/encoding/base32";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* assertEquals(
|
|
* decodeBase32("GZRTMMDDGA======"),
|
|
* new TextEncoder().encode("6c60c0"),
|
|
* );
|
|
* ```
|
|
*/
|
|
export function decodeBase32(b32: string): Uint8Array {
|
|
return decode(b32, lookup);
|
|
}
|
|
|
|
/**
|
|
* Converts data into a base32-encoded string.
|
|
*
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-6}
|
|
*
|
|
* @param data The data to encode.
|
|
* @returns The base32-encoded string.
|
|
*
|
|
* @example Usage
|
|
* ```ts
|
|
* import { encodeBase32 } from "@std/encoding/base32";
|
|
* import { assertEquals } from "@std/assert";
|
|
*
|
|
* assertEquals(encodeBase32("6c60c0"), "GZRTMMDDGA======");
|
|
* ```
|
|
*/
|
|
export function encodeBase32(data: ArrayBuffer | Uint8Array | string): string {
|
|
return encode(data, lookup);
|
|
}
|