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

number-converter-alphabet

v1.1.0

Published

Convert a number to any base/any custom alphabet

Downloads

309

Readme

number-converter-alphabet Build Status

Convert a number to any base/any custom alphabet.

Why

When you need to convert a number to a custom base/alphabet, this comes in handy. For instance, for Excel style column names, you can count from 0 to n and get the representation of the number as a column name: "AZ" "BA" easily.

Install

$ npm install --save number-converter-alphabet

Usage

import convert, { ALPHABET_HEXADECIMAL, ALPHABET_BINARY } from 'number-converter-alphabet';

console.log(convert(3735928559, ALPHABET_HEXADECIMAL));
// Output: DEADBEEF
console.log(convert(3735928559, ALPHABET_BINARY))
// Output: 11011110101011011011111011101111
console.log(convert(3735928559, '0123'))
// Output: 3132223123323233

console.log(convert(35427, ['ping|', 'pong|', 'tok|']));
// Output: pong|tok|pong|ping|pong|tok|pong|ping|pong|ping|

Implicit leading zero

In our number world, ...00001 === 1, we don't think of the leading zeros. Same for hexadecimal : ...000FF === FF.

But sometimes, you want those to be different. For instance, in Excel, the column name are using the normal alphabet, and after "Z", there is "AA", not "BA".

You can pass an option to control this behavior: implicitLeadingZero (default: true).

import convert, { ALPHABET_ASCII, ALPHABET_DECIMAL } from 'number-converter-alphabet';

console.log(convert(26, ALPHABET_ASCII, { implicitLeadingZero: true }));
// Output: ba
console.log(convert(26, ALPHABET_ASCII, { implicitLeadingZero: false }));
// Output: aa

console.log(convert(26, ALPHABET_DECIMAL, { implicitLeadingZero: true }));
// Output: 26
console.log(convert(6, ALPHABET_DECIMAL, { implicitLeadingZero: false }));
// Output: 6
console.log(convert(16, ALPHABET_DECIMAL, { implicitLeadingZero: false }));
// Output: 06
console.log(convert(26, ALPHABET_DECIMAL, { implicitLeadingZero: false }));
// Output: 16

Generator

For commodity, there is a also a generator function that is available, to automatically increment the value it outputs :

import generator from 'number-converter-alphabet';

const gen = generator(ALPHABET_ASCII)
gen(); // 'a'
gen(); // 'b'

const gen = generator(ALPHABET_ASCII, { start: 26 });
gen(); // 'z'
gen(); // 'aa'