Comparing version 1.0.2 to 1.0.3
15
index.js
@@ -5,8 +5,8 @@ var base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
encode: function encode64(num) { | ||
return map(toBase(num, 64), function (pos) { | ||
return base64Chars[pos]; | ||
}).join('') || base64Chars[0]; | ||
return (prefix(num) + map(toBase(Math.abs(num), 64), function (pos) { | ||
return base64Chars.charAt(pos); | ||
}).join('')) || base64Chars.charAt(0); | ||
}, | ||
decode: function decode64(base64) { | ||
return fromBase(map(base64.split(''), function (pos) { | ||
return (prefix(base64) ? -1 : 1) * fromBase(map(base64.replace(/^-/, '').split(''), function (pos) { | ||
return base64Chars.indexOf(pos); | ||
@@ -25,2 +25,7 @@ }), 64); | ||
function prefix(num) { | ||
if (String(num).charAt(0) === '-') return '-' | ||
return '' | ||
} | ||
function toBase(value, outputBase) { | ||
@@ -42,2 +47,2 @@ var digits = []; | ||
return vals; | ||
} | ||
} |
{ | ||
"name": "base64int", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Pure JS integer to/from base64 converter", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Base64 Int | ||
---------- | ||
Pure JS integer to/from base64 converter compatible with IE8+, Opera, Firefox, Chrome and Safari. Outputs a base64 string that can be stored in cookies, useful for compressing numbers. | ||
Pure JS integer to/from base64 converter compatible with IE7+, Opera, Firefox, Chrome and Safari. Outputs a base64 string that can be stored in cookies, useful for compressing numbers. | ||
#### How to install | ||
```bash | ||
npm install base64int | ||
``` | ||
_Example_ | ||
@@ -7,0 +12,0 @@ ```javascript |
20
test.js
@@ -18,3 +18,3 @@ var base64Int = require('./index'); | ||
for (i = 0; i < l; i++) { | ||
expect(base64Int.encode(i)).to.be(base64Chars[i]); | ||
expect(base64Int.encode(i)).to.be(base64Chars.charAt(i)); | ||
} | ||
@@ -24,3 +24,3 @@ }); | ||
}); | ||
it('should encode arbitrary numbers into base 64', function () { | ||
@@ -32,2 +32,16 @@ expect(base64Int.encode(128)).to.be('CA'); | ||
}); | ||
describe('negative numbers', function () { | ||
it('should encode/decode to the same number', function () { | ||
for (var i = 2; i < 52; i++) { | ||
var testNum = -(Math.pow(2, i) + Math.round(Math.random() * 10)); | ||
expect(base64Int.decode(base64Int.encode(testNum))).to.be(testNum); | ||
} | ||
}); | ||
it('should encode arbitrary numbers into base 64', function () { | ||
expect(base64Int.encode(-128)).to.be('-CA'); | ||
expect(base64Int.encode(-129)).to.be('-CB'); | ||
expect(base64Int.encode(-1928736)).to.be('-HW4g'); | ||
}); | ||
}) | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5338
118
21