Comparing version 0.1.0 to 0.2.0
var codes = require('./codes'); | ||
module.exports = function() { | ||
// make passed arguments into an array | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
// total reset, to make sure no styles bleed over | ||
// specifies default foreground, default background, reset | ||
var reset = '\x1b[39;49;0m'; | ||
// this will be the output string we will build | ||
var output = ''; | ||
// captures text input between double handlebars | ||
var handlebar_regex = /\{\{(.*?)\}\}/g; | ||
function replacement(match, p1) { | ||
return getANSI(p1); | ||
} | ||
// iterate over passed args | ||
for (var o = 0; o < args.length; o++) { | ||
// given the text the regex pulls out | ||
function getANSI(styleString) { | ||
// if an argument is an array, it will be interpreted as a list of codes | ||
if (args[o] instanceof Array) { | ||
// make an array of input items that are separated by commas | ||
var styles = styleString.split(','); | ||
// add the start of an ansi code | ||
output += '\x1b['; | ||
// a list to stage items to be combined later | ||
var list = []; | ||
// 'group' will be an array of ansi code numbers as strings | ||
var group = []; | ||
// iterate over the array of styles | ||
for (var s = 0; s < styles.length; s++) { | ||
// iterate over the list of codes | ||
for (var i = 0; i < args[o].length; i++) { | ||
// if the item is a number, add it directly to the list | ||
if (/^[0-9]+$/.test(styles[s])) { | ||
list.push(styles[s]); | ||
// if a code is a number, convert it to a string and put it in the group | ||
if (typeof args[o][i] === 'number') { | ||
group.push(args[o][i].toString(10)); | ||
// if the item is in the list of codes in the json file, get its value | ||
// and add it to the list | ||
} else if (typeof codes[styles[s]] !== 'undefined') { | ||
list.push(codes[styles[s]]); | ||
// otherwise if it's a string that is a key in the json file then | ||
// get its code value from the json file and put it in the group | ||
} else if (typeof args[o][i] === 'string' && typeof codes[args[o][i]] !== 'undefined') { | ||
group.push(codes[args[o][i]]); | ||
// if these conditions aren't met, simply ignore this value and continue | ||
} else { | ||
continue; | ||
} | ||
} | ||
// otherwise if no match is found simply ignore the input and continue | ||
} else { | ||
continue; | ||
} | ||
} | ||
// wrap the semicolon-separated list in ansi grossness | ||
return '\x1b[' + list.join(';') + 'm'; | ||
} | ||
// append the group as a string joining codes with a semicolon | ||
output += group.join(';'); | ||
module.exports = function() { | ||
// make passed arguments into an array | ||
var args = Array.prototype.slice.call(arguments, 0); | ||
// finally, append 'm' which ends the ansi code | ||
output += 'm'; | ||
// this will be the output string we will build | ||
var output = ''; | ||
// end of (if (args[o] instanceof array)) | ||
// iterate over passed strings | ||
for (var i = 0; i < args.length; i++) { | ||
var current = args[i].replace(handlebar_regex, replacement); | ||
// add formatted string to output | ||
output += current; | ||
// if an argument is a string, simply append it to the output | ||
} else if (typeof args[o] === 'string'){ | ||
output += args[o]; | ||
// if an argument is neither a string nor array, ignore it | ||
} else { | ||
continue; | ||
// reset if styles were added | ||
if (current !== args[i]) { | ||
output += reset; | ||
} | ||
} | ||
// total reset, to make sure no styles bleed over | ||
// specifies default foreground, default background, reset | ||
output += '\x1b[39;49;0m'; | ||
// return the complete output string | ||
return output; | ||
}; |
{ | ||
"name": "colorado", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "ridiculously simple console string styling", | ||
@@ -5,0 +5,0 @@ "main": "colorado.js", |
@@ -1,2 +0,2 @@ | ||
#colorado | ||
#colorado [![NPM version](https://badge.fury.io/js/colorado.png)](http://badge.fury.io/js/colorado) | ||
@@ -15,3 +15,3 @@ ridiculously simple console string styling in [nodejs](http://nodejs.org). | ||
``` | ||
`colorado` is a function that takes any number of arguments. | ||
`colorado` is a function that takes any number of strings as arguments. | ||
@@ -23,6 +23,6 @@ Pass in a string to add plain text: | ||
``` | ||
Pass it an array of styles and it will turn them into ANSI codes: | ||
Use double handlebars to specify styles (comma separated): | ||
```javascript | ||
var dangerous = colorado(['red','bold','blink'], 'danger!'); | ||
var dangerous = colorado('{{red,bold,blink}}danger!'); | ||
console.log(dangerous); // made you look | ||
@@ -33,14 +33,14 @@ ``` | ||
```javascript | ||
var justAsDangerous = colorado([31,1,5], 'danger!'); | ||
var justAsDangerous = colorado('{{31,1,5}}danger!'); | ||
console.log(justAsDangerous); // remarkably similar | ||
``` | ||
Easily inline styles by passing several arguments: | ||
Styles are reset between each string passed: | ||
```javascript | ||
var cuteStory = colorado( | ||
'I was reading all this boring text and then ', | ||
['bold', 'yellow', 'redBG'], | ||
'BAM!', | ||
[22,36,49], ' everything looked cyan to me...' | ||
'I was reading all this boring text and then... ', | ||
'{{bold,yellow,redBG}} BAM! ', | ||
'{{36}} everything looked cyan to me...' | ||
); | ||
console.log(cuteStory); // very anecdotal | ||
``` |
@@ -5,6 +5,6 @@ var colorado = require('../colorado'); | ||
test.ok(colorado); | ||
test.equal(colorado(['red'], 'test'), '\x1b[31mtest\x1b[39;49;0m'); | ||
test.equal(colorado(['blink', 'cyan'], 'test', ['green'], 'again'), '\x1b[5;36mtest\x1b[32magain\x1b[39;49;0m'); | ||
test.equal(colorado([31,1], 'warning'), '\x1b[31;1mwarning\x1b[39;49;0m'); | ||
test.equal(colorado('{{red}}test'), '\x1b[31mtest\x1b[39;49;0m'); | ||
test.equal(colorado('{{blink,cyan}}test{{green}}again'), '\x1b[5;36mtest\x1b[32magain\x1b[39;49;0m'); | ||
test.equal(colorado('{{31,1}}warning'), '\x1b[31;1mwarning\x1b[39;49;0m'); | ||
test.done(); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122
5388