mirror of
				https://github.com/flynx/colors.js.git
				synced 2025-10-30 19:40:09 +00:00 
			
		
		
		
	[dist] First pass at JSHINT of codebase
This commit is contained in:
		
							parent
							
								
									891da6434c
								
							
						
					
					
						commit
						4b2ae6c5ec
					
				
							
								
								
									
										358
									
								
								colors.js
									
									
									
									
									
								
							
							
						
						
									
										358
									
								
								colors.js
									
									
									
									
									
								
							| @ -45,103 +45,20 @@ if (!isHeadless) { | ||||
| // Prototypes the string object to have additional method calls that add terminal colors
 | ||||
| //
 | ||||
| var addProperty = function (color, func) { | ||||
|   exports[color] = function(str) { | ||||
|   exports[color] = function (str) { | ||||
|     return func.apply(str); | ||||
|   }; | ||||
|   String.prototype.__defineGetter__(color, func); | ||||
| } | ||||
| }; | ||||
| 
 | ||||
| //
 | ||||
| // Iterate through all default styles and colors
 | ||||
| //
 | ||||
| 
 | ||||
| var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta']; | ||||
| x.forEach(function (style) { | ||||
| 
 | ||||
|   // __defineGetter__ at the least works in more browsers
 | ||||
|   // http://robertnyman.com/javascript/javascript-getters-setters.html
 | ||||
|   // Object.defineProperty only works in Chrome
 | ||||
|   addProperty(style, function () { | ||||
|     return stylize(this, style); | ||||
|   }); | ||||
| }); | ||||
| 
 | ||||
| function sequencer(map) { | ||||
|   return function () { | ||||
|     if (!isHeadless) { | ||||
|       return this.replace(/( )/, '$1'); | ||||
|     } | ||||
|     var exploded = this.split(""); | ||||
|     var i = 0; | ||||
|     exploded = exploded.map(map); | ||||
|     return exploded.join(""); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| var rainbowMap = (function () { | ||||
|   var rainbowColors = ['red','yellow','green','blue','magenta']; //RoY G BiV
 | ||||
|   return function (letter, i, exploded) { | ||||
|     if (letter == " ") { | ||||
|       return letter; | ||||
|     } else { | ||||
|       return stylize(letter, rainbowColors[i++ % rainbowColors.length]); | ||||
|     } | ||||
|   } | ||||
| })(); | ||||
| 
 | ||||
| exports.themes = {}; | ||||
| 
 | ||||
| exports.addSequencer = function (name, map) { | ||||
|   addProperty(name, sequencer(map)); | ||||
| } | ||||
| 
 | ||||
| exports.addSequencer('rainbow', rainbowMap); | ||||
| exports.addSequencer('zebra', function (letter, i, exploded) { | ||||
|   return i % 2 === 0 ? letter : letter.inverse; | ||||
| }); | ||||
| 
 | ||||
| exports.setTheme = function (theme) { | ||||
|   if (typeof theme === 'string') { | ||||
|     try { | ||||
|       exports.themes[theme] = require(theme); | ||||
|       applyTheme(exports.themes[theme]); | ||||
|       return exports.themes[theme]; | ||||
|     } catch (err) { | ||||
|       return err; | ||||
|     } | ||||
|   } else { | ||||
|     applyTheme(theme); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| function applyTheme (theme) { | ||||
| 
 | ||||
|   //
 | ||||
|   // Remark: This is a list of methods that exist
 | ||||
|   // on String that you should not overwrite.
 | ||||
|   //
 | ||||
|   var stringPrototypeBlacklist = [ | ||||
|     '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor', | ||||
|     'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt', | ||||
|     'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring', | ||||
|     'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight' | ||||
|   ]; | ||||
| 
 | ||||
|   Object.keys(theme).forEach(function(prop){ | ||||
|     if (stringPrototypeBlacklist.indexOf(prop) !== -1) { | ||||
|       console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name'); | ||||
|     } else { | ||||
|       addProperty(prop, function(){ | ||||
|         return exports[theme[prop]](this); | ||||
|       }); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
| 
 | ||||
| function stylize(str, style) { | ||||
| 
 | ||||
|   if (exports.mode == 'console') { | ||||
|     var styles = { | ||||
|   var styles; | ||||
| 
 | ||||
|   if (exports.mode === 'console') { | ||||
|     styles = { | ||||
|       //styles
 | ||||
|       'bold'      : ['\033[1m',  '\033[22m'], | ||||
|       'italic'    : ['\033[3m',  '\033[23m'], | ||||
| @ -159,8 +76,8 @@ function stylize(str, style) { | ||||
|       'red'       : ['\033[31m', '\033[39m'], | ||||
|       'yellow'    : ['\033[33m', '\033[39m'] | ||||
|     }; | ||||
|   } else if (exports.mode == 'browser') { | ||||
|     var styles = { | ||||
|   } else if (exports.mode === 'browser') { | ||||
|     styles = { | ||||
|       //styles
 | ||||
|       'bold'      : ['<b>',  '</b>'], | ||||
|       'italic'    : ['<i>',  '</i>'], | ||||
| @ -178,123 +95,210 @@ function stylize(str, style) { | ||||
|       'red'       : ['<span style="color:red;">',     '</span>'], | ||||
|       'yellow'    : ['<span style="color:yellow;">',  '</span>'] | ||||
|     }; | ||||
|   } else if (exports.mode == 'none') { | ||||
|   } else if (exports.mode === 'none') { | ||||
|     return str; | ||||
|   } else { | ||||
|     console.log('unsupported mode, try "browser", "console" or "none"'); | ||||
|   } | ||||
|   return styles[style][0] + str + styles[style][1]; | ||||
| } | ||||
| 
 | ||||
| function applyTheme(theme) { | ||||
| 
 | ||||
|   //
 | ||||
|   // Remark: This is a list of methods that exist
 | ||||
|   // on String that you should not overwrite.
 | ||||
|   //
 | ||||
|   var stringPrototypeBlacklist = [ | ||||
|     '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor', | ||||
|     'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt', | ||||
|     'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring', | ||||
|     'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight' | ||||
|   ]; | ||||
| 
 | ||||
|   Object.keys(theme).forEach(function (prop) { | ||||
|     if (stringPrototypeBlacklist.indexOf(prop) !== -1) { | ||||
|       console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name'); | ||||
|     } else { | ||||
|       addProperty(prop, function () { | ||||
|         return exports[theme[prop]](this); | ||||
|       }); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| //
 | ||||
| // Iterate through all default styles and colors
 | ||||
| //
 | ||||
| var x = ['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta']; | ||||
| x.forEach(function (style) { | ||||
| 
 | ||||
|   // __defineGetter__ at the least works in more browsers
 | ||||
|   // http://robertnyman.com/javascript/javascript-getters-setters.html
 | ||||
|   // Object.defineProperty only works in Chrome
 | ||||
|   addProperty(style, function () { | ||||
|     return stylize(this, style); | ||||
|   }); | ||||
| }); | ||||
| 
 | ||||
| function sequencer(map) { | ||||
|   return function () { | ||||
|     if (!isHeadless) { | ||||
|       return this.replace(/( )/, '$1'); | ||||
|     } | ||||
|     var exploded = this.split(""), i = 0; | ||||
|     exploded = exploded.map(map); | ||||
|     return exploded.join(""); | ||||
|   }; | ||||
| } | ||||
| 
 | ||||
| var rainbowMap = (function () { | ||||
|   var rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']; //RoY G BiV
 | ||||
|   return function (letter, i, exploded) { | ||||
|     if (letter === " ") { | ||||
|       return letter; | ||||
|     } else { | ||||
|       return stylize(letter, rainbowColors[i++ % rainbowColors.length]); | ||||
|     } | ||||
|   }; | ||||
| })(); | ||||
| 
 | ||||
| exports.themes = {}; | ||||
| 
 | ||||
| exports.addSequencer = function (name, map) { | ||||
|   addProperty(name, sequencer(map)); | ||||
| }; | ||||
| 
 | ||||
| // don't summon zalgo
 | ||||
| addProperty('zalgo', function () { | ||||
|   return zalgo(this); | ||||
| exports.addSequencer('rainbow', rainbowMap); | ||||
| exports.addSequencer('zebra', function (letter, i, exploded) { | ||||
|   return i % 2 === 0 ? letter : letter.inverse; | ||||
| }); | ||||
| 
 | ||||
| exports.setTheme = function (theme) { | ||||
|   if (typeof theme === 'string') { | ||||
|     try { | ||||
|       exports.themes[theme] = require(theme); | ||||
|       applyTheme(exports.themes[theme]); | ||||
|       return exports.themes[theme]; | ||||
|     } catch (err) { | ||||
|       console.log(err); | ||||
|       return err; | ||||
|     } | ||||
|   } else { | ||||
|     applyTheme(theme); | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| addProperty('stripColors', function () { | ||||
|   return ("" + this).replace(/\u001b\[\d+m/g, ''); | ||||
| }); | ||||
| 
 | ||||
| // please no
 | ||||
| function zalgo(text, options) { | ||||
|   var soul = { | ||||
|     "up" : [ | ||||
|       '̍','̎','̄','̅', | ||||
|       '̿','̑','̆','̐', | ||||
|       '͒','͗','͑','̇', | ||||
|       '̈','̊','͂','̓', | ||||
|       '̈','͊','͋','͌', | ||||
|       '̃','̂','̌','͐', | ||||
|       '̀','́','̋','̏', | ||||
|       '̒','̓','̔','̽', | ||||
|       '̉','ͣ','ͤ','ͥ', | ||||
|       'ͦ','ͧ','ͨ','ͩ', | ||||
|       'ͪ','ͫ','ͬ','ͭ', | ||||
|       'ͮ','ͯ','̾','͛', | ||||
|       '͆','̚' | ||||
|       ], | ||||
|       '̍', '̎', '̄', '̅', | ||||
|       '̿', '̑', '̆', '̐', | ||||
|       '͒', '͗', '͑', '̇', | ||||
|       '̈', '̊', '͂', '̓', | ||||
|       '̈', '͊', '͋', '͌', | ||||
|       '̃', '̂', '̌', '͐', | ||||
|       '̀', '́', '̋', '̏', | ||||
|       '̒', '̓', '̔', '̽', | ||||
|       '̉', 'ͣ', 'ͤ', 'ͥ', | ||||
|       'ͦ', 'ͧ', 'ͨ', 'ͩ', | ||||
|       'ͪ', 'ͫ', 'ͬ', 'ͭ', | ||||
|       'ͮ', 'ͯ', '̾', '͛', | ||||
|       '͆', '̚' | ||||
|     ], | ||||
|     "down" : [ | ||||
|       '̖','̗','̘','̙', | ||||
|       '̜','̝','̞','̟', | ||||
|       '̠','̤','̥','̦', | ||||
|       '̩','̪','̫','̬', | ||||
|       '̭','̮','̯','̰', | ||||
|       '̱','̲','̳','̹', | ||||
|       '̺','̻','̼','ͅ', | ||||
|       '͇','͈','͉','͍', | ||||
|       '͎','͓','͔','͕', | ||||
|       '͖','͙','͚','̣' | ||||
|       ], | ||||
|       '̖', '̗', '̘', '̙', | ||||
|       '̜', '̝', '̞', '̟', | ||||
|       '̠', '̤', '̥', '̦', | ||||
|       '̩', '̪', '̫', '̬', | ||||
|       '̭', '̮', '̯', '̰', | ||||
|       '̱', '̲', '̳', '̹', | ||||
|       '̺', '̻', '̼', 'ͅ', | ||||
|       '͇', '͈', '͉', '͍', | ||||
|       '͎', '͓', '͔', '͕', | ||||
|       '͖', '͙', '͚', '̣' | ||||
|     ], | ||||
|     "mid" : [ | ||||
|       '̕','̛','̀','́', | ||||
|       '͘','̡','̢','̧', | ||||
|       '̨','̴','̵','̶', | ||||
|       '͜','͝','͞', | ||||
|       '͟','͠','͢','̸', | ||||
|       '̷','͡',' ҉' | ||||
|       ] | ||||
|       '̕', '̛', '̀', '́', | ||||
|       '͘', '̡', '̢', '̧', | ||||
|       '̨', '̴', '̵', '̶', | ||||
|       '͜', '͝', '͞', | ||||
|       '͟', '͠', '͢', '̸', | ||||
|       '̷', '͡', ' ҉' | ||||
|     ] | ||||
|   }, | ||||
|   all = [].concat(soul.up, soul.down, soul.mid), | ||||
|   zalgo = {}; | ||||
| 
 | ||||
|   function randomNumber(range) { | ||||
|     r = Math.floor(Math.random()*range); | ||||
|     var r = Math.floor(Math.random() * range); | ||||
|     return r; | ||||
|   }; | ||||
|   } | ||||
| 
 | ||||
|   function is_char(character) { | ||||
|     var bool = false; | ||||
|     all.filter(function(i){ | ||||
|      bool = (i == character); | ||||
|     all.filter(function (i) { | ||||
|       bool = (i === character); | ||||
|     }); | ||||
|     return bool; | ||||
|   } | ||||
| 
 | ||||
|   function heComes(text, options){ | ||||
|       result = ''; | ||||
|       options = options || {}; | ||||
|       options["up"] = options["up"] || true; | ||||
|       options["mid"] = options["mid"] || true; | ||||
|       options["down"] = options["down"] || true; | ||||
|       options["size"] = options["size"] || "maxi"; | ||||
|       var counts; | ||||
|       text = text.split(''); | ||||
|        for(var l in text){ | ||||
|          if(is_char(l)) { continue; } | ||||
|          result = result + text[l]; | ||||
|   function heComes(text, options) { | ||||
|     var result = '', counts, l; | ||||
|     options = options || {}; | ||||
|     options["up"] = options["up"] || true; | ||||
|     options["mid"] = options["mid"] || true; | ||||
|     options["down"] = options["down"] || true; | ||||
|     options["size"] = options["size"] || "maxi"; | ||||
|     text = text.split(''); | ||||
|     for (l in text) { | ||||
|       if (is_char(l)) { | ||||
|         continue; | ||||
|       } | ||||
|       result = result + text[l]; | ||||
|       counts = {"up" : 0, "down" : 0, "mid" : 0}; | ||||
|       switch (options.size) { | ||||
|         case 'mini': | ||||
|           counts.up = randomNumber(8); | ||||
|           counts.min = randomNumber(2); | ||||
|           counts.down = randomNumber(8); | ||||
|         break; | ||||
|         case 'maxi': | ||||
|           counts.up = randomNumber(16) + 3; | ||||
|           counts.min = randomNumber(4) + 1; | ||||
|           counts.down = randomNumber(64) + 3; | ||||
|         break; | ||||
|         default: | ||||
|           counts.up = randomNumber(8) + 1; | ||||
|           counts.mid = randomNumber(6) / 2; | ||||
|           counts.down = randomNumber(8) + 1; | ||||
|         break; | ||||
|       } | ||||
| 
 | ||||
|         counts = {"up" : 0, "down" : 0, "mid" : 0}; | ||||
| 
 | ||||
|         switch(options.size) { | ||||
|           case 'mini': | ||||
|             counts.up = randomNumber(8); | ||||
|             counts.min= randomNumber(2); | ||||
|             counts.down = randomNumber(8); | ||||
|           break; | ||||
|           case 'maxi': | ||||
|             counts.up = randomNumber(16) + 3; | ||||
|             counts.min = randomNumber(4) + 1; | ||||
|             counts.down = randomNumber(64) + 3; | ||||
|           break; | ||||
|           default: | ||||
|             counts.up = randomNumber(8) + 1; | ||||
|             counts.mid = randomNumber(6) / 2; | ||||
|             counts.down= randomNumber(8) + 1; | ||||
|           break; | ||||
|         } | ||||
| 
 | ||||
|         var arr = ["up", "mid", "down"]; | ||||
|         for(var d in arr){ | ||||
|           var index = arr[d]; | ||||
|           for (var i = 0 ; i <= counts[index]; i++) | ||||
|           { | ||||
|             if(options[index]) { | ||||
|                 result = result + soul[index][randomNumber(soul[index].length)]; | ||||
|               } | ||||
|             } | ||||
|       var arr = ["up", "mid", "down"]; | ||||
|       for (var d in arr) { | ||||
|         var index = arr[d]; | ||||
|         for (var i = 0 ; i <= counts[index]; i++) { | ||||
|           if (options[index]) { | ||||
|             result = result + soul[index][randomNumber(soul[index].length)]; | ||||
|           } | ||||
|         } | ||||
|       return result; | ||||
|   }; | ||||
|       } | ||||
|     } | ||||
|     return result; | ||||
|   } | ||||
|   return heComes(text); | ||||
| } | ||||
| 
 | ||||
| addProperty('stripColors', function() { | ||||
|   return ("" + this).replace(/\u001b\[\d+m/g,''); | ||||
| }); | ||||
| 
 | ||||
| // don't summon zalgo
 | ||||
| addProperty('zalgo', function () { | ||||
|   return zalgo(this); | ||||
| }); | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user