Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
km19809 opened this issue Sep 15, 2021 · 6 comments
Open

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

km19809 opened this issue Sep 15, 2021 · 6 comments
Labels

Comments

@km19809
Copy link

km19809 commented Sep 15, 2021

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
Copy link
Contributor

jcubic commented Sep 15, 2021

will:

var result = {chars: []};

fix the issue?

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

@km19809
Copy link
Author

km19809 commented Sep 16, 2021

  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

@patorjk patorjk added the bug label Sep 16, 2021
@jcubic
Copy link
Contributor

jcubic commented Dec 12, 2021

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

@patorjk
Copy link
Owner

patorjk commented Dec 13, 2021

The empty string solution sounds good to me.

@jcubic
Copy link
Contributor

jcubic commented Sep 28, 2023

@Lev-Shapiro did you delete your comment?

@Lev-Shapiro
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants