504515ad1c3e9b92554ca7f7e187e40ca6ee9ba70cae79bec1da7b88e10ad9bff13750e40f2e78d0c17847841553bcb5c386b8ee1721d1eb5c07cf74de92c0 3.0 KB

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