Version 1
[yaffs-website] / node_modules / colors / lib / extendStringPrototype.js
1 var colors = require('./colors');
2
3 module['exports'] = function () {
4
5   //
6   // Extends prototype of native string object to allow for "foo".red syntax
7   //
8   var addProperty = function (color, func) {
9     String.prototype.__defineGetter__(color, func);
10   };
11
12   var sequencer = function sequencer (map, str) {
13       return function () {
14         var exploded = this.split(""), i = 0;
15         exploded = exploded.map(map);
16         return exploded.join("");
17       }
18   };
19
20   addProperty('strip', function () {
21     return colors.strip(this);
22   });
23
24   addProperty('stripColors', function () {
25     return colors.strip(this);
26   });
27
28   addProperty("trap", function(){
29     return colors.trap(this);
30   });
31
32   addProperty("zalgo", function(){
33     return colors.zalgo(this);
34   });
35
36   addProperty("zebra", function(){
37     return colors.zebra(this);
38   });
39
40   addProperty("rainbow", function(){
41     return colors.rainbow(this);
42   });
43
44   addProperty("random", function(){
45     return colors.random(this);
46   });
47
48   addProperty("america", function(){
49     return colors.america(this);
50   });
51
52   //
53   // Iterate through all default styles and colors
54   //
55   var x = Object.keys(colors.styles);
56   x.forEach(function (style) {
57     addProperty(style, function () {
58       return colors.stylize(this, style);
59     });
60   });
61
62   function applyTheme(theme) {
63     //
64     // Remark: This is a list of methods that exist
65     // on String that you should not overwrite.
66     //
67     var stringPrototypeBlacklist = [
68       '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'charAt', 'constructor',
69       'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', 'charCodeAt',
70       'indexOf', 'lastIndexof', 'length', 'localeCompare', 'match', 'replace', 'search', 'slice', 'split', 'substring',
71       'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimLeft', 'trimRight'
72     ];
73
74     Object.keys(theme).forEach(function (prop) {
75       if (stringPrototypeBlacklist.indexOf(prop) !== -1) {
76         console.log('warn: '.red + ('String.prototype' + prop).magenta + ' is probably something you don\'t want to override. Ignoring style name');
77       }
78       else {
79         if (typeof(theme[prop]) === 'string') {
80           colors[prop] = colors[theme[prop]];
81           addProperty(prop, function () {
82             return colors[theme[prop]](this);
83           });
84         }
85         else {
86           addProperty(prop, function () {
87             var ret = this;
88             for (var t = 0; t < theme[prop].length; t++) {
89               ret = colors[theme[prop][t]](ret);
90             }
91             return ret;
92           });
93         }
94       }
95     });
96   }
97
98   colors.setTheme = function (theme) {
99     if (typeof theme === 'string') {
100       try {
101         colors.themes[theme] = require(theme);
102         applyTheme(colors.themes[theme]);
103         return colors.themes[theme];
104       } catch (err) {
105         console.log(err);
106         return err;
107       }
108     } else {
109       applyTheme(theme);
110     }
111   };
112
113 };