figlet.js icon indicating copy to clipboard operation
figlet.js copied to clipboard

TypeError: Cannot read properties of undefined (reading 'reduce')

Open km19809 opened this issue 4 years ago • 6 comments

I found the same issue(#73), but it is closed.

I examined the code, then found a few things.

  1. In generateFigTextLines, nextFigChars.chars becomes undefined; It is because of breakWord.
  2. When nextFigChars.chars is undefined, joinFigArray throws 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.

km19809 avatar Sep 15 '21 05:09 km19809

will:

var result = {chars: []};

fix the issue?

Do you have reproduction that can be added to the unit tests?

jcubic avatar Sep 15 '21 07:09 jcubic

  1. 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
  2. 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

km19809 avatar Sep 16 '21 00:09 km19809

@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.

jcubic avatar Dec 12 '21 23:12 jcubic

The empty string solution sounds good to me.

patorjk avatar Dec 13 '21 02:12 patorjk

@Lev-Shapiro did you delete your comment?

jcubic avatar Sep 28 '23 19:09 jcubic

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

Lev-Shapiro avatar Sep 29 '23 03:09 Lev-Shapiro