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

@cheprasov/base-converter

v1.0.0

Published

The library allows to convert a number between arbitrary bases

Downloads

32

Readme

MIT license

@cheprasov/base-converter

The library allows to convert a number between arbitrary bases.

Note

Before using the library please check native method Number.toString([radix]), maybe this is what you need. The library was written mostly for converting more complicated or custom bases.

Quick examples:


import { BaseConverter, baseBinary, baseHexadecimal } from '@cheprasov/base-converter';

// encode

console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 bin

console.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hex

console.log('255 dec =>', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');
// 255 dec => BAABBA custom

// decode

console.log('101 bin =>', BaseConverter.decode(101, baseBinary), 'dec');
// 101 bin => 5 dec

console.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 dec

console.log('BAA custom =>', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');
// BAA custom => 9 dec

// convert

console.log('1010 bin =>', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');
// 1010 bin => A hex

console.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 bin

console.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin

How to install

> npm install @cheprasov/base-converter

Documentation

Import

// import library
import { BaseConverter } from '@cheprasov/base-converter';

// import preset bases
import {
    baseBinary,
    baseOctal,
    baseDecimal,
    baseHexadecimal,
    base62
} from '@cheprasov/base-converter';

Custom base

A base has type Array<string|number>, it means that each base is array of integers or strings. Also, each element is the array should have 1 symbol only.

Examples:


// correct
const base4 = [0, 1, 2, 3];
const base8 = [0, 1, 2, 3, 'W', 'X', 'Y', 'Z'];
const baseSome = ['0', 'a', 'A', 'b', 'B'];
const baseSymbols = ['!', '@', '#', '$', '%', '^', '&', '*', '.', '-', '+', '='];

// wrong
const base4 = [0, 10, 20, 30]; // should be 1 symbols for each element
const base8 = [0, 1, 2, 3, 'AA', 'BB', 'CC', 'DD']; // should be 1 symbols for each element
const baseSome = ['0', '1', '1', '2', '2']; // duplicates of items
const baseSome = ['A']; // should be more then 1 element in the array

Public methods

encode(value: number, base: Array<string | number>): string | null

Converts decimal number to provided base. Will return null if value can not be converted.

Example:

console.log('7 dec =>', BaseConverter.encode(7, baseBinary), 'bin');
// 7 dec => 111 bin

console.log('255 dec =>', BaseConverter.encode(255, baseHexadecimal), 'hex');
// 255 dec => FF hex

console.log('255 dec =>', BaseConverter.encode(255, ['A', 'B', 'C']), 'custom');
// 255 dec => BAABBA custom

decode(value: string, base: Array<string | number>): number | null

Converts value from provided base to decimal number. Will return null if value can not be converted.

Example:

console.log('101 bin =>', BaseConverter.decode(101, baseBinary), 'dec');
// 101 bin => 5 dec

console.log('FA hex =>', BaseConverter.decode('FA', baseHexadecimal), 'dec');
// FA hex => 250 dec

console.log('BAA custom =>', BaseConverter.decode('BAA', ['A', 'B', 'C']), 'dec');
// BAA custom => 9 dec

convert(value: string | number, fromBase: BaseType, toBase: BaseType): string | null

Converts value from one base to another base. Will return null if value can not be converted.

console.log('1010 bin =>', BaseConverter.convert(1010, baseBinary, baseHexadecimal), 'hex');
// 1010 bin => A hex

console.log('FF hex =>', BaseConverter.convert('FF', baseHexadecimal, baseBinary), 'bin');
// FF hex => 11111111 bin

console.log('FF custom =>', BaseConverter.convert('FF', ['A', 'B', 'C', 'D', 'E', 'F'], ['1', '2', '3']), 'custom');
// FF custom => 2133 bin

Something does not work

Feel free to fork project, fix bugs, tests and finally request for pull