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 @@ |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4748
71