npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

usebase

v3.0.18

Published

Encode Integers into Strings and decode Strings into Integers.

Downloads

21

Readme

UseBase:Encode and Decode Numbers

npm v9.6.7 Unpacked Size: 17 kB Github Talk with me

Table of Content

Features

  • Encode Integers using a base.
  • Suport to encode and decode.
  • Flexible support for both strings and arrays as bases.
  • Support for various bases, including binary, octal, hexadecimal, and custom character sets.

Support

ES6 Module Node.js

About

UseBase is a versatile JavaScript library designed to simplify the process of encoding and decoding numbers using various numeral bases. Whether you need to represent integers in binary, hexadecimal, custom character sets, or even emojis.

About it's usage, useBase is a function that recieve a param base (optional), a string or array containing the base to be used, the default value is: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.

useBase returns a object with two functions encode and decode, and a read-only value base, containing the base used to create this instance.

  • encode recieve two params, integer the number to be encoded and auxArray (optional) the array that the function uses as auxiliary array to store the values when the base is a array (don't pass any value if you dont know what you are doing). It returns a string or array of the integer encoded, the returned value have two proto properties:

    • raw property containing the integer param used to encode this value.
    • base property containing the base used to create this useBase instance.
  • decode recieve one param, value a encoded string or array. It returns a decode number, the returned value have a proto value:

    • base property containing the base used to create this useBase instance.

Installing

Install the package using npm:

npm install usebase

Once the package is installed, you can import useBase to your project:

import useBase from "usebase";

useBase().encode(42); // returns "Q"

If you are using CommomJS, there are some ways to import the module, one of them is using the import async function:

import("usebase").then(({ default: useBase }) => {
    useBase().encode(42); // returns "Q"
});

// or in your async function
const { default: useBase } = await import("usebase");

useBase().encode(42); // returns "Q"

CDN

Using jsDelivr CDN (browser module):


<script src="https://cdn.jsdelivr.net/npm/usebase@latest/index.js"></script>

Using unpkg CDN:


<script src="https://unpkg.com/usebase@latest/index.js"></script>

Example

There is a simple example of usage:

useBase().encode(42); // returns "Q"

useBase().decode("Q"); // returns 42

Using the Proto Properties

The useBase function have some proto properties containing some built-in bases:

useBase.base2; /* or */ useBase.binary;

useBase.base5; /* or */ useBase.quinary;

useBase.base8; /* or */ useBase.octal;

useBase.base12; /* or */ useBase.duodecimal;

useBase.base16; /* or */ useBase.hexadecimal;

useBase.base32;

useBase.base34;

useBase.base62;

useBase.base64;

// usage example:
useBase.binary.encode(42); // returns "101010"

useBase.octal.decode("52"); // returns 42

Using the raw proto property from the enconde returned value, you can easily translate the value from a base to another:

let valueInBinary = useBase.binary.encode(42); // "101010"

let valueInOctal = useBase.octal.encode(valueInBinary.raw); // "52"

Custom Bases

Using custom bases:

// using a custom base2 (binary)
useBase("01").encode(42); // returns "101010" 

// using a custom base8 (octal)
useBase("01234567").decode("52"); // returns 42

// using a custom base
useBase("MYBASE").encode(42); // returns "YYM"

Multiple Instances

You can also save multiple instances and use them as you want:

import useBase from "usebase";
const { base2, base8, base12, base16, base32, base34, base62, base64 } = useBase;

// or with custom bases

const base2 = useBase("01"); // binary

const base8 = useBase("01234567"); // octal

const base10 = useBase("0123456789"); // decimal

const base16 = useBase("0123456789ABCDEF"); // hexadecimal

const baseLetters = useBase("abcdefghijklmnopqrstuvwxyz"); // alphabet

const baseLettersAndUpperCase = useBase(); // alphabet and UpperCase alphabet

Base as Array

Using arrays:

let myArray = "The answer to life, the universe and everything is 42".split(" ");
const base42 = useBase(myArray); // base of hitchhiker's guide to the galaxy

let myEncode = base42.encode(42); // returns array (2) ['the', 'to']

let myDecode = base42.decode(myEncode); // returns 42

Special Characters

Some special character use more than one space per character inside a string so you can't use it as string nor split to use as an array. We need to split it using another method and pass this new array to useBase

// function to split special characters
const splitPlus = (string) => [...new Intl.Segmenter().segment(string)].map(x => x.segment);

let myArray = splitPlus("«»©®℗™×='‘’‚‛¬–÷—“”„‟⁄");
const baseSpecialChars = useBase(myArray); // Special characteres

baseSpecialChars.encode(42); // returns (2) ['»', '‟']

let myOtherArray = splitPlus("😀😁😂🤣😃😄😅😆😉😊😋😎😍😘🥰😗");
const base16AsEmoji = useBase(myOtherArray); // Hexademojinal 🤣

base16AsEmoji.encode(42); // returns array (2) ['😂', '😋']

Errors

List of error throws:

  • Thrown Error - If useBase base param is given but its not a string or array: "Param 'base' must be of type String or Array".
  • Thrown Error - If encode integer param is not given as a number: "Param 'number' must be of type Int".
  • Thrown Error - If encode auxArray param is given but it is not an array: "Param 'auxArray' must be an array".
  • Thrown Error - If decode value param is not given as a string or array: "Param 'value' must be of type String or Array".

List of possible errors and cautions you need to take:

  • Warn - If useBase base param includes the same value 2 or more times: The result of encoding and decoding may be incorrect.
  • Warn - If encode integer param is given as a negative number: It will return undefined as useBase does not support negative values.
  • Error - If encode integer param is given as a number bigger than 1e+308: RangeError: Maximum call stack size exceeded.
  • Warn - If decode value param is given as a string or array wich characters or values are not included in the base: The return number will be a mess and incorrect.
  • Warn - If decode value param is given as a string somehow is too complex or too long: The return number will be Infinity.
  • Warn - If encode auxArray param is given a value: If you don't understand what you are doing, it will probably not work as expected.
  • Warn - If any value is assigned to any of the proto properties: The proto properties are read-only.

Credits

Thanks to this stack overflow question i got the inspiration to create useBase.

I got the splitPlus function from rootEnginear in this stack overflow answer.

To make this README i used as reference Axio's README.

Go back to top ↑