Make stylize() work for non-ASCI styles (#155)

This commit is contained in:
DABH 2019-09-22 16:27:58 -07:00
parent a1407aee04
commit b4d964b514
2 changed files with 16 additions and 1 deletions

View File

@ -62,7 +62,16 @@ var stylize = colors.stylize = function stylize(str, style) {
return str+'';
}
return ansiStyles[style].open + str + ansiStyles[style].close;
var styleMap = ansiStyles[style];
// Stylize should work for non-ANSI styles, too
if(!styleMap && style in colors){
// Style maps like trap operate as functions on strings;
// they don't have properties like open or close.
return colors[style](str);
}
return styleMap.open + str + styleMap.close;
};
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;

View File

@ -30,6 +30,12 @@ assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
assert.ok(s.rainbow);
assert.equal(colors.stylize("foo", "rainbow"), '\u001b[31mf\u001b[39m\u001b[33mo\u001b[39m\u001b[32mo\u001b[39m');
assert.ok(colors.stylize(s, "america"));
assert.ok(colors.stylize(s, "zebra"));
assert.ok(colors.stylize(s, "trap"));
assert.ok(colors.stylize(s, "random"));
aE(s, 'white', 37);
aE(s, 'grey', 90);
aE(s, 'black', 30);