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