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