js-base64
Advanced tools
Comparing version 3.1.3 to 3.2.0
@@ -31,3 +31,3 @@ | ||
*/ | ||
const version = '3.1.3'; | ||
const version = '3.2.0'; | ||
const _b64chars | ||
@@ -70,3 +70,3 @@ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
const _btoa = typeof btoa === 'function' | ||
? btoa.bind(this) | ||
? btoa | ||
: (src) => { | ||
@@ -129,3 +129,3 @@ if (src.match(/[^\x00-\xFF]/)) throw new RangeError( | ||
const _atob = typeof atob === 'function' | ||
? atob.bind(this) | ||
? atob | ||
: (a) => { | ||
@@ -156,18 +156,2 @@ return String(a) | ||
}; | ||
const gBase64 = { | ||
VERSION: version, | ||
atob: _atob, | ||
btoa: _btoa, | ||
fromBase64: decode, | ||
toBase64: encode, | ||
utob: utob, | ||
encode: encode, | ||
encodeURI: encodeURI, | ||
encodeURL: encodeURI, | ||
btou: btou, | ||
decode: decode, | ||
// noConflict: noConflict, | ||
fromUint8Array: fromUint8Array, | ||
toUint8Array: toUint8Array | ||
}; | ||
const _noEnum = (v) => { | ||
@@ -178,3 +162,3 @@ return { | ||
}; | ||
gBase64.extendString = function() { | ||
const extendString = function() { | ||
const _add = (name, body) => Object.defineProperty( | ||
@@ -199,4 +183,3 @@ String.prototype, name, _noEnum(body) | ||
}; | ||
// make Base64.extendUint8Array() available | ||
gBase64.extendUint8Array = function() { | ||
const extendUint8Array = function() { | ||
const _add = (name, body) => Object.defineProperty( | ||
@@ -215,8 +198,25 @@ Uint8Array.prototype, name, _noEnum(body) | ||
}; | ||
gBase64.extendBuiltins = () => { | ||
gBase64.extendString(); | ||
gBase64.extendUint8Array(); | ||
const extendBuiltins = () => { | ||
extendString(); | ||
extendUint8Array(); | ||
} | ||
const gBase64 = { | ||
VERSION: version, | ||
atob: _atob, | ||
btoa: _btoa, | ||
fromBase64: decode, | ||
toBase64: encode, | ||
encode: encode, | ||
encodeURI: encodeURI, | ||
encodeURL: encodeURI, | ||
utob: utob, | ||
btou: btou, | ||
decode: decode, | ||
fromUint8Array: fromUint8Array, | ||
toUint8Array: toUint8Array, | ||
extendString: extendString, | ||
extendUint8Array: extendUint8Array, | ||
extendBuiltins: extendBuiltins | ||
} | ||
global.Base64 = gBase64; | ||
@@ -223,0 +223,0 @@ gBase64.noConflict = () => { |
{ | ||
"name": "js-base64", | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"description": "Yet another Base64 transcoder in pure-JS", | ||
"main": "base64.js", | ||
"module": "base64.mjs", | ||
"browser": "base64-es5.js", | ||
"files": [ | ||
@@ -14,10 +13,9 @@ "base64.js", | ||
"makecjs": "util/makecjs > base64.js", | ||
"test": "npm run makecjs && mocha --require @babel/register", | ||
"transpile": "babel base64.js -o base64-es5.js" | ||
"test": "npm run makecjs && mocha --require @babel/register" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.10.0", | ||
"@babel/core": "^7.10.0", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.10.4", | ||
"@babel/preset-env": "^7.10.0", | ||
"@babel/register": "^7.10.0", | ||
"@babel/preset-env": "^7.10.0", | ||
"mocha": "^8.0.0" | ||
@@ -24,0 +22,0 @@ }, |
@@ -9,3 +9,3 @@ [![build status](https://secure.travis-ci.org/dankogai/js-base64.png)](http://travis-ci.org/dankogai/js-base64) | ||
Version 3 is completely rewritten with ES2015 features like arrow functions. All modern browsers and node 12 or up are directly supported. Your codes should run unchanged. IE is no longer supported directly but you can transpile the script to use it (see below). | ||
Version 3 is completely rewritten with ES2015 features like arrow functions. All modern browsers and node.js are directly supported. Your codes should run unchanged. IE is no longer supported directly but you can transpile the script to use it (see below). | ||
@@ -24,16 +24,2 @@ The hardest part of maintaining this module was not Base64 features, but cross-platform support (eg. nodejs vs web browsers). By making ES2015 mandatory virtually all codes are common (except `atob()` and `btoa()`). | ||
### Transpile to ES5 | ||
Since `base64.js` is now written in ES2015 (aka ES6), It no longer runs on IE. Yet you can still transpile it to ES5 and the resulting script does run on IE. just | ||
```shell | ||
$ npm run transpile | ||
``` | ||
which does | ||
```shell | ||
$ babel base64.js -o base64-es5.js | ||
``` | ||
### In Browser | ||
@@ -50,3 +36,3 @@ | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.1.3/base64.min.js"> | ||
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.2.0/base64.min.js"> | ||
``` | ||
@@ -66,2 +52,5 @@ | ||
import { Base64 } from 'js-base64'; | ||
// or if you prefer no Base64 namespace | ||
import { encode, decode } from 'js-base64'; | ||
``` | ||
@@ -74,3 +63,6 @@ | ||
// note jsdelivr.net does not automatically minify .mjs | ||
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.1.3/base64.mjs'; | ||
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.2.0/base64.mjs'; | ||
// or if you prefer no Base64 namespace | ||
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.2.0/base64.mjs'; | ||
</script> | ||
@@ -82,29 +74,29 @@ ``` | ||
```javascript | ||
Base64.encode('dankogai'); // ZGFua29nYWk= | ||
Base64.btoa( 'dankogai'); // ZGFua29nYWk= | ||
Base64.fromUint8Array( // ZGFua29nYWk= | ||
new Uint8Array([100,97,110,107,111,103,97,105]) | ||
); | ||
Base64.fromUint8Array( // ZGFua29nYW which is URI safe | ||
new Uint8Array([100,97,110,107,111,103,97,105]), true | ||
); | ||
Base64.encode( '小飼弾'); // 5bCP6aO85by+ | ||
Base64.encodeURI('小飼弾'); // 5bCP6aO85by- which equals to Base64.encode('小飼弾', true) | ||
Base64.btoa( '小飼弾'); // raises exception | ||
let latin = 'dankogai'; | ||
let utf8 = '小飼弾' | ||
let u8s = new Uint8Array([100,97,110,107,111,103,97,105]); | ||
Base64.encode(latin); // ZGFua29nYWk= | ||
Base64.btoa(latin); // ZGFua29nYWk= | ||
Base64.btoa(utf8); // raises exception | ||
Base64.fromUint8Array(u8s); // ZGFua29nYWk= | ||
Base64.fromUint8Array(u8s); // ZGFua29nYW which is URI safe | ||
Base64.encode(utf8); // 5bCP6aO85by+ | ||
Base64.encode(utf8, true) // 5bCP6aO85by- | ||
Base64.encodeURI(utf8); // 5bCP6aO85by- | ||
``` | ||
```javascript | ||
Base64.decode('ZGFua29nYWk='); // dankogai | ||
Base64.atob( 'ZGFua29nYWk='); // dankogai | ||
Base64.toUint8Array( // new Uint8Array([100,97,110,107,111,103,97,105]) | ||
'ZGFua29nYWk=' | ||
); | ||
Base64.decode('5bCP6aO85by+'); // 小飼弾 | ||
Base64.decode( 'ZGFua29nYWk=');// dankogai | ||
Base64.atob( 'ZGFua29nYWk=');// dankogai | ||
Base64.atob( '5bCP6aO85by+');// 'å°é£¼å¼¾' which is nonsense | ||
Base64.toUint8Array('ZGFua29nYWk=');// u8s above | ||
Base64.decode( '5bCP6aO85by+');// 小飼弾 | ||
// note .decodeURI() is unnecessary since it accepts both flavors | ||
Base64.decode('5bCP6aO85by-'); // 小飼弾 | ||
Base64.atob( '5bCP6aO85by+'); // 'å°é£¼å¼¾' which is nonsense | ||
Base64.decode( '5bCP6aO85by-');// 小飼弾 | ||
``` | ||
### String Extension for ES5 | ||
### Built-in Extensions | ||
By default `Base64` leaves built-in prototypes untouched. But you can extend them as below. | ||
```javascript | ||
@@ -122,3 +114,3 @@ // you have to explicitly extend String.prototype | ||
'5bCP6aO85by-'.fromBase64(); // 小飼弾 | ||
'5bCP6aO85by-'.toUint8Array();// new Uint8Array([100,97,110,107,111,103,97,105]) | ||
'5bCP6aO85by-'.toUint8Array();// u8s above | ||
``` | ||
@@ -130,3 +122,2 @@ | ||
// once extended, you can do the following | ||
const u8s = new Uint8Array([100,97,110,107,111,103,97,105]); | ||
u8s.toBase64(); // 'ZGFua29nYWk=' | ||
@@ -137,3 +128,6 @@ u8s.toBase64URI(); // 'ZGFua29nYWk' | ||
You can extend both via `Base64.extendBuiltin()`. | ||
```javascript | ||
// extend all at once | ||
Base64.extendBuiltins() | ||
``` | ||
@@ -140,0 +134,0 @@ ### TypeScript |
Sorry, the diff of this file is not supported yet
20095
447
149