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