@kalin-setterberg/encoding
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

JS functions for working with encodings. Contains typings for TypeScript.

Encode Base64

All data are processed using Uint8Arrays. In a node-like context you may encode a Buffer but the return value will be a plain Uint8Array.

import { b64enc } from "@kalin-setterberg/encoding";
const useBase64url = true;
const data = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]);
/** @type {Uint8Array} */
const result = b64enc(data, useBase64url);

Encode UTF-8

Prefers the use of a globally defined TextEncoder following the WHATWG standard. If no TextEncoder is defined but there is a global Buffer, as in node, a Buffer will be used for encoding.

Falls back upon a custom implementation supporting up to 16 bit code points.

import { utf8enc } from "@kalin-setterberg/encoding";
const data = "hello world";
/** @type {Uint8Array} */
const result = utf8enc(data);

Decode UTF-8

Prefers the use of a globally defined TextDecoder following the WHATWG standard. If no TextDecoder is defined but there is a global Buffer, as in node, Buffer.toString() will be used for decoding when the parameter is an instance of Buffer.

Falls back upon a custom implementation supporting up to 16 bit code points.

import { utf8dec } from "@kalin-setterberg/encoding";
const data = new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]);
/** @type {string} */
const result = utf8dec(data);

Readme

Keywords

none

Package Sidebar

Install

npm i @kalin-setterberg/encoding

Weekly Downloads

0

Version

0.1.3

License

ISC

Unpacked Size

56.6 kB

Total Files

31

Last publish

Collaborators

  • lumphe
  • timluq