Skip to content

Commit b4d964b

Browse files
committed
Make stylize() work for non-ASCI styles (#155)
1 parent a1407ae commit b4d964b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/colors.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ var stylize = colors.stylize = function stylize(str, style) {
6262
return str+'';
6363
}
6464

65-
return ansiStyles[style].open + str + ansiStyles[style].close;
65+
var styleMap = ansiStyles[style];
66+
67+
// Stylize should work for non-ANSI styles, too
68+
if(!styleMap && style in colors){
69+
// Style maps like trap operate as functions on strings;
70+
// they don't have properties like open or close.
71+
return colors[style](str);
72+
}
73+
74+
return styleMap.open + str + styleMap.close;
6675
};
6776

6877
var matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;

tests/basic-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ assert.equal(s.inverse, '\x1B[7m' + s + '\x1B[27m');
3030

3131
assert.ok(s.rainbow);
3232

33+
assert.equal(colors.stylize("foo", "rainbow"), '\u001b[31mf\u001b[39m\u001b[33mo\u001b[39m\u001b[32mo\u001b[39m');
34+
assert.ok(colors.stylize(s, "america"));
35+
assert.ok(colors.stylize(s, "zebra"));
36+
assert.ok(colors.stylize(s, "trap"));
37+
assert.ok(colors.stylize(s, "random"));
38+
3339
aE(s, 'white', 37);
3440
aE(s, 'grey', 90);
3541
aE(s, 'black', 30);

0 commit comments

Comments
 (0)