Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added big number format and base parsing

  • Loading branch information...
commit 07828fd075229f3c4ec7a766c89e0d73713d5769 1 parent df5963f
@alanclarke authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+/node_modules
+.DS_Store
View
10 README.md
@@ -2,6 +2,8 @@
a simple utitlity to encode and decode ints from a predefined alphabet
+this includes support for big integers via https://github.com/justmoon/node-bignum
+
some good uses of this utility include:
- reducing the number of characters required to express a numerical value
@@ -21,15 +23,17 @@ int_encoder.encode(12345678); // "ZXP0"
int_encoder.decode('ZXP0'); // 12345678
//invoke custom alphabet option
-int_encoder.alphabet('0123456789abcdef') //hex alphabet
+int_encoder.alphabet('0123456789abcdef') // hex alphabet
int_encoder.encode(255); // "ff"
int_encoder.decode('ff'); // 255
//check what alphabet is being used
-int_encoder.alphabet() //0123456789abcdef
-
+int_encoder.alphabet() // 0123456789abcdef
+//convert big hex number using optional base argument
+int_encoder.encode('e6c6b53d3c8160b22dad35a0f705ec09', 16); // 'hbDcW9aE89tzLYjDgyzajJ'
+int_encoder.decode('e6c6b53d3c8160b22dad35a0f705ec09', 16); // 'e6c6b53d3c8160b22dad35a0f705ec09'
```
View
34 lib/int-encoder.js
@@ -5,39 +5,45 @@
* Copyright (c) 2012 alan clarke
* Licensed under the MIT license.
*/
-
+var bignum = require('bignum');
(function(){
module.exports = (function(alphabet) {
- alphabet = (alphabet || 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
+ var alphabets = {
+ 'hex':'0123456789abcdef',
+ 'reset': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
+ };
+
+ alphabet = (alphabet || alphabets.reset);
var re = {
is_alphabet: new RegExp('^['+alphabet+']*$'),
is_int: /^\d*$/
};
return {
- encode:function(value){
- if(!re.is_int.test(value)){
- throw new Error("int-encoder:encode parameter must be a positive integer");
+ encode:function(value, base){
+ if(!re.is_int.test(value)&&!base){
+ throw new Error("int-encoder:encode parameter must be a positive integer or string containing only digits");
}
+ value = bignum(value.toString(), base);
var result = '';
- if(value===0){ return alphabet[0]; }
- while(value>0){
- var v = value % alphabet.length;
+ if(value.eq(0)){ return alphabet[0]; }
+ while(value.gt(0)){
+ var v = value.mod(alphabet.length);
result = alphabet[v] + result;
- value -= v;
- value = Math.floor(value/alphabet.length);
+ value = value.sub(v).div(alphabet.length);
}
return result;
},
- decode:function(str){
+ decode:function(str, base){
if(!re.is_alphabet.test(str)){
throw new Error("int-encoder:decode parameter must be a string made up exclusively from characters from the alphabet option ("+alphabet+")");
}
- var result = 0;
+ var result = bignum(0);
for( var i =0; i < str.length; i++ ) {
- result += (alphabet.indexOf(str[i]) * Math.pow(alphabet.length,str.length - 1 - i ))
+ var index = bignum(alphabet.indexOf(str[i]));
+ result = result.add(index.mul(bignum(alphabet.length).pow(str.length - 1 - i)));
}
- return result;
+ return result.toString(base);
},
alphabet:function(_alphabet){
if(_alphabet){
View
5 package.json
@@ -1,7 +1,7 @@
{
"name": "int-encoder",
"description": "a simple utitlity to encode and decode ints from a predefined alphabet",
- "version": "0.1.0",
+ "version": "0.1.1",
"homepage": "https://github.com/alanclarke/int-encoder",
"author": {
"name": "alan clarke",
@@ -29,7 +29,8 @@
"test": "grunt test"
},
"devDependencies": {
- "grunt": "~0.3.11"
+ "grunt": "~0.3.11",
+ "bignum": "*"
},
"keywords": []
}
View
7 test/int-encoder_test.js
@@ -35,6 +35,13 @@ exports['encode'] = {
test.ok( i.toString().length >= encoded.toString().length, 'encoded string is equal to or longer than int' );
test.equal(i,int_encoder.decode(encoded),'decoded string is same as input');
});
+
+ var hex = 'e6c6b53d3c8160b22dad35a0f705ec09', encoded_hex = int_encoder.encode(hex,16);
+
+ test.equal(encoded_hex,'hbDcW9aE89tzLYjDgyzajJ', 'should encode large hex values');
+
+ test.equal(int_encoder.decode(encoded_hex, 16),'e6c6b53d3c8160b22dad35a0f705ec09', 'should decode large hex values');
+
test.done();
},
'errors':function(test){
Please sign in to comment.
Something went wrong with that request. Please try again.