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

power-radix

v3.0.1

Published

Library for converting numbers from one radix representation (encoding) to another, with optional custom defined encodings. Inspired by rubyworks/radix.

Downloads

803

Readme

power-radix

Build Status npm version Code Climate codecov Dependency Status devDependency Status Standard - JavaScript Style Guide

Library for converting numbers from one radix representation (encoding) to another, with optional custom defined encodings. Inspired by rubyworks/radix.

power-radix-encodings is a useful collection of common encodings that are independently requireable.

Features

  • Convert to and from any base.
  • Define custom encoding and character sets.

Installing

$ npm install power-radix

Usage

Base conversions with ASCII ordered notations are easy in Javascript.

(255).toString(16) === 'ff'
parseInt('ff', 16) === 255

But JavaScript limits you to radix values 2 - 36.

(255).toString(37) // error

power-radix provides the means of converting to and from any base.
For example, a number in base 256 can be representated by the array [100, 10] (Math.pow(100, 256) + Math.pow(10, 1)) and can be converted to base 10.

// as an array
new PowerRadix([100, 10], 256).toArray(10); // ['2', '5', '6', '1', '0']

// or as a string
new PowerRadix([100, 10], 256).toString(10); // "25610"

power-radix also supports custom character encodings as base and target radixes. By default, power-radix uses the following character encoding:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

You can optionally specify an array of characters to use as symbols for a radix to give your output a custom encoding.

var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
new PowerRadix([1, 0], 10).toArray(base); // ['W', 'Q']
new PowerRadix('10', 10).toArray(base);   // ['W', 'Q']
new PowerRadix(10, 10).toArray(base);     // ['W', 'Q']

new PowerRadix([1, 0], 10).toString(base); // "WQ"
new PowerRadix('10', 10).toString(base);   // "WQ"
new PowerRadix(10, 10).toString(base);     // "WQ"

Or specify an array of characters to use as symbols for the base radix

var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
var stdBase10 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
new PowerRadix(['W', 'Q'], base).toArray(stdBase10);   // [1, 0]
new PowerRadix(['W', 'Q'], base.join('')).toArray(10); // [1, 0]
new PowerRadix(['W', 'Q'].join(''), base).toArray(stdBase10);   // [1, 0]
new PowerRadix(['W', 'Q'].join(''), base.join('')).toArray(10); // [1, 0]

new PowerRadix(['W', 'Q'], base).toString(stdBase10);   // "10"
new PowerRadix(['W', 'Q'], base.join('')).toString(10); // "10"
new PowerRadix(['W', 'Q'].join(), base).toString(stdBase10);   // "10"
new PowerRadix(['W', 'Q'].join(), base.join('')).toString(10); // "10"

Examples

// Node.js standard library crypto module
var crypto = require('crypto');

// Produce a SHA1 hash digest. SHA1 digests are 160 bits (20 bytes).
var sha1HashDigest = crypto.createHash('sha1').update('').digest('hex');
// "da39a3ee5e6b4b0d3255bfef95601890afd80709"

// When represented in radix 2 (binary), the representation is 160 characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base2-binary'));
// "1101101000111001101000111110111001011110011010110100101100001101001100100101010110111111111011111001010101100000000110001001000010101111110110000000011100001001"

// When represented in radix 10 (decimal), the representation is 49 (varies) characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base10-decimal'));
// "1245845410931227995499360226027473197403882391305"

// When represented in radix 16 (hexadecimal), the representation is 40 characters in length
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(require('power-radix-encodings/base16-hexadecimal-lowercase'));
// "da39a3ee5e6b4b0d3255bfef95601890afd80709"

var radix255CharacterEncoding = [];
for(var i = 0; i < 256; i++) { radix255CharacterEncoding.push(i+''); }

// When represented in radix 255, the representation is 20 characters in length (note:
// "characters" are actually decimal characters concatenated together)

new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(radix255CharacterEncoding);
// The decimal value of each of the 20 bytes of the sha1 hash digest
//  ['218', '57', '163', '238', '94', '107', '75', '13', '50', '85', '191', '239', '149', '96', '24', '144', '175', '216', '7', '9']

var radix255BinaryCharacterEncoding = [];
for(var i = 0; i < 256; i++) { radix255BinaryCharacterEncoding.push(new PowerRadix(i, 10).toString(2)); }
new PowerRadix(sha1HashDigest, require('power-radix-encodings/base16-hexadecimal-lowercase'))
  .toString(radix255BinaryCharacterEncoding);
// The binary representations of each of the 20 bytes of the sha1 hash digest (note: each byte representation doesn't have padding leading zeros)
// ['11011010', '111001', '10100011', '11101110', '1011110', '1101011', '1001011', '1101', '110010', '1010101', '10111111', '11101111', '10010101', '1100000', '11000', '10010000', '10101111', '11011000', '111', '1001']

Testing

// Tests + coverage reports are run using Lab
$ npm test
// Test coverage reports
npm run test-cov # will auto-open Google Chrome with html coverage report

Authors

License

power-radix is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.