diff --git a/angular-base64.js b/angular-base64.js index 6c1d665..5ed0a37 100644 --- a/angular-base64.js +++ b/angular-base64.js @@ -71,8 +71,18 @@ function decode(s) { // convert to string s = "" + s; - var pads, i, b10; - var imax = s.length; + var pads, i, b10, imax; + + if ((s.length % 4) === 3) { + s = s + PADCHAR; + } + + if ((s.length % 4) === 2) { + s = s + PADCHAR + PADCHAR; + } + + imax = s.length; + if (imax === 0) { return s; } diff --git a/package.json b/package.json index 315ec19..d2893d5 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,15 @@ "grunt-contrib-uglify": "", "grunt-notify": "", "grunt-strip": "", - "grunt-text-replace": "" + "grunt-text-replace": "", + "tape": "^4.6.3" }, "name": "@ninjatronic/angular-base64", "description": "Encapsulation of Nick Galbreath's base64.js library for AngularJS", "version": "0.0.2", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node tests" }, "repository": { "type": "git", diff --git a/tests.js b/tests.js new file mode 100644 index 0000000..4e58c27 --- /dev/null +++ b/tests.js @@ -0,0 +1,50 @@ +var test = require('tape'); + +var constants = {}; + +global.angular = { + module: function (moduleName, deps) { + return { + constant: function (constantName, value) { constants[constantName] = value; } + } + } +} + +var b64 = require('./angular-base64.js'); + +test('constant registered', function (t) { + t.plan(1); + + t.notEqual(undefined, constants.$base64); +}) + +test('can encode and decode non-padded', function (t) { + t.plan(2); + + var text = "The quick brown fox jumps over the lazy doges"; + var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZ2Vz"; + + t.equal(b64, constants.$base64.encode(text)); + t.equal(text, constants.$base64.decode(b64)); +}) + +test('can encode and decode padded', function (t) { + t.plan(2); + + var text = "The quick brown fox jumps over the lazy dog"; + var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=="; + + console.log(constants.$base64.encode(text)) + + t.equal(b64, constants.$base64.encode(text)); + t.equal(text, constants.$base64.decode(b64)); +}) + +test('can decode with padding missing', function (t) { + t.plan(1); + + var text = "The quick brown fox jumps over the lazy dog"; + var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw"; + + t.equal(text, constants.$base64.decode(b64)); +})