js-base64 2.5.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
@@ -42,14 +42,29 @@ import { Base64 } from 'js-base64';
42
42
  ## SYNOPSIS
43
43
 
44
44
  ```javascript
45
- Base64.encode('dankogai'); // ZGFua29nYWk=
46
- Base64.encode('小飼弾'); // 5bCP6aO85by+
47
- Base64.encodeURI('小飼弾'); // 5bCP6aO85by-
45
+ Base64.encode('dankogai'); // ZGFua29nYWk=
46
+ Base64.btoa( 'dankogai'); // ZGFua29nYWk=
47
+ Base64.fromUint8Array( // ZGFua29nYWk=
48
+ new Uint8Array([100,97,110,107,111,103,97,105])
49
+ );
50
+ Base64.fromUint8Array( // ZGFua29nYW which is URI safe
51
+ new Uint8Array([100,97,110,107,111,103,97,105]), true
52
+ );
53
+ Base64.encode( '小飼弾'); // 5bCP6aO85by+
54
+ Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true)
55
+ Base64.btoa( '小飼弾'); // raises exception
56
+ ```
48
57
 
58
+ ```javascript
49
59
  Base64.decode('ZGFua29nYWk='); // dankogai
60
+ Base64.atob( 'ZGFua29nYWk='); // dankogai
61
+ Base64.toUint8Array( // new Uint8Array([100,97,110,107,111,103,97,105])
62
+ 'ZGFua29nYWk='
63
+ );
50
64
  Base64.decode('5bCP6aO85by+'); // 小飼弾
51
65
  // note .decodeURI() is unnecessary since it accepts both flavors
52
66
  Base64.decode('5bCP6aO85by-'); // 小飼弾
67
+ Base64.atob( '5bCP6aO85by+'); // '小飼弾' which is nonsense
53
68
  ```
54
69
 
55
70
  ### String Extension for ES5
@@ -59,13 +74,13 @@ if (Base64.extendString) {
59
74
  // you have to explicitly extend String.prototype
60
75
  Base64.extendString();
61
76
  // once extended, you can do the following
62
- 'dankogai'.toBase64(); // ZGFua29nYWk=
63
- '小飼弾'.toBase64(); // 5bCP6aO85by+
64
- '小飼弾'.toBase64(true); // 5bCP6aO85by-
65
- '小飼弾'.toBase64URI(); // 5bCP6aO85by-
66
- 'ZGFua29nYWk='.fromBase64(); // dankogai
67
- '5bCP6aO85by+'.fromBase64(); // 小飼弾
68
- '5bCP6aO85by-'.fromBase64(); // 小飼弾
77
+ 'dankogai'.toBase64(); // ZGFua29nYWk=
78
+ '小飼弾'.toBase64(); // 5bCP6aO85by+
79
+ '小飼弾'.toBase64(true); // 5bCP6aO85by-
80
+ '小飼弾'.toBase64URI(); // 5bCP6aO85by-
81
+ 'ZGFua29nYWk='.fromBase64(); // dankogai
82
+ '5bCP6aO85by+'.fromBase64(); // 小飼弾
83
+ '5bCP6aO85by-'.fromBase64(); // 小飼弾
69
84
  }
