New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 2.0.0 to 2.0.1

18

index.js

@@ -1,2 +0,2 @@

const DEFAULT_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
const DEFAULT_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';

@@ -7,10 +7,12 @@ // inspired from https://codegolf.stackexchange.com/questions/1620/arbitrary-base-conversion/21672#21672

* Convert a string or number from srcRadix to dstRadix
* @param {string?} alphabet
* @param {string} [inputAlphabet=DEFAULT_ALPHABET] alphabet of the input string
* @param {string} [outputAlphabet=DEFAULT_ALPHABET] alphabet to buse for the output string
* @returns {(input: string | number, inputBase?: number, outputBase?: number) => string}
*/
function BaseConv(charset = DEFAULT_CHARSET) {
const charsetMap = new Map(Array.from(charset, (s, i) => [s, i])); // char => position map, to avoid calling a O(n) indexOf
function BaseConv(inputAlphabet = DEFAULT_ALPHABET, outputAlphabet = inputAlphabet) {
// char => position map, to avoid calling a O(n) indexOf
const inputAlphabetMap = new Map(Array.from(inputAlphabet, (s, i) => [s, i]));
return (_str = '', inputBase = 10, outputBase = charset.length) => {
if (outputBase > charset.length) throw new Error(`dst radix exceeds current charset length (${charset.length})`);
return (_str = '', inputBase = 10, outputBase = outputAlphabet.length) => {
if (outputBase > outputAlphabet.length) throw new Error(`Output radix exceeds the output alphabet length (${outputAlphabet.length})`);

@@ -20,3 +22,3 @@ const res = [];

const s = inputBase <= 36 ? str.toLowerCase() : str;
let nums = Array.from(s, x => charsetMap.get(x));
let nums = Array.from(s, x => inputAlphabetMap.get(x));

@@ -45,3 +47,3 @@ while (nums.length) {

return res.reverse()
.map(x => charset[x])
.map(x => outputAlphabet[x])
.join('');

@@ -48,0 +50,0 @@ };

{
"name": "base-conv",
"version": "2.0.0",
"version": "2.0.1",
"description": "Convert arbitrarily large numbers from any radix representation to any other",

@@ -10,3 +10,3 @@ "main": "index.js",

},
"repository": "github:caub/base-conv",
"repository": "github:caub/baseconv",
"keywords": [

@@ -21,5 +21,5 @@ "number",

"bugs": {
"url": "https://github.com/caub/base-conv/issues"
"url": "https://github.com/caub/baseconv/issues"
},
"homepage": "https://github.com/caub/base-conv#readme",
"homepage": "https://github.com/caub/baseconv#readme",
"devDependencies": {

@@ -26,0 +26,0 @@ "codecov": "^3.2.0",

@@ -7,4 +7,4 @@ # radix conversion

- default charset is `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'`
- you can change it by passing a chartset to the `BaseConv` exported function
- default alphabet is `'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'` for input and output
- you can change it by passing different alphabets to the `BaseConv` exported function
- no IEEE-754 limitation

@@ -11,0 +11,0 @@

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