I am currently implementing a piece of JavaScript code taken from a publicly available repository located at:
https://github.com/base62/base62.js
My goal is to capture the output for further manipulation, specifically for a date conversion process. However, when I attempt to slice the integer, I encounter an error indicating that it is not a function. Below is a sample of the code where the issue arises (highlighted within a comment).
Review the code snippet below:
const base62 = {
charset:
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
encode: (integer) => {
if (integer === 0) {
return 0;
}
let s = [];
while (integer > 0) {
s = [base62.charset[integer % 62], ...s];
integer = Math.floor(integer / 62);
}
return s.join("");
},
decode: (chars) =>
chars
.split("")
.reverse()
.reduce(
(prev, curr, i) => prev + base62.charset.indexOf(curr) * 62 ** i,
0
),
};
let sample = '3YY';
console.log(base62.decode(sample)); // the expected output should be 15312
// When trying to store this in a variable and use slice, it throws an error
let decodeMe = base62.decode(sample).slice(0, 4);
The error message typically reads like this:
VM28714:1 Uncaught TypeError: base62.decode(sample).slice is not a function.
I'm looking for advice on how I can effectively retrieve the output of base62.decode(sample), which is 15312, so that I can integrate it into the script at a later point.