Skip to content

Commit

Permalink
Code cleanup, naming updates and comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
opensoars committed Nov 24, 2014
1 parent baa9e01 commit de890e7
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lib/cls.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/** StyleMap
/**
* Style map
*
* @private
* @prop s {map} style types
* @prop c {map} color types
* @prop s {map} Style codes
* @prop c {map} Color codes
*/
var sm = {
var SM = {

s: {
'bold': ['\x1B[1m', '\x1B[22m'],
'italic': ['\x1B[3m', '\x1B[23m'],
'underline': ['\x1B[4m', '\x1B[24m'],
'inverse': ['\x1B[7m', '\x1B[27m'],
'bold': ['\x1B[1m', '\x1B[22m'],
'italic': ['\x1B[3m', '\x1B[23m'],
'underline': ['\x1B[4m', '\x1B[24m'],
'inverse': ['\x1B[7m', '\x1B[27m'],
'strikethrough': ['\x1B[9m', '\x1B[29m']
},

Expand All @@ -29,39 +31,46 @@ var sm = {


/**
* Text styler
*
* @public
* @param t {string|object} Text to style
* @param c {string} opt. Text color
* @param s {string|array} opt. Text style(s)
* @return str {string}
* @param t {string|object} Text to style
* @param [c] {string} Text color
* @param [s] {string|array} Text style(s)
* @return t {string} Styled string
*/
function cls(t, c, s){

var str = '';

if(!t && t !== 0 && t !== false) t = '';
if(typeof c !== 'string') c = '';
// If t is not set, we default to an empty string
if(!t && t !== 0 && t !== false)
t = '';

str = t;
// If c is not set or it's not of type string, default to empty string
if(typeof c !== 'string')
c = '';

if(typeof str === 'object')
str = JSON.stringify(str);
if(typeof t === 'object')
t = JSON.stringify(t);

if(sm.c[c])
str = sm.c[c][0] + str + sm.c[c][1];
// If a color is found matching c, apply it
if(SM.c[c])
t = SM.c[c][0] + t + SM.c[c][1];

// If s is passed, apply style(s)
if(s){
// If we're dealing with an array of style strings, apply them
// else we simply apply a single style
if(s.constructor === Array)
s.forEach(function (sType){
if(sm.s[sType]) str = sm.s[sType][0] + str + sm.s[sType][1]
s.forEach(function (s_type){
if(SM.s[s_type])
t = SM.s[s_type][0] + t + SM.s[s_type][1]
});

else if(typeof s === 'string')
if(sm.s[s])
str = sm.s[s][0] + str + sm.s[s][1];
if(SM.s[s])
t = SM.s[s][0] + t + SM.s[s][1];
}

return str;
return t;
}

module.exports = cls;

0 comments on commit de890e7

Please sign in to comment.