js-base64 2.6.1 → 2.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -47,6 +47,9 @@ Base64.btoa( 'dankogai'); // ZGFua29nYWk=
47
47
  Base64.fromUint8Array( // ZGFua29nYWk=
48
48
  new Uint8Array([100,97,110,107,111,103,97,105])
49
49
  );
50
+ Base64.fromUint8Array( // ZGFua29nYW which is URI safe
51
+ new Uint8Array([100,97,110,107,111,103,97,105]), true
52
+ );
50
53
  Base64.encode( '小飼弾'); // 5bCP6aO85by+
51
54
  Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
52
55
  Base64.btoa( '小飼弾'); // raises exception
package/base64.js CHANGED
@@ -22,7 +22,7 @@
22
22
  // existing version for noConflict()
23
23
  global = global || {};
24
24
  var _Base64 = global.Base64;
25
- var version = "2.6.1";
25
+ var version = "2.6.2";
26
26
  // constants
27
27
  var b64chars
28
28
  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -79,18 +79,30 @@
79
79
  var _encode = function(u) {
80
80
  return btoa(utob(String(u)));
81
81
  };
82
+ var mkUriSafe = function (b64) {
83
+ return b64.replace(/[+\/]/g, function(m0) {
84
+ return m0 == '+' ? '-' : '_';
85
+ }).replace(/=/g, '');
86
+ };
82
87
  var encode = function(u, urisafe) {
83
- return !urisafe
84
- ? _encode(String(u))
85
- : _encode(String(u)).replace(/[+\/]/g, function(m0) {
86
- return m0 == '+' ? '-' : '_';
87
- }).replace(/=/g, '');
88
+ return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
88
89
  };
89
90
  var encodeURI = function(u) { return encode(u, true) };
90
- var fromUint8Array = function(a) {
91
- return btoa(Array.from(a, function(c) {
92
- return String.fromCharCode(c)
93
- }).join(''));
91
+ var fromUint8Array;
92
+ if (global.Uint8Array) fromUint8Array = function(a, urisafe) {
93
+ // return btoa(fromCharCode.apply(null, a));
94
+ var b64 = '';
95
+ for (var i = 0, l = a.length; i < l; i += 3) {
96
+ var a0 = a[i], a1 = a[i+1], a2 = a[i+2];
97
+ var ord = a0 << 16 | a1 << 8 | a2;
98
+ b64 += b64chars.charAt( ord >>> 18)
99
+ + b64chars.charAt((ord >>> 12) & 63)
100
+ + ( typeof a1 != 'undefined'
101
+ ? b64chars.charAt((ord >>> 6) & 63) : '=')
102
+ + ( typeof a2 != 'undefined'
103
+ ? b64chars.charAt( ord & 63) : '=');
104
+ }
105
+ return urisafe ? mkUriSafe(b64) : b64;
94
106
  };
95
107
  // decoder stuff
96
108
  var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
@@ -150,7 +162,8 @@
150
162
  }).replace(/[^A-Za-z0-9\+\/]/g, '')
151
163
  );
152
164
  };
153
- var toUint8Array = function(a) {
165
+ var toUint8Array;
166
+ if (global.Uint8Array) toUint8Array = function(a) {
154
167
  return Uint8Array.from(atob(a), function(c) {
155
168
  return c.charCodeAt(0);
156
169
  });
package/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "2.6.1",
3
+ "version": "2.6.2",
4
4
  "license": "BSD-3-Clause",
5
5
  "main": [
6
6
  "./base64.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "2.6.1",
3
+ "version": "2.6.2",
4
4
  "description": "Yet another Base64 transcoder in pure-JS",
5
5
  "main": "base64.js",
6
6
  "directories": {
package/test/dankogai.js CHANGED
@@ -42,3 +42,27 @@ describe('Base64', function () {
42
42
  it('.decode', is(Base64.decode('5bCP6aO85by+'), '小飼弾'));
43
43
  it('.decode', is(Base64.decode('5bCP6aO85by-'), '小飼弾'));
44
44
  });
45
+
46
+ if (typeof Uint8Array === 'function') describe('fromBase64', function() {
47
+ it('dankogai', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103,97,105])), Base64.encode('dankogai')));
48
+ it('dankoga', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103,97])), Base64.encode('dankoga')));
49
+ it('dankog', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111,103])), Base64.encode('dankog')));
50
+ it('danko', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107,111])), Base64.encode('danko')));
51
+ it('dank', is(Base64.fromUint8Array(new Uint8Array([100,97,110,107])), Base64.encode('dank')));
52
+ it('dan', is(Base64.fromUint8Array(new Uint8Array([100,97,110])), Base64.encode('dan')));
53
+ it('da', is(Base64.fromUint8Array(new Uint8Array([100,97])), Base64.encode('da')));
54
+ it('d', is(Base64.fromUint8Array(new Uint8Array([100])), Base64.encode('d')));
55
+ it('', is(Base64.fromUint8Array(new Uint8Array([])), Base64.encode('')));
56
+ });
57
+
58
+ if (typeof Uint8Array === 'function') describe('toBase64', function() {
59
+ it('ZGFua29nYWk=', is(Base64.toUint8Array('ZGFua29nYWk=').toString(), '100,97,110,107,111,103,97,105'));
60
+ it('ZGFua29nYQ==', is(Base64.toUint8Array('ZGFua29nYQ==').toString(), '100,97,110,107,111,103,97'));
61
+ it('ZGFua29n', is(Base64.toUint8Array('ZGFua29n').toString(), '100,97,110,107,111,103'));
62
+ it('ZGFua28=', is(Base64.toUint8Array('ZGFua28=').toString(), '100,97,110,107,111'));
63
+ it('ZGFuaw==', is(Base64.toUint8Array('ZGFuaw==').toString(), '100,97,110,107'));
64
+ it('ZGFu', is(Base64.toUint8Array('ZGFu').toString(), '100,97,110'));
65
+ it('ZGE=', is(Base64.toUint8Array('ZGE=').toString(), '100,97'));
66
+ it('ZA==', is(Base64.toUint8Array('ZA==').toString(), '100'));
67
+ it('', is(Base64.toUint8Array('').toString(), ''));
68
+ });