Base64
Base64 encoding/decoding in pure JS on both modern Browsers and Node.js based on base64-js.
Also supports URL-safe base64
Features
- Encoding input supports typeof
string
, number
and bigint
- Encoding input supports
ArrayBuffer
or Uint8Array
- Encoding/Decoding via
TextEncoder
/TextDecoder
under browser and Buffer
under Node.js
Browser requirement
Node.js requirement
- v10.4.0+ for
bigint
base64 encoding - ICU installation for base64 decoding with encodeings other then
utf-8
and utf-16le
Installing
npm install @waiting/base64
Usage of Node.js
See the Docs for details
Encodeing
import { b64encode, b64fromBuffer, b64urlEncode } from '@waiting/base64'
b64encode('A') === 'QQ=='
b64encode('𠮷') === b64encode('\uD842\uDFB7') === b64encode('\u{20BB7}')
b64encode('schöne') === 'c2Now7ZuZQ=='
b64encode(1n) === b64encode(1)
const u8arr = Uint8Array.from([0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87])
b64fromBuffer(u8arr) === b64encode('中文')
b64urlEncode('A') === 'QQ'
Decoding
import { b64decode, b64urlDecode } from '@waiting/base64'
b64decode('MQ==') === '1'
b64urlDecode('MQ') === '1'
Transfer
import { b64toURLSafe, b64fromURLSafe } from '@waiting/base64'
b64toURLSafe('QQ==') === 'QQ'
b64toURLSafe('5Lit5paH5rWL6K+V') === '5Lit5paH5rWL6K-V'
b64fromURLSafe('QQ') === 'QQ=='
b64fromURLSafe('0J_RgNC40LLQtdGCLCDQvNC40YAh') === '0J/RgNC40LLQtdGCLCDQvNC40YAh'
Usage of browser
See the Docs for details
ESM
<script type="module">
import { b64encode, b64urlEncode } from './base64.esm.min.js'
console.log(b64encode('A'))
console.log(b64urlEncode('A'))
</script>
UMD
<script src="./base64.umd.min.js"></script>
<script>
console.log(base64.b64encode('A'))
console.log(base64.b64urlEncode('A'))
</script>
Testing
npm run test
npm run test:browser
cd .config && npm i
npm run browser:detect
Demos
License
MIT
Languages