Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support of big data #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions bases.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
// See README.md for details.

var bases = (typeof exports !== 'undefined' ? exports : (window.Bases = {}));
const Big = require('big.js');

// Returns a string representation of the given number for the given alphabet:
bases.toAlphabet = function (num, alphabet) {
var base = alphabet.length;
var digits = []; // these will be in reverse order since arrays are stacks
num = Big(num);

// execute at least once, even if num is 0, since we should return the '0':
do {
digits.push(num % base); // TODO handle negatives properly?
num = Math.floor(num / base);
digits.push(num.mod(base)); // TODO handle negatives properly?
num = num.div(base);
num = new Big(num).round(0, 0);
} while (num > 0);

var chars = [];
Expand All @@ -24,15 +27,15 @@ bases.toAlphabet = function (num, alphabet) {

// Returns an integer representation of the given string for the given alphabet:
bases.fromAlphabet = function (str, alphabet) {
var base = alphabet.length;
var base = new Big(alphabet.length);
var pos = 0;
var num = 0;
var num = new Big(0);
var c;

while (str.length) {
c = str[str.length - 1];
str = str.substr(0, str.length - 1);
num += Math.pow(base, pos) * alphabet.indexOf(c);
num = num.plus(base.pow(pos).times(alphabet.indexOf(c)));
pos++;
}

Expand Down
43 changes: 26 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
{ "name": "bases"
, "version": "0.2.1"
, "description": "Utility for converting numbers to/from different bases/alphabets."
, "author": "Aseem Kishore <[email protected]>"
, "homepage": "https://github.com/aseemk/bases.js"
, "keywords": ["alphabet", "base", "base-36", "base-58", "base-62"]
, "repository":
{ "type": "git"
, "url": "https://github.com/aseemk/bases.js.git"
}
, "main": "bases.js"
, "dependencies": {}
, "engines":
{ "node": "*"
}
, "scripts":
{ "test": "node test"
{
"name": "bases",
"version": "0.2.1",
"description": "Utility for converting numbers to/from different bases/alphabets.",
"author": "Aseem Kishore <[email protected]>",
"homepage": "https://github.com/aseemk/bases.js",
"keywords": [
"alphabet",
"base",
"base-36",
"base-58",
"base-62"
],
"repository": {
"type": "git",
"url": "https://github.com/aseemk/bases.js.git"
},
"main": "bases.js",
"dependencies": {
"big.js": "^5.2.2"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "node test"
}
}
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,13 @@ for (var base in DATA) {
}
}

const maxMd5Hash = 'ffffffffffffffffffffffffffffffff';
const number = bases.fromBase(maxMd5Hash, 16);
const compacted = bases.toBase(number, 62);
console.log(compacted);
const backNumber = bases.fromBase(compacted, 62);
const backHash = bases.toBase(backNumber, 16);
assert.equal(backHash, maxMd5Hash);

// finally...
console.log('All tests passed.');