1 | 'use strict'
|
2 |
|
3 | module.exports = {
|
4 | unescape: unescape,
|
5 | escape: escape,
|
6 | encode: encode,
|
7 | decode: decode
|
8 | }
|
9 |
|
10 | function unescape (str) {
|
11 | return (str + '==='.slice((str.length + 3) % 4))
|
12 | .replace(/-/g, '+')
|
13 | .replace(/_/g, '/')
|
14 | }
|
15 |
|
16 | function escape (str) {
|
17 | return str.replace(/\+/g, '-')
|
18 | .replace(/\//g, '_')
|
19 | .replace(/=/g, '')
|
20 | }
|
21 |
|
22 | function encode (str, encoding) {
|
23 | return escape(Buffer.from(str, encoding || 'utf8').toString('base64'))
|
24 | }
|
25 |
|
26 | function decode (str, encoding) {
|
27 | return Buffer.from(unescape(str), 'base64').toString(encoding || 'utf8')
|
28 | }
|