-
Notifications
You must be signed in to change notification settings - Fork 3
/
base62.js
34 lines (32 loc) · 901 Bytes
/
base62.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const base62 = require('ksuid/base62')
module.exports = {
/**
* Encode a buffer to Base62.
*
* @param {Buffer} buffer - The buffer to encode.
* @returns {Promise<string>} The encoded value.
*/
encode: (buffer) => {
return new Promise((resolve, reject) => {
if (!Buffer.isBuffer(buffer)) {
return reject(new TypeError('Expected buffer to be of type Buffer.'))
}
return resolve(base62.encode(buffer))
})
},
/**
* Decode a Base62 string to its original buffer.
*
* @param {string} text - The string to decode.
* @returns {Promise<Buffer>} The decoded buffer.
*/
decode: (text) => {
return new Promise((resolve, reject) => {
if (typeof text !== 'string') {
return reject(
new TypeError(`Expected text to be a string. Received ${text}.`))
}
return resolve(base62.decode(text))
})
}
}