index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. var chalk = require("chalk");
  3. var parser = require("./lib/parser");
  4. var dlv = require("dlv");
  5. /**
  6. * Stateless compiler.
  7. * @param {String} string
  8. * @param {Object} [custom] - Any custom methods
  9. * @param {Object} [opts] - Options
  10. * @returns {String}
  11. */
  12. function compile(string, custom, opts) {
  13. opts = opts || {};
  14. return parseAst(createAst(parser, string), custom, function (err) {
  15. if (err) {
  16. if (opts.logErrors) {
  17. console.log(err.msg);
  18. }
  19. if (opts.failOnError) {
  20. throw Error(err.msg);
  21. }
  22. }
  23. });
  24. }
  25. /**
  26. * @param parser
  27. * @param string
  28. * @returns {*}
  29. */
  30. function createAst(parser, string) {
  31. return parser.parse(string);
  32. }
  33. /**
  34. * @param ast
  35. * @param custom
  36. * @param {Function} cb
  37. */
  38. function parseAst(ast, custom, cb) {
  39. var colors = [];
  40. return ast.reduce(function (joined, item) {
  41. var fn;
  42. if (item.color) {
  43. if (item.text) {
  44. if (fn = resolveFun(item.color, custom)) {
  45. colors.push(fn);
  46. return joined + fn(item.text);
  47. } else {
  48. cb({
  49. msg: "Method does not exist: " + item.color
  50. });
  51. return joined + item.text;
  52. }
  53. }
  54. }
  55. if (item.buffer) {
  56. return colors.length
  57. ? joined + colors[colors.length-1](item.buffer)
  58. : joined + item.buffer;
  59. }
  60. if (item.reset) {
  61. colors.pop();
  62. if (item.text) {
  63. return colors.length
  64. ? joined + colors[colors.length-1](item.text)
  65. : joined + item.text;
  66. }
  67. }
  68. return joined;
  69. }, "");
  70. }
  71. /**
  72. * @param path
  73. * @param custom
  74. * @returns {*}
  75. */
  76. function resolveFun(path, custom) {
  77. var fn;
  78. if (fn = getFun(custom, path)) {
  79. return fn.bind({compile:compile});
  80. }
  81. return getFun(chalk, path);
  82. }
  83. /**
  84. * Get a function from an object
  85. */
  86. function getFun(obj, path) {
  87. if (!obj) {
  88. return false;
  89. }
  90. return dlv(obj, path);
  91. }
  92. /**
  93. * @param {Object} [opts]
  94. * @param {Object} custom
  95. * @returns {Compiler}
  96. */
  97. function Compiler(custom, opts) {
  98. opts = opts || {};
  99. custom = custom || {};
  100. this.prefix = "";
  101. if (typeof opts.prefix === "string") {
  102. this.prefix = compile(opts.prefix, custom, opts);
  103. }
  104. if (typeof opts.prefix === "function") {
  105. this.prefix = opts.prefix;
  106. }
  107. this.compile = function (string, noPrefix) {
  108. var out = "";
  109. if (!noPrefix) {
  110. if (typeof this.prefix === "function") {
  111. out = this.prefix.apply({compile: compile}, [string, opts]);
  112. } else {
  113. out = this.prefix;
  114. }
  115. }
  116. return out + compile(string, custom, opts);
  117. };
  118. return this;
  119. }
  120. module.exports = compile;
  121. module.exports.parse = function (string) {
  122. return createAst(parser, string);
  123. };
  124. module.exports.clean = function (string) {
  125. var ast = createAst(parser, string);
  126. return ast.reduce(function (joined, item) {
  127. if (item.color) {
  128. if (item.text) {
  129. return joined + item.text;
  130. }
  131. }
  132. if (item.buffer) {
  133. return joined + item.buffer;
  134. }
  135. if (item.reset) {
  136. return joined + item.text;
  137. }
  138. return joined;
  139. }, "");
  140. };
  141. module.exports.Compiler = Compiler;