Skip to content

Commit

Permalink
Make stylize() work for non-ASCI styles (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
DABH committed Sep 22, 2019
1 parent a1407ae commit b4d964b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/colors.js
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/basic-test.js
Expand Up @@ -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);
Expand Down

0 comments on commit b4d964b

Please sign in to comment.