70
85
  ```
71
86
 
package/base64.html CHANGED
@@ -22,7 +22,7 @@
22
22
  </tr>
23
23
  <tr><th width="50%">Roundtrip</th><th>iframe w/ data: (no IE)</th></tr>
24
24
  <tr>
25
- <th><textarea id="roundtrip" cols=32" rows="4" disabled></textarea></th>
25
+ <th><textarea id="roundtrip" cols="32" rows="4" disabled></textarea></th>
26
26
  <th><iframe id="data" width="80%" height="64"></iframe></th>
27
27
  </tr>
28
28
  </tbody></table>
package/base64.js CHANGED
@@ -22,16 +22,7 @@
22
22
  // existing version for noConflict()
23
23
  global = global || {};
24
24
  var _Base64 = global.Base64;
25
- var version = "2.5.1";
26
- // if node.js and NOT React Native, we use Buffer
27
- var buffer;
28
- if (typeof module !== 'undefined' && module.exports) {
29
- try {
30
- buffer = eval("require('buffer').Buffer");
31
- } catch (err) {
32
- buffer = undefined;
33
- }
34
- }
25
+ var version = "2.6.2";
35
26
  // constants
36
27
  var b64chars
37
28
  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -49,8 +40,8 @@
49
40
  : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
50
41
  + fromCharCode(0x80 | (cc & 0x3f)))
51
42
  : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
52
- + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
53
- + fromCharCode(0x80 | ( cc & 0x3f)));
43
+ + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
44
+ + fromCharCode(0x80 | ( cc & 0x3f)));
54
45
  } else {
55
46
  var cc = 0x10000
56
47
  + (c.charCodeAt(0) - 0xD800) * 0x400
@@ -78,37 +69,43 @@
78
69
  ];
79
70
  return chars.join('');
80
71
  };
81
- var btoa = global.btoa ? function(b) {
82
- return global.btoa(b);
83
- } : function(b) {
72
+ var btoa = global.btoa && typeof global.btoa == 'function'
73
+ ? function(b){ return global.btoa(b) } : function(b) {
74
+ if (b.match(/[^\x00-\xFF]/)) throw new RangeError(
75
+ 'The string contains invalid characters.'
76
+ );
84
77
  return b.replace(/[\s\S]{1,3}/g, cb_encode);
85
78
  };
86
- var _encode = buffer ?
87
- buffer.from && Uint8Array && buffer.from !== Uint8Array.from
88
- ? function (u) {
89
- return (u.constructor === buffer.constructor ? u : buffer.from(u))
90
- .toString('base64')
91
- }
92
- : function (u) {
93
- return (u.constructor === buffer.constructor ? u : new buffer(u))
94
- .toString('base64')
95
- }
96
- : function (u) { return btoa(utob(u)) }
97
- ;
79
+ var _encode = function(u) {
80
+ return btoa(utob(String(u)));
81
+ };
82
+ var mkUriSafe = function (b64) {
83
+ return b64.replace(/[+\/]/g, function(m0) {
84
+ return m0 == '+' ? '-' : '_';
85
+ }).replace(/=/g, '');
86
+ };
98
87
  var encode = function(u, urisafe) {
99
- return !urisafe
100
- ? _encode(String(u))
101
- : _encode(String(u)).replace(/[+\/]/g, function(m0) {
102
- return m0 == '+' ? '-' : '_';
103
- }).replace(/=/g, '');
88
+ return urisafe ? mkUriSafe(_encode(u)) : _encode(u);
104
89
  };
105
90
  var encodeURI = function(u) { return encode(u, true) };
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;
106
+ };
106
107
  // decoder stuff
107
- var re_btou = new RegExp([
108
- '[\xC0-\xDF][\x80-\xBF]',
109
- '[\xE0-\xEF][\x80-\xBF]{2}',
110
- '[\xF0-\xF7][\x80-\xBF]{3}'
111
- ].join('|'), 'g');
108
+ var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
112
109
  var cb_btou = function(cccc) {
113
110
  switch(cccc.length) {
114
111
  case 4:
@@ -150,31 +147,27 @@
150
147
  chars.length -= [0, 0, 2, 1][padlen];
151
148
  return chars.join('');
152
149
  };
153
- var _atob = global.atob ? function(a) {
154
- return global.atob(a);
155
- } : function(a){
150
+ var _atob = global.atob && typeof global.atob == 'function'
151
+ ? function(a){ return global.atob(a) } : function(a){
156
152
  return a.replace(/\S{1,4}/g, cb_decode);
157
153
  };
158
154
  var atob = function(a) {
159
155
  return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
160
156
  };
161
- var _decode = buffer ?
162
- buffer.from && Uint8Array && buffer.from !== Uint8Array.from
163
- ? function(a) {
164
- return (a.constructor === buffer.constructor
165
- ? a : buffer.from(a, 'base64')).toString();
166
- }
167
- : function(a) {
168
- return (a.constructor === buffer.constructor
169
- ? a : new buffer(a, 'base64')).toString();
170
- }
171
- : function(a) { return btou(_atob(a)) };
157
+ var _decode = function(a) { return btou(_atob(a)) };
172
158
  var decode = function(a){
173
159
  return _decode(
174
- String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
175
- .replace(/[^A-Za-z0-9\+\/]/g, '')
160
+ String(a).replace(/[-_]/g, function(m0) {
161
+ return m0 == '-' ? '+' : '/'
162
+ }).replace(/[^A-Za-z0-9\+\/]/g, '')
176
163
  );
177
164
  };
165
+ var toUint8Array;
166
+ if (global.Uint8Array) toUint8Array = function(a) {
167
+ return Uint8Array.from(atob(a), function(c) {
168
+ return c.charCodeAt(0);
169
+ });
170
+ };
178
171
  var noConflict = function() {
179
172
  var Base64 = global.Base64;
180
173
  global.Base64 = _Base64;
@@ -193,7 +186,8 @@
193
186
  btou: btou,
194
187
  decode: decode,
195
188
  noConflict: noConflict,
196
- __buffer__: buffer
189
+ fromUint8Array: fromUint8Array,
190
+ toUint8Array: toUint8Array
197
191
  };
198
192
  // if ES5 is available, make Base64.extendString() available
199
193
  if (typeof Object.defineProperty === 'function') {
package/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-base64",
3
- "version": "2.5.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.5.1",
3
+ "version": "2.6.2",
4
4
  "description": "Yet another Base64 transcoder in pure-JS",
5
5
  "main": "base64.js",
6
6
  "directories": {
@@ -12,7 +12,7 @@
12
12
  "devDependencies": {
13
13
  "babel-preset-env": "^1.7.0",
14
14
  "babel-register": "^6.26.0",
15
- "mocha": "*"
15
+ "mocha": "5.x.x"
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
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
+ });
package/test/index.html CHANGED
@@ -7,7 +7,7 @@
7
7
  <body>
8
8
  <div id="mocha"></div>
9
9
 
10
- <script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
10
+ <script src="https://cdn.rawgit.com/jquery/jquery/3.5.1/dist/jquery.min.js"></script>
11
11
  <script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
12
12
  <script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
13
13
  <script>
package/base64.min.js DELETED
@@ -1 +0,0 @@
1
- (function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory(global):typeof define==="function"&&define.amd?define(factory):factory(global)})(typeof self!=="undefined"?self:typeof window!=="undefined"?window:typeof global!=="undefined"?global:this,function(global){"use strict";global=global||{};var _Base64=global.Base64;var version="2.5.1";var buffer;if(typeof module!=="undefined"&&module.exports){try{buffer=eval("require('buffer').Buffer")}catch(err){buffer=undefined}}var b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64tab=function(bin){var t={};for(var i=0,l=bin.length;i<l;i++)t[bin.charAt(i)]=i;return t}(b64chars);var fromCharCode=String.fromCharCode;var cb_utob=function(c){if(c.length<2){var cc=c.charCodeAt(0);return cc<128?c:cc<2048?fromCharCode(192|cc>>>6)+fromCharCode(128|cc&63):fromCharCode(224|cc>>>12&15)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}else{var cc=65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320);return fromCharCode(240|cc>>>18&7)+fromCharCode(128|cc>>>12&63)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}};var re_utob=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob=function(u){return u.replace(re_utob,cb_utob)};var cb_encode=function(ccc){var padlen=[0,2,1][ccc.length%3],ord=ccc.charCodeAt(0)<<16|(ccc.length>1?ccc.charCodeAt(1):0)<<8|(ccc.length>2?ccc.charCodeAt(2):0),chars=[b64chars.charAt(ord>>>18),b64chars.charAt(ord>>>12&63),padlen>=2?"=":b64chars.charAt(ord>>>6&63),padlen>=1?"=":b64chars.charAt(ord&63)];return chars.join("")};var btoa=global.btoa?function(b){return global.btoa(b)}:function(b){return b.replace(/[\s\S]{1,3}/g,cb_encode)};var _encode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(u){return(u.constructor===buffer.constructor?u:buffer.from(u)).toString("base64")}:function(u){return(u.constructor===buffer.constructor?u:new buffer(u)).toString("base64")}:function(u){return btoa(utob(u))};var encode=function(u,urisafe){return!urisafe?_encode(String(u)):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=new RegExp(["[À-ß][€-¿]","[à-ï][€-¿]{2}","[ð-÷][€-¿]{3}"].join("|"),"g");var cb_btou=function(cccc){switch(cccc.length){case 4:var cp=(7&cccc.charCodeAt(0))<<18|(63&cccc.charCodeAt(1))<<12|(63&cccc.charCodeAt(2))<<6|63&cccc.charCodeAt(3),offset=cp-65536;return fromCharCode((offset>>>10)+55296)+fromCharCode((offset&1023)+56320);case 3:return fromCharCode((15&cccc.charCodeAt(0))<<12|(63&cccc.charCodeAt(1))<<6|63&cccc.charCodeAt(2));default:return fromCharCode((31&cccc.charCodeAt(0))<<6|63&cccc.charCodeAt(1))}};var btou=function(b){return b.replace(re_btou,cb_btou)};var cb_decode=function(cccc){var len=cccc.length,padlen=len%4,n=(len>0?b64tab[cccc.charAt(0)]<<18:0)|(len>1?b64tab[cccc.charAt(1)]<<12:0)|(len>2?b64tab[cccc.charAt(2)]<<6:0)|(len>3?b64tab[cccc.charAt(3)]:0),chars=[fromCharCode(n>>>16),fromCharCode(n>>>8&255),fromCharCode(n&255)];chars.length-=[0,0,2,1][padlen];return chars.join("")};var _atob=global.atob?function(a){return global.atob(a)}:function(a){return a.replace(/\S{1,4}/g,cb_decode)};var atob=function(a){return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g,""))};var _decode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(a){return(a.constructor===buffer.constructor?a:buffer.from(a,"base64")).toString()}:function(a){return(a.constructor===buffer.constructor?a:new buffer(a,"base64")).toString()}:function(a){return btou(_atob(a))};var decode=function(a){return _decode(String(a).replace(/[-_]/g,function(m0){return m0=="-"?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))};var noConflict=function(){var Base64=global.Base64;global.Base64=_Base64;return Base64};global.Base64={VERSION:version,atob:atob,btoa:btoa,fromBase64:decode,toBase64:encode,utob:utob,encode:encode,encodeURI:encodeURI,btou:btou,decode:decode,noConflict:noConflict,__buffer__:buffer};if(typeof Object.defineProperty==="function"){var noEnum=function(v){return{value:v,enumerable:false,writable:true,configurable:true}};global.Base64.extendString=function(){Object.defineProperty(String.prototype,"fromBase64",noEnum(function(){return decode(this)}));Object.defineProperty(String.prototype,"toBase64",noEnum(function(urisafe){return encode(this,urisafe)}));Object.defineProperty(String.prototype,"toBase64URI",noEnum(function(){return encode(this,true)}))}}if(global["Meteor"]){Base64=global.Base64}if(typeof module!=="undefined"&&module.exports){module.exports.Base64=global.Base64}else if(typeof define==="function"&&define.amd){define([],function(){return global.Base64})}return{Base64:global.Base64}});