Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

base-conv

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

base-conv - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

.vscode/settings.json

47

index.js
'use strict';
// inspired from https://codegolf.stackexchange.com/questions/1620/arbitrary-base-conversion/21672#21672
let CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

@@ -15,5 +13,20 @@ let CHARSET_MAP; // char => position map, to avoid calling indexOf

const convert = (numbers, src_base, dst_base) => {
// inspired from https://codegolf.stackexchange.com/questions/1620/arbitrary-base-conversion/21672#21672
/**
* Convert a string or number from srcRadix to dstRadix
* @param {String | Number} str
* @param {Number} srcBase
* @param {Number} dstBase
* @param {Boolean} safe avoid throwing for unrecognized input chars, and just ignore
* @returns {String}
*/
const convert = (_str = '', src_base = 10, dst_base = CHARSET.length, safe) => {
if (dst_base > CHARSET.length) throw new Error(`src or dst radix exceeds current charset length (${CHARSET.length})`);
const res = [];
let nums = numbers;
const str = _str + '';
const s = src_base <= 36 ? str.toLowerCase() : str;
let nums = Array.from(s, x => CHARSET_MAP.get(x));
while (nums.length) {

@@ -24,2 +37,6 @@ // divide successive powers of dst_base

for (var i = 0; i < nums.length; i++) {
if (nums[i] >= src_base) {
if (safe) continue;
throw new Error(`digit "${s[i]}" is unknown for base=${src_base}`);
}
var accumulator = nums[i] + remainder * src_base;

@@ -38,19 +55,3 @@ const digit = (accumulator / dst_base) | 0; // rounding faster than Math.floor

return res.reverse();
};
/**
* Convert a number from srcRadix to dstRadix
* note: we don't throw for an unrecognized char
* @param {String | Number} _str
* @param {Number} srcBase
* @param {Number} dstBase
* @returns {String}
*/
const conv = (_str = '', srcBase = 10, dstBase = CHARSET.length) => {
if (srcBase > CHARSET.length || dstBase > CHARSET.length) throw new Error(`src or dst radix exceeds current charset length (${CHARSET.length})`);
const str = _str + '';
const s = srcBase <= 36 ? str.toLowerCase() : str;
const num = Array.from(s, x => CHARSET_MAP.get(x));
return convert(num, srcBase, dstBase)
return res.reverse()
.map(x => CHARSET[x])

@@ -60,4 +61,4 @@ .join('');

conv.setCharset = setCharset; // rollup doesn't like mixed named and default exports for cjs
convert.setCharset = setCharset;
module.exports = conv;
module.exports = convert;
{
"name": "base-conv",
"version": "1.1.0",
"version": "1.2.0",
"description": "Convert arbitrarily big numbers from any radix representation to any other",

@@ -5,0 +5,0 @@ "main": "index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc