javascript - Cannot code prepend logger correctly -
i have simple function prepend desired string beginning of each line. have working streams, in cases it's convenient using console.log() type of construct.
here function:
// takes string prepend, , stream prepend to:
exports.lp = function (str, strm) { return function prependlog() { var args = array.from(arguments); var hasnonwhitespace = args.some(function (a) { var str = string(a); return str.length > 0 && /\s/g.test(str); }); if (hasnonwhitespace) { strm.write(str); } args.foreach(function (s, i) { string(s).split('\n').foreach(function (s, i) { if (i < 1) { strm.write(s + ' '); } else { strm.write('\n' + str + s); } }); }); strm.write('\n'); }; };
here use:
const {lp} = require('log-prepend'); const fn = lp(' [foobar] ', process.stdout); fn('\n'); fn(); fn(); fn('','',''); fn('log1', 'log2\n3',4,5 + '\n55'); fn('a','b','c');
and here output above:
[foobar] [foobar] log1 log2 [foobar] 34 5 [foobar] 55 [foobar] b c
the problem empty lines no non-whitespace character, works, when include newline character, outputs [foobar] though there nothing on line.
i can't figure out why function doesn't omit [foobar]
lines no non-whitespace. exact, first instance of [foobar]
above mystifying me.
having little trouble following logic here because expecting use index variable first foreach function, when it's using second? renaming initial variables might help.
Comments
Post a Comment