irc-colors
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -1,2 +0,2 @@ | ||
var colors = { | ||
const colors = { | ||
'00': ['white'], | ||
@@ -20,3 +20,3 @@ '01': ['black'], | ||
var styles = { | ||
const styles = { | ||
normal : '\x0F', | ||
@@ -28,10 +28,14 @@ underline : '\x1F', | ||
const styleChars = {}; | ||
Object.keys(styles).forEach(function(key) { | ||
styleChars[styles[key]] = true; | ||
}); | ||
// Coloring character. | ||
var c = '\x03'; | ||
var pos2 = c.length + 2; | ||
var zero = styles.bold + styles.bold; | ||
const c = '\x03'; | ||
const zero = styles.bold + styles.bold; | ||
var allColors = { | ||
fg: [], bg: [], styles: Object.keys(styles), | ||
const allColors = { | ||
fg: [], bg: [], styles: Object.keys(styles), custom: [], extras: [], | ||
}; | ||
@@ -48,9 +52,9 @@ | ||
var bg = function(str) { | ||
var pos = str.indexOf(c); | ||
if (pos !== 0) { | ||
return c + '01,' + code + str + c; | ||
} else { | ||
return (str.substr(pos, pos2)) + ',' + code + (str.substr(pos2 + 2)); | ||
} | ||
}; | ||
var pos = str.indexOf(c); | ||
if (pos !== 0) { | ||
return c + '01,' + code + str + c; | ||
} else { | ||
return (str.substr(pos, 3)) + ',' + code + (str.substr(5)); | ||
} | ||
}; | ||
@@ -65,3 +69,2 @@ colors[code].forEach(function(color) { | ||
// Style functions. | ||
@@ -75,8 +78,8 @@ Object.keys(styles).forEach(function(style) { | ||
// Extras. | ||
var extras = { | ||
// Some custom helpers. | ||
const custom = { | ||
rainbow: function(str, colorArr) { | ||
var rainbow = ['red', 'olive', 'yellow', 'green', | ||
'blue', 'navy', 'violet']; | ||
var rainbow = [ | ||
'red', 'olive', 'yellow', 'green', 'blue', 'navy', 'violet' | ||
]; | ||
colorArr = colorArr || rainbow; | ||
@@ -93,3 +96,11 @@ var l = colorArr.length; | ||
}, | ||
}; | ||
Object.keys(custom).forEach(function(extra) { | ||
allColors.custom.push(extra); | ||
exports[extra] = custom[extra]; | ||
}); | ||
// Extras. | ||
const extras = { | ||
stripColors: function(str) { | ||
@@ -100,3 +111,34 @@ return str.replace(/\x03\d{0,2}(,\d{0,2}|\x02\x02)?/g, ''); | ||
stripStyle: function(str) { | ||
return str.replace(/([\x0F\x02\x16\x1F])(.+)\1/g, '$2'); | ||
var path = []; | ||
var i, len, char; | ||
for (i = 0, len = str.length; i < len; i++) { | ||
char = str[i]; | ||
if (styleChars[char] || char === c) { | ||
var lastChar = path[path.length - 1]; | ||
if (lastChar && lastChar[0] === char) { | ||
var p0 = lastChar[1]; | ||
// Don't strip out styles with no characters inbetween. | ||
// And don't strip out color codes. | ||
if (i - p0 > 1 && char !== c) { | ||
str = str.slice(0, p0) + str.slice(p0 + 1, i) + str.slice(i + 1); | ||
i -= 2; | ||
} | ||
path.pop(); | ||
} else { | ||
path.push([str[i], i]); | ||
} | ||
} | ||
} | ||
// Remove any unmatching style characterss. | ||
// Traverse list backwards to make removing less complicated. | ||
for (i = path.length - 1; i >= 0; i--) { | ||
char = path[i]; | ||
if (char[0] !== c) { | ||
var pos = char[1]; | ||
str = str.slice(0, pos) + str.slice(pos + 1); | ||
} | ||
} | ||
return str; | ||
}, | ||
@@ -110,2 +152,3 @@ | ||
Object.keys(extras).forEach(function(extra) { | ||
allColors.extras.push(extra); | ||
exports[extra] = extras[extra]; | ||
@@ -119,6 +162,9 @@ }); | ||
allColors[type].forEach(function(color) { | ||
fn.__defineGetter__(color, function() { | ||
var f = function(str) { return exports[color](fn(str)); }; | ||
addGetters(f, [].concat(types, type)); | ||
return f; | ||
if (fn[color] != null) { return; } | ||
Object.defineProperty(fn, color, { | ||
get: function() { | ||
var f = function(str) { return exports[color](fn(str)); }; | ||
addGetters(f, [].concat(types, type)); | ||
return f; | ||
}, | ||
}); | ||
@@ -152,7 +198,2 @@ }); | ||
} | ||
Object.keys(extras).forEach(function(extra) { | ||
irc[extra] = function() { | ||
return extras[extra](str); }; | ||
}); | ||
}; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"repository": { | ||
@@ -17,2 +17,5 @@ "type": "git", | ||
"main": "./lib/irc-colors.js", | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
@@ -19,0 +22,0 @@ "test": "istanbul cover vows -- --spec test/*-test.js" |
@@ -14,3 +14,3 @@ # irc.colors.js | ||
```javascript | ||
var c = require('irc-colors'); | ||
const c = require('irc-colors'); | ||
... | ||
@@ -68,3 +68,3 @@ ircbot.say('#chan', c.blue('hello everyone')); // prints blue text | ||
```js | ||
var c = require('irc-colors'); | ||
const c = require('irc-colors'); | ||
@@ -71,0 +71,0 @@ ircbot.on('message', function(from, message) { |
8427
165
4