mirror of
				https://github.com/flynx/colors.js.git
				synced 2025-10-31 03:50:11 +00:00 
			
		
		
		
	[dist] First pass at JSHINT of codebase
This commit is contained in:
		
							parent
							
								
									891da6434c
								
							
						
					
					
						commit
						4b2ae6c5ec
					
				
							
								
								
									
										356
									
								
								colors.js
									
									
									
									
									
								
							
							
						
						
									
										356
									
								
								colors.js
									
									
									
									
									
								
							| @ -45,103 +45,20 @@ if (!isHeadless) { | |||||||
| // Prototypes the string object to have additional method calls that add terminal colors
 | // Prototypes the string object to have additional method calls that add terminal colors
 | ||||||
| //
 | //
 | ||||||
| var addProperty = function (color, func) { | var addProperty = function (color, func) { | ||||||
|   exports[color] = function(str) { |   exports[color] = function (str) { | ||||||
|     return func.apply(str); |     return func.apply(str); | ||||||
|   }; |   }; | ||||||
|   String.prototype.__defineGetter__(color, func); |   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) { | function stylize(str, style) { | ||||||
| 
 | 
 | ||||||
|   if (exports.mode == 'console') { |   var styles; | ||||||
|     var styles = { | 
 | ||||||
|  |   if (exports.mode === 'console') { | ||||||
|  |     styles = { | ||||||
|       //styles
 |       //styles
 | ||||||
|       'bold'      : ['\033[1m',  '\033[22m'], |       'bold'      : ['\033[1m',  '\033[22m'], | ||||||
|       'italic'    : ['\033[3m',  '\033[23m'], |       'italic'    : ['\033[3m',  '\033[23m'], | ||||||
| @ -159,8 +76,8 @@ function stylize(str, style) { | |||||||
|       'red'       : ['\033[31m', '\033[39m'], |       'red'       : ['\033[31m', '\033[39m'], | ||||||
|       'yellow'    : ['\033[33m', '\033[39m'] |       'yellow'    : ['\033[33m', '\033[39m'] | ||||||
|     }; |     }; | ||||||
|   } else if (exports.mode == 'browser') { |   } else if (exports.mode === 'browser') { | ||||||
|     var styles = { |     styles = { | ||||||
|       //styles
 |       //styles
 | ||||||
|       'bold'      : ['<b>',  '</b>'], |       'bold'      : ['<b>',  '</b>'], | ||||||
|       'italic'    : ['<i>',  '</i>'], |       'italic'    : ['<i>',  '</i>'], | ||||||
| @ -178,123 +95,210 @@ function stylize(str, style) { | |||||||
|       'red'       : ['<span style="color:red;">',     '</span>'], |       'red'       : ['<span style="color:red;">',     '</span>'], | ||||||
|       'yellow'    : ['<span style="color:yellow;">',  '</span>'] |       'yellow'    : ['<span style="color:yellow;">',  '</span>'] | ||||||
|     }; |     }; | ||||||
|   } else if (exports.mode == 'none') { |   } else if (exports.mode === 'none') { | ||||||
|     return str; |     return str; | ||||||
|   } else { |   } else { | ||||||
|     console.log('unsupported mode, try "browser", "console" or "none"'); |     console.log('unsupported mode, try "browser", "console" or "none"'); | ||||||
|   } |   } | ||||||
|   return styles[style][0] + str + styles[style][1]; |   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
 | exports.addSequencer('rainbow', rainbowMap); | ||||||
| addProperty('zalgo', function () { | exports.addSequencer('zebra', function (letter, i, exploded) { | ||||||
|   return zalgo(this); |   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
 | // please no
 | ||||||
| function zalgo(text, options) { | function zalgo(text, options) { | ||||||
|   var soul = { |   var soul = { | ||||||
|     "up" : [ |     "up" : [ | ||||||
|       '̍','̎','̄','̅', |       '̍', '̎', '̄', '̅', | ||||||
|       '̿','̑','̆','̐', |       '̿', '̑', '̆', '̐', | ||||||
|       '͒','͗','͑','̇', |       '͒', '͗', '͑', '̇', | ||||||
|       '̈','̊','͂','̓', |       '̈', '̊', '͂', '̓', | ||||||
|       '̈','͊','͋','͌', |       '̈', '͊', '͋', '͌', | ||||||
|       '̃','̂','̌','͐', |       '̃', '̂', '̌', '͐', | ||||||
|       '̀','́','̋','̏', |       '̀', '́', '̋', '̏', | ||||||
|       '̒','̓','̔','̽', |       '̒', '̓', '̔', '̽', | ||||||
|       '̉','ͣ','ͤ','ͥ', |       '̉', 'ͣ', 'ͤ', 'ͥ', | ||||||
|       'ͦ','ͧ','ͨ','ͩ', |       'ͦ', 'ͧ', 'ͨ', 'ͩ', | ||||||
|       'ͪ','ͫ','ͬ','ͭ', |       'ͪ', 'ͫ', 'ͬ', 'ͭ', | ||||||
|       'ͮ','ͯ','̾','͛', |       'ͮ', 'ͯ', '̾', '͛', | ||||||
|       '͆','̚' |       '͆', '̚' | ||||||
|       ], |     ], | ||||||
|     "down" : [ |     "down" : [ | ||||||
|       '̖','̗','̘','̙', |       '̖', '̗', '̘', '̙', | ||||||
|       '̜','̝','̞','̟', |       '̜', '̝', '̞', '̟', | ||||||
|       '̠','̤','̥','̦', |       '̠', '̤', '̥', '̦', | ||||||
|       '̩','̪','̫','̬', |       '̩', '̪', '̫', '̬', | ||||||
|       '̭','̮','̯','̰', |       '̭', '̮', '̯', '̰', | ||||||
|       '̱','̲','̳','̹', |       '̱', '̲', '̳', '̹', | ||||||
|       '̺','̻','̼','ͅ', |       '̺', '̻', '̼', 'ͅ', | ||||||
|       '͇','͈','͉','͍', |       '͇', '͈', '͉', '͍', | ||||||
|       '͎','͓','͔','͕', |       '͎', '͓', '͔', '͕', | ||||||
|       '͖','͙','͚','̣' |       '͖', '͙', '͚', '̣' | ||||||
|       ], |     ], | ||||||
|     "mid" : [ |     "mid" : [ | ||||||
|       '̕','̛','̀','́', |       '̕', '̛', '̀', '́', | ||||||
|       '͘','̡','̢','̧', |       '͘', '̡', '̢', '̧', | ||||||
|       '̨','̴','̵','̶', |       '̨', '̴', '̵', '̶', | ||||||
|       '͜','͝','͞', |       '͜', '͝', '͞', | ||||||
|       '͟','͠','͢','̸', |       '͟', '͠', '͢', '̸', | ||||||
|       '̷','͡',' ҉' |       '̷', '͡', ' ҉' | ||||||
|       ] |     ] | ||||||
|   }, |   }, | ||||||
|   all = [].concat(soul.up, soul.down, soul.mid), |   all = [].concat(soul.up, soul.down, soul.mid), | ||||||
|   zalgo = {}; |   zalgo = {}; | ||||||
| 
 | 
 | ||||||
|   function randomNumber(range) { |   function randomNumber(range) { | ||||||
|     r = Math.floor(Math.random()*range); |     var r = Math.floor(Math.random() * range); | ||||||
|     return r; |     return r; | ||||||
|   }; |   } | ||||||
| 
 | 
 | ||||||
|   function is_char(character) { |   function is_char(character) { | ||||||
|     var bool = false; |     var bool = false; | ||||||
|     all.filter(function(i){ |     all.filter(function (i) { | ||||||
|      bool = (i == character); |       bool = (i === character); | ||||||
|     }); |     }); | ||||||
|     return bool; |     return bool; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   function heComes(text, options){ |   function heComes(text, options) { | ||||||
|       result = ''; |     var result = '', counts, l; | ||||||
|       options = options || {}; |     options = options || {}; | ||||||
|       options["up"] = options["up"] || true; |     options["up"] = options["up"] || true; | ||||||
|       options["mid"] = options["mid"] || true; |     options["mid"] = options["mid"] || true; | ||||||
|       options["down"] = options["down"] || true; |     options["down"] = options["down"] || true; | ||||||
|       options["size"] = options["size"] || "maxi"; |     options["size"] = options["size"] || "maxi"; | ||||||
|       var counts; |     text = text.split(''); | ||||||
|       text = text.split(''); |     for (l in text) { | ||||||
|        for(var l in text){ |       if (is_char(l)) { | ||||||
|          if(is_char(l)) { continue; } |         continue; | ||||||
|          result = result + text[l]; |       } | ||||||
|  |       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}; |       var arr = ["up", "mid", "down"]; | ||||||
| 
 |       for (var d in arr) { | ||||||
|         switch(options.size) { |         var index = arr[d]; | ||||||
|           case 'mini': |         for (var i = 0 ; i <= counts[index]; i++) { | ||||||
|             counts.up = randomNumber(8); |           if (options[index]) { | ||||||
|             counts.min= randomNumber(2); |             result = result + soul[index][randomNumber(soul[index].length)]; | ||||||
|             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)]; |  | ||||||
|               } |  | ||||||
|             } |  | ||||||
|           } |           } | ||||||
|         } |         } | ||||||
|       return result; |       } | ||||||
|   }; |     } | ||||||
|  |     return result; | ||||||
|  |   } | ||||||
|   return heComes(text); |   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