patorjk / figlet.js

A FIG Driver written in JavaScript which aims to fully implement the FIGfont spec.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot read property 'reduce' of undefined

jcubic opened this issue · comments

Just got this error when using a custom font in NodeJS. Even the example 'Ghost' font doesn't work. And preloadFonts use fetch so it's for browser only.

I have code like this:

const { promisify } = require('util');
const figlet = require('figlet');
const render = promisify(figlet.text);

const config = {
    font: 'Colossal',
    width: 80,
    whitespaceBreak: true
};

module.exports = async () => {
    const text = 'Jakub T. Jankiewicz';
    return {
        wrap: await render(text, {...config, width: 12}),
        big: await  render(text, config)
    };
};

The issue with probably with line wrapping.

I've found the error in the code. I've tried to reproduce it, to first write unit tests, but it seems that it works fine. I have no idea why it w throwing an error in first place.

I faced the same issue.
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.