helpers.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Generated by CoffeeScript 1.12.6
  2. (function() {
  3. var buildLocationData, extend, flatten, ref, repeat, syntaxErrorToString;
  4. exports.starts = function(string, literal, start) {
  5. return literal === string.substr(start, literal.length);
  6. };
  7. exports.ends = function(string, literal, back) {
  8. var len;
  9. len = literal.length;
  10. return literal === string.substr(string.length - len - (back || 0), len);
  11. };
  12. exports.repeat = repeat = function(str, n) {
  13. var res;
  14. res = '';
  15. while (n > 0) {
  16. if (n & 1) {
  17. res += str;
  18. }
  19. n >>>= 1;
  20. str += str;
  21. }
  22. return res;
  23. };
  24. exports.compact = function(array) {
  25. var i, item, len1, results;
  26. results = [];
  27. for (i = 0, len1 = array.length; i < len1; i++) {
  28. item = array[i];
  29. if (item) {
  30. results.push(item);
  31. }
  32. }
  33. return results;
  34. };
  35. exports.count = function(string, substr) {
  36. var num, pos;
  37. num = pos = 0;
  38. if (!substr.length) {
  39. return 1 / 0;
  40. }
  41. while (pos = 1 + string.indexOf(substr, pos)) {
  42. num++;
  43. }
  44. return num;
  45. };
  46. exports.merge = function(options, overrides) {
  47. return extend(extend({}, options), overrides);
  48. };
  49. extend = exports.extend = function(object, properties) {
  50. var key, val;
  51. for (key in properties) {
  52. val = properties[key];
  53. object[key] = val;
  54. }
  55. return object;
  56. };
  57. exports.flatten = flatten = function(array) {
  58. var element, flattened, i, len1;
  59. flattened = [];
  60. for (i = 0, len1 = array.length; i < len1; i++) {
  61. element = array[i];
  62. if ('[object Array]' === Object.prototype.toString.call(element)) {
  63. flattened = flattened.concat(flatten(element));
  64. } else {
  65. flattened.push(element);
  66. }
  67. }
  68. return flattened;
  69. };
  70. exports.del = function(obj, key) {
  71. var val;
  72. val = obj[key];
  73. delete obj[key];
  74. return val;
  75. };
  76. exports.some = (ref = Array.prototype.some) != null ? ref : function(fn) {
  77. var e, i, len1, ref1;
  78. ref1 = this;
  79. for (i = 0, len1 = ref1.length; i < len1; i++) {
  80. e = ref1[i];
  81. if (fn(e)) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. };
  87. exports.invertLiterate = function(code) {
  88. var line, lines, maybe_code;
  89. maybe_code = true;
  90. lines = (function() {
  91. var i, len1, ref1, results;
  92. ref1 = code.split('\n');
  93. results = [];
  94. for (i = 0, len1 = ref1.length; i < len1; i++) {
  95. line = ref1[i];
  96. if (maybe_code && /^([ ]{4}|[ ]{0,3}\t)/.test(line)) {
  97. results.push(line);
  98. } else if (maybe_code = /^\s*$/.test(line)) {
  99. results.push(line);
  100. } else {
  101. results.push('# ' + line);
  102. }
  103. }
  104. return results;
  105. })();
  106. return lines.join('\n');
  107. };
  108. buildLocationData = function(first, last) {
  109. if (!last) {
  110. return first;
  111. } else {
  112. return {
  113. first_line: first.first_line,
  114. first_column: first.first_column,
  115. last_line: last.last_line,
  116. last_column: last.last_column
  117. };
  118. }
  119. };
  120. exports.addLocationDataFn = function(first, last) {
  121. return function(obj) {
  122. if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
  123. obj.updateLocationDataIfMissing(buildLocationData(first, last));
  124. }
  125. return obj;
  126. };
  127. };
  128. exports.locationDataToString = function(obj) {
  129. var locationData;
  130. if (("2" in obj) && ("first_line" in obj[2])) {
  131. locationData = obj[2];
  132. } else if ("first_line" in obj) {
  133. locationData = obj;
  134. }
  135. if (locationData) {
  136. return ((locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ((locationData.last_line + 1) + ":" + (locationData.last_column + 1));
  137. } else {
  138. return "No location data";
  139. }
  140. };
  141. exports.baseFileName = function(file, stripExt, useWinPathSep) {
  142. var parts, pathSep;
  143. if (stripExt == null) {
  144. stripExt = false;
  145. }
  146. if (useWinPathSep == null) {
  147. useWinPathSep = false;
  148. }
  149. pathSep = useWinPathSep ? /\\|\// : /\//;
  150. parts = file.split(pathSep);
  151. file = parts[parts.length - 1];
  152. if (!(stripExt && file.indexOf('.') >= 0)) {
  153. return file;
  154. }
  155. parts = file.split('.');
  156. parts.pop();
  157. if (parts[parts.length - 1] === 'coffee' && parts.length > 1) {
  158. parts.pop();
  159. }
  160. return parts.join('.');
  161. };
  162. exports.isCoffee = function(file) {
  163. return /\.((lit)?coffee|coffee\.md)$/.test(file);
  164. };
  165. exports.isLiterate = function(file) {
  166. return /\.(litcoffee|coffee\.md)$/.test(file);
  167. };
  168. exports.throwSyntaxError = function(message, location) {
  169. var error;
  170. error = new SyntaxError(message);
  171. error.location = location;
  172. error.toString = syntaxErrorToString;
  173. error.stack = error.toString();
  174. throw error;
  175. };
  176. exports.updateSyntaxError = function(error, code, filename) {
  177. if (error.toString === syntaxErrorToString) {
  178. error.code || (error.code = code);
  179. error.filename || (error.filename = filename);
  180. error.stack = error.toString();
  181. }
  182. return error;
  183. };
  184. syntaxErrorToString = function() {
  185. var codeLine, colorize, colorsEnabled, end, filename, first_column, first_line, last_column, last_line, marker, ref1, ref2, ref3, ref4, start;
  186. if (!(this.code && this.location)) {
  187. return Error.prototype.toString.call(this);
  188. }
  189. ref1 = this.location, first_line = ref1.first_line, first_column = ref1.first_column, last_line = ref1.last_line, last_column = ref1.last_column;
  190. if (last_line == null) {
  191. last_line = first_line;
  192. }
  193. if (last_column == null) {
  194. last_column = first_column;
  195. }
  196. filename = this.filename || '[stdin]';
  197. codeLine = this.code.split('\n')[first_line];
  198. start = first_column;
  199. end = first_line === last_line ? last_column + 1 : codeLine.length;
  200. marker = codeLine.slice(0, start).replace(/[^\s]/g, ' ') + repeat('^', end - start);
  201. if (typeof process !== "undefined" && process !== null) {
  202. colorsEnabled = ((ref2 = process.stdout) != null ? ref2.isTTY : void 0) && !((ref3 = process.env) != null ? ref3.NODE_DISABLE_COLORS : void 0);
  203. }
  204. if ((ref4 = this.colorful) != null ? ref4 : colorsEnabled) {
  205. colorize = function(str) {
  206. return "\x1B[1;31m" + str + "\x1B[0m";
  207. };
  208. codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
  209. marker = colorize(marker);
  210. }
  211. return filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + this.message + "\n" + codeLine + "\n" + marker;
  212. };
  213. exports.nameWhitespaceCharacter = function(string) {
  214. switch (string) {
  215. case ' ':
  216. return 'space';
  217. case '\n':
  218. return 'newline';
  219. case '\r':
  220. return 'carriage return';
  221. case '\t':
  222. return 'tab';
  223. default:
  224. return string;
  225. }
  226. };
  227. }).call(this);