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 🙏

© 2026 – Pkg Stats / Ryan Hefner

custom-encoder

v0.0.2

Published

Custom encoder from integers to any custom base and viceversa

Readme

Custom Encoder

Encoder from integers to any custom base and vice-versa

Installation

npm install --save custom-encoder

Usage

The package exports a function that generates the encoder. you can simply call the generator to store the encoder itself --as in example 1--, or you can store the function and call it each time you need a new encoder --as in example 2--.

// Require the package
const encoder = require('custom-encoder')();

// Set a new base
encoder.setBase(myCustomBase);

// Encode an integer to the current base
let myIntEncoded = encoder.encode(myInt);

// Decode from a String according to the current base
let myDecodedInt = encoder.decode(myIntEncoded);

// Get the current base as a string
let myBase = encoder.getBase();

You can create more than one encoder if you need to use more than one different base at the same time.

const encoderGenerator = require('custom-encoder');
const encoder1 = encoderGenerator();
const encoder2 = encoderGenerator();

encoder1.setBase(2);
encoder2.setBase('ab');

console.log(encoder1.encode(10)); // prints: '1010'
console.log(encoder2.encode(10)); // prints: 'baba'

API

The API is very simple. There are four methods:

encoder.setBase()

Set the base that will be used by functions encode and decode.

Syntax
encoder.setBase(base)
Params

base
It can be either one of the numbers 2, 8, 10, 16, or a string. If the value given is a number, it sets the base to the following symbols:

  • 2: 01
  • 8: 01234567
  • 10: 0123456789
  • 16: 0123456789abcdef

If the value given is a string, it must contain the symbols of the base ordered from less value to more value.

Throws

It trows the following errors:

  • TypeError: If base is not 2, 8, 10 or 16, it must be a string.
  • Error: All symbols in the base must be unique.
  • Error: The base must have at least two different valid symbols.
Example
// Set the base to binary
encoder.setBase(2);

// Set the base to hexadecimal
encoder.setBase(16);

// Set a custom base where the symbol 'b' is one unit bigger than 'a', and
// symbol '1' is one unit bigger than 'd'
encoder.setBase('abcd1234') 

encoder.encode()

Encodes the given integer according to the current base.

Syntax
encoder.encode(intNumber)
Params

intNumber
Is the number given to be encoded. It must be an integer, or the function throws an error.

Returns

It returns a string with the encoded value of intNumber according to the current base.

Throws

It trows the following error:

  • TypeError: The given number intNumber must be an integer.
Example
// Set the base to binary
encoder.setBase(2);
encoder.encode(10); // result: '1010'

// Set the base to hexadecimal
encoder.setBase(16);
encoder.encode(195951310); // result: 'badface'

// Set a custom base
encoder.setBase('myfriend');
encoder.encode(481); // result: 'diy'

encoder.decode()

Decodes the given string according to the current base.

Syntax
encoder.decode(codedString)
Params

codedString
Is the string given to be decoded. The user is the responsible to check that the base used to encode is the same being used to decode.

Returns

It returns the integer corresponding to the given encoded string according to the current base.

Throws

It trows the following errors:

  • TypeError: The input must be a string.
  • Error: The input contains symbols that are not in the base.
Example
// Set the base to binary
encoder.setBase(2);
encoder.decode('1010'); // result: 10

// Set the base to hexadecimal
encoder.setBase(16);
encoder.encode('badface'); // result: 195951310

// Set a custom base
encoder.setBase('myfriend');
encoder.encode('diy'); // result: 481

encoder.getCurrentBase()

Returns the current base as a string of the symbols used.

Syntax
encoder.getCurrentBase()
Returns

A string with the symbols in the current base, ordered from smaller to bigger value in the base.

Example
// Set the base to binary
encoder.setBase(2);
encoder.getCurrentBase(); // result: '01'

// Set the base to hexadecimal
encoder.setBase(16);
encoder.getCurrentBase(); // result: '0123456789abcdef'

// Set a custom base
encoder.setBase('myfriend');
encoder.getCurrentBase(); // result: 'myfriend'