From f487e8ebadb6a2809fdb8dd05af68b56537d371f Mon Sep 17 00:00:00 2001 From: DABH Date: Sun, 9 Dec 2018 17:28:46 -0800 Subject: [PATCH] Fix #244 and #248 --- .npmignore | 1 + lib/extendStringPrototype.js | 22 ++++++++++++++-------- tests/basic-test.js | 8 ++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.npmignore b/.npmignore index fd842b0..4b45fc3 100644 --- a/.npmignore +++ b/.npmignore @@ -2,3 +2,4 @@ /tests/ /.travis.yml /screenshots +*.sw* diff --git a/lib/extendStringPrototype.js b/lib/extendStringPrototype.js index f56bd26..8642ea6 100644 --- a/lib/extendStringPrototype.js +++ b/lib/extendStringPrototype.js @@ -73,16 +73,22 @@ module['exports'] = function() { } else { if (typeof(theme[prop]) === 'string') { colors[prop] = colors[theme[prop]]; + addProperty(prop, function() { + return colors[prop](this); + }); } else { - var tmp = colors[theme[prop][0]]; - for (var t = 1; t < theme[prop].length; t++) { - tmp = tmp[theme[prop][t]]; - } - colors[prop] = tmp; + var themePropApplicator = function(str) { + var ret = str || this; + for (var t = 0; t < theme[prop].length; t++) { + ret = colors[theme[prop][t]](ret); + } + return ret; + }; + addProperty(prop, themePropApplicator); + colors[prop] = function(str){ + return themePropApplicator(str); + }; } - addProperty(prop, function() { - return colors[prop](this); - }); } }); } diff --git a/tests/basic-test.js b/tests/basic-test.js index 8ffb149..fd8aced 100644 --- a/tests/basic-test.js +++ b/tests/basic-test.js @@ -64,10 +64,10 @@ 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' ); + '\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[31m' + '\x1b[3m' + '\x1b[7m' + s + - '\x1b[27m' + '\x1b[23m' + '\x1b[39m' ); \ No newline at end of file + '\x1b[7m' + '\x1b[3m' + '\x1b[31m' + s + + '\x1b[39m' + '\x1b[23m' + '\x1b[27m' );