PageRenderTime 64ms CodeModel.GetById 18ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/coffee-script/helpers.js

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