figlet.js
figlet.js copied to clipboard
TypeError: Cannot read properties of undefined (reading 'reduce')
I found the same issue(#73), but it is closed.
I examined the code, then found a few things.
- In
generateFigTextLines,nextFigChars.charsbecomes undefined; It is because ofbreakWord. - When
nextFigChars.charsis undefined,joinFigArraythrows an Error.
The first thing is a more significant problem. The code looks like this:
function breakWord(figChars, len, opts) {
var result = {};
for (var i = figChars.length; --i;) {
var w = joinFigArray(figChars.slice(0, i), len, opts);
if (figLinesWidth(w) <= opts.width) {
result.outputFigText = w;
if (i < figChars.length) {
result.chars = figChars.slice(i);
} else {
result.chars = [];
}
break;
}
}
return result;
}
The return value, result, is sometimes not fully initialized.
will:
var result = {chars: []};
fix the issue?
Do you have reproduction that can be added to the unit tests?
- It doesn't work...
The new error says:
TypeError: Cannot read properties of undefined (reading 'length') at http://127.0.0.1:5500/figlet.js:719:37 - I am not familiar with unit tests, but I can make the reproduction.
let msg = "Jonathan";
figlet(msg, {
font: "sam3kr",
horizontalLayout: 'default',
verticalLayout: "universal smushing",
width: 8,
whitespaceBreak: true
}, function (err, text) {
//my callback
});
With a width under 15, it throws an error like above.
But with a width of 15, it works!
It seems that a width narrower than the font's width makes the error.
My font, "sam3kr"s maximum width is 14.
I cannot find the exact program flow yet.
It would be great if appropriate error message to the user. ex) Invalid input: A width must be larger than the font's width
@patorjk Just found this issue again, in my browser history. I will try to check why the error happen. If it's my feature I will check what I can do to fix it.
But there is a need for decision, I don't remember what old code did. What the code should do:
- [ ] return one letter per row
- [ ] return empty string
- [ ] throw an error
I myself think (when I use the code) that it should return empty string or throw an error. I can use one or the other in my code. I prefer empty string so I can just wrap with try..catch the return empty string on error.
The empty string solution sounds good to me.
@Lev-Shapiro did you delete your comment?
Yes, I don't know if you still have that problem, for now I'm working on another feature in this repo. If you still have this problem - tell me, so I'd know it's status because 2 years it's quite a long peiod any many stuff could already change