From dee3ab049407dc77271e0351b2a81f7273827e4e Mon Sep 17 00:00:00 2001 From: zucher Date: Mon, 25 Jun 2018 14:37:45 +0200 Subject: [PATCH 1/2] Fix #180 - Custom with multi attr failure Fix #180 - Themes can have multiple attributes - but not in safe mode --- lib/extendStringPrototype.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/extendStringPrototype.js b/lib/extendStringPrototype.js index ed608f6..6d6bbfe 100644 --- a/lib/extendStringPrototype.js +++ b/lib/extendStringPrototype.js @@ -73,18 +73,16 @@ module['exports'] = function() { } else { if (typeof(theme[prop]) === 'string') { colors[prop] = colors[theme[prop]]; - addProperty(prop, function() { - return colors[theme[prop]](this); - }); } else { - addProperty(prop, function() { - var ret = this; - for (var t = 0; t < theme[prop].length; t++) { - ret = colors[theme[prop][t]](ret); - } - return ret; - }); + var tmp = colors[theme[prop][0]]; + for (var t = 1; t < theme[prop].length; t++) { + tmp = tmp[theme[prop][t]]; + } + colors[prop] = tmp; } + addProperty(prop, function() { + return colors[prop](this); + }); } }); } From a98deca85f652150c1c5b3e686766020b48c73dd Mon Sep 17 00:00:00 2001 From: Vincent Bailleau Date: Mon, 25 Jun 2018 22:30:44 +0200 Subject: [PATCH 2/2] Add tests for custom entries with multiple values --- tests/basic-test.js | 10 ++++++++++ tests/safe-test.js | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/tests/basic-test.js b/tests/basic-test.js index 4cabf1a..8ffb149 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -61,3 +61,13 @@ assert.equal(typeof ('astring'.red), 'string'); assert.equal(typeof ('astring'.error), 'string'); assert.equal(s, 'string'); + +colors.setTheme({custom: ['blue', 'bold', 'underline']}); +assert.equal(colors.custom(s), + '\x1b[34m' + '\x1b[1m' + '\x1b[4m' + s + + '\x1b[24m' + '\x1b[22m' + '\x1b[39m' ); + +colors.setTheme({custom: ['red', 'italic', 'inverse']}); +assert.equal(colors.custom(s), + '\x1b[31m' + '\x1b[3m' + '\x1b[7m' + s + + '\x1b[27m' + '\x1b[23m' + '\x1b[39m' ); \ No newline at end of file diff --git a/tests/safe-test.js b/tests/safe-test.js index db886eb..b2a091c 100644 --- a/tests/safe-test.js +++ b/tests/safe-test.js @@ -57,3 +57,12 @@ colors.setTheme({error: 'red'}); assert.equal(typeof (colors.red('astring')), 'string'); assert.equal(typeof (colors.error('astring')), 'string'); +colors.setTheme({custom: ['blue', 'bold', 'underline']}); +assert.equal(colors.custom(s), + '\x1b[4m' + '\x1b[1m' + '\x1b[34m' + s + + '\x1b[39m' + '\x1b[22m' + '\x1b[24m' ); + +colors.setTheme({custom: ['red', 'italic', 'inverse']}); +assert.equal(colors.custom(s), + '\x1b[7m' + '\x1b[3m' + '\x1b[31m' + s + + '\x1b[39m' + '\x1b[23m' + '\x1b[27m' );