patorjk / figlet.js

A FIG Driver written in JavaScript which aims to fully implement the FIGfont spec.
MIT License
2.61k stars 167 forks source link

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

Open km19809 opened 2 years ago

km19809 commented 2 years ago

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.

jcubic commented 2 years ago

will:

var result = {chars: []};

fix the issue?

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

km19809 commented 2 years ago
  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

jcubic commented 2 years ago

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

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.

patorjk commented 2 years ago

The empty string solution sounds good to me.

jcubic commented 11 months ago

@Lev-Shapiro did you delete your comment?

Lev-Shapiro commented 11 months ago

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