Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extendStringPrototype.js 3.2KB

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