helpers.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Generated by CoffeeScript 1.10.0
  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;
  78. for (i = 0, len1 = this.length; i < len1; i++) {
  79. e = this[i];
  80. if (fn(e)) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. };
  86. exports.invertLiterate = function(code) {
  87. var line, lines, maybe_code;
  88. maybe_code = true;
  89. lines = (function() {
  90. var i, len1, ref1, results;
  91. ref1 = code.split('\n');
  92. results = [];
  93. for (i = 0, len1 = ref1.length; i < len1; i++) {
  94. line = ref1[i];
  95. if (maybe_code && /^([ ]{4}|[ ]{0,3}\t)/.test(line)) {
  96. results.push(line);
  97. } else if (maybe_code = /^\s*$/.test(line)) {
  98. results.push(line);
  99. } else {
  100. results.push('# ' + line);
  101. }
  102. }
  103. return results;
  104. })();
  105. return lines.join('\n');
  106. };
  107. buildLocationData = function(first, last) {
  108. if (!last) {
  109. return first;
  110. } else {
  111. return {
  112. first_line: first.first_line,
  113. first_column: first.first_column,
  114. last_line: last.last_line,
  115. last_column: last.last_column
  116. };
  117. }
  118. };
  119. exports.addLocationDataFn = function(first, last) {
  120. return function(obj) {
  121. if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
  122. obj.updateLocationDataIfMissing(buildLocationData(first, last));
  123. }
  124. return obj;
  125. };
  126. };
  127. exports.locationDataToString = function(obj) {
  128. var locationData;
  129. if (("2" in obj) && ("first_line" in obj[2])) {
  130. locationData = obj[2];
  131. } else if ("first_line" in obj) {
  132. locationData = obj;
  133. }
  134. if (locationData) {
  135. return ((locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ((locationData.last_line + 1) + ":" + (locationData.last_column + 1));
  136. } else {
  137. return "No location data";
  138. }
  139. };
  140. exports.baseFileName = function(file, stripExt, useWinPathSep) {
  141. var parts, pathSep;
  142. if (stripExt == null) {
  143. stripExt = false;
  144. }
  145. if (useWinPathSep == null) {
  146. useWinPathSep = false;
  147. }
  148. pathSep = useWinPathSep ? /\\|\// : /\//;
  149. parts = file.split(pathSep);
  150. file = parts[parts.length - 1];
  151. if (!(stripExt && file.indexOf('.') >= 0)) {
  152. return file;
  153. }
  154. parts = file.split('.');
  155. parts.pop();
  156. if (parts[parts.length - 1] === 'coffee' && parts.length > 1) {
  157. parts.pop();
  158. }
  159. return parts.join('.');
  160. };
  161. exports.isCoffee = function(file) {
  162. return /\.((lit)?coffee|coffee\.md)$/.test(file);
  163. };
  164. exports.isLiterate = function(file) {
  165. return /\.(litcoffee|coffee\.md)$/.test(file);
  166. };
  167. exports.throwSyntaxError = function(message, location) {
  168. var error;
  169. error = new SyntaxError(message);
  170. error.location = location;
  171. error.toString = syntaxErrorToString;
  172. error.stack = error.toString();
  173. throw error;
  174. };
  175. exports.updateSyntaxError = function(error, code, filename) {
  176. if (error.toString === syntaxErrorToString) {
  177. error.code || (error.code = code);
  178. error.filename || (error.filename = filename);
  179. error.stack = error.toString();
  180. }
  181. return error;
  182. };
  183. syntaxErrorToString = function() {
  184. var codeLine, colorize, colorsEnabled, end, filename, first_column, first_line, last_column, last_line, marker, ref1, ref2, ref3, ref4, start;
  185. if (!(this.code && this.location)) {
  186. return Error.prototype.toString.call(this);
  187. }
  188. ref1 = this.location, first_line = ref1.first_line, first_column = ref1.first_column, last_line = ref1.last_line, last_column = ref1.last_column;
  189. if (last_line == null) {
  190. last_line = first_line;
  191. }
  192. if (last_column == null) {
  193. last_column = first_column;
  194. }
  195. filename = this.filename || '[stdin]';
  196. codeLine = this.code.split('\n')[first_line];
  197. start = first_column;
  198. end = first_line === last_line ? last_column + 1 : codeLine.length;
  199. marker = codeLine.slice(0, start).replace(/[^\s]/g, ' ') + repeat('^', end - start);
  200. if (typeof process !== "undefined" && process !== null) {
  201. colorsEnabled = ((ref2 = process.stdout) != null ? ref2.isTTY : void 0) && !((ref3 = process.env) != null ? ref3.NODE_DISABLE_COLORS : void 0);
  202. }
  203. if ((ref4 = this.colorful) != null ? ref4 : colorsEnabled) {
  204. colorize = function(str) {
  205. return "\x1B[1;31m" + str + "\x1B[0m";
  206. };
  207. codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
  208. marker = colorize(marker);
  209. }
  210. return filename + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + this.message + "\n" + codeLine + "\n" + marker;
  211. };
  212. exports.nameWhitespaceCharacter = function(string) {
  213. switch (string) {
  214. case ' ':
  215. return 'space';
  216. case '\n':
  217. return 'newline';
  218. case '\r':
  219. return 'carriage return';
  220. case '\t':
  221. return 'tab';
  222. default:
  223. return string;
  224. }
  225. };
  226. }).call(this);