Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
1
// Generated by CoffeeScript 1.6.3
2
(function() {
3
var buildLocationData, extend, flatten, last, repeat, _ref;
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 item, _i, _len, _results;
30
_results = [];
31
for (_i = 0, _len = array.length; _i < _len; _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, _len;
67
flattened = [];
68
for (_i = 0, _len = array.length; _i < _len; _i++) {
69
element = array[_i];
70
if (element instanceof Array) {
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.last = last = function(array, back) {
87
return array[array.length - (back || 0) - 1];
88
};
89
90
exports.some = (_ref = Array.prototype.some) != null ? _ref : function(fn) {
91
var e, _i, _len;
92
for (_i = 0, _len = this.length; _i < _len; _i++) {
93
e = this[_i];
94
if (fn(e)) {
95
return true;
96
}
97
}
98
return false;
99
};
100
101
exports.invertLiterate = function(code) {
102
var line, lines, maybe_code;
103
maybe_code = true;
104
lines = (function() {
105
var _i, _len, _ref1, _results;
106
_ref1 = code.split('\n');
107
_results = [];
108
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
109
line = _ref1[_i];
110
if (maybe_code && /^([ ]{4}|[ ]{0,3}\t)/.test(line)) {
111
_results.push(line);
112
} else if (maybe_code = /^\s*$/.test(line)) {
113
_results.push(line);
114
} else {
115
_results.push('# ' + line);
116
}
117
}
118
return _results;
119
})();
120
return lines.join('\n');
121
};
122
123
buildLocationData = function(first, last) {
124
if (!last) {
125
return first;
126
} else {
127
return {
128
first_line: first.first_line,
129
first_column: first.first_column,
130
last_line: last.last_line,
131
last_column: last.last_column
132
};
133
}
134
};
135
136
exports.addLocationDataFn = function(first, last) {
137
return function(obj) {
138
if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
139
obj.updateLocationDataIfMissing(buildLocationData(first, last));
140
}
141
return obj;
142
};
143
};
144
145
exports.locationDataToString = function(obj) {
146
var locationData;
147
if (("2" in obj) && ("first_line" in obj[2])) {
148
locationData = obj[2];
149
} else if ("first_line" in obj) {
150
locationData = obj;
151
}
152
if (locationData) {
153
return ("" + (locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ("" + (locationData.last_line + 1) + ":" + (locationData.last_column + 1));
154
} else {
155
return "No location data";
156
}
157
};
158
159
exports.baseFileName = function(file, stripExt, useWinPathSep) {
160
var parts, pathSep;
161
if (stripExt == null) {
162
stripExt = false;
163
}
164
if (useWinPathSep == null) {
165
useWinPathSep = false;
166
}
167
pathSep = useWinPathSep ? /\\|\// : /\//;
168
parts = file.split(pathSep);
169
file = parts[parts.length - 1];
170
if (!stripExt) {
171
return file;
172
}
173
parts = file.split('.');
174
parts.pop();
175
if (parts[parts.length - 1] === 'coffee' && parts.length > 1) {
176
parts.pop();
177
}
178
return parts.join('.');
179
};
180
181
exports.isCoffee = function(file) {
182
return /\.((lit)?coffee|coffee\.md)$/.test(file);
183
};
184
185
exports.isLiterate = function(file) {
186
return /\.(litcoffee|coffee\.md)$/.test(file);
187
};
188
189
exports.throwSyntaxError = function(message, location) {
190
var error;
191
if (location.last_line == null) {
192
location.last_line = location.first_line;
193
}
194
if (location.last_column == null) {
195
location.last_column = location.first_column;
196
}
197
error = new SyntaxError(message);
198
error.location = location;
199
throw error;
200
};
201
202
exports.prettyErrorMessage = function(error, fileName, code, useColors) {
203
var codeLine, colorize, end, first_column, first_line, last_column, last_line, marker, message, start, _ref1;
204
if (!error.location) {
205
return error.stack || ("" + error);
206
}
207
_ref1 = error.location, first_line = _ref1.first_line, first_column = _ref1.first_column, last_line = _ref1.last_line, last_column = _ref1.last_column;
208
codeLine = code.split('\n')[first_line];
209
start = first_column;
210
end = first_line === last_line ? last_column + 1 : codeLine.length;
211
marker = repeat(' ', start) + repeat('^', end - start);
212
if (useColors) {
213
colorize = function(str) {
214
return "\x1B[1;31m" + str + "\x1B[0m";
215
};
216
codeLine = codeLine.slice(0, start) + colorize(codeLine.slice(start, end)) + codeLine.slice(end);
217
marker = colorize(marker);
218
}
219
message = "" + fileName + ":" + (first_line + 1) + ":" + (first_column + 1) + ": error: " + error.message + "\n" + codeLine + "\n" + marker;
220
return message;
221
};
222
223
}).call(this);
224
225