jashkenas--coffeescript/lib/coffee-script/helpers.js

164 lines
3.8 KiB
JavaScript
Raw Normal View History

2013-03-04 22:07:16 +00:00
// Generated by CoffeeScript 1.6.1
2010-07-25 07:15:12 +00:00
(function() {
var buildLocationData, extend, flatten, _ref;
2010-09-25 00:29:44 +00:00
exports.starts = function(string, literal, start) {
2010-09-25 00:18:47 +00:00
return literal === string.substr(start, literal.length);
};
2010-09-25 00:29:44 +00:00
exports.ends = function(string, literal, back) {
2010-09-26 14:28:48 +00:00
var len;
len = literal.length;
return literal === string.substr(string.length - len - (back || 0), len);
2010-09-25 00:18:47 +00:00
};
2013-02-26 17:41:01 +00:00
exports.repeat = function(str, n) {
var res;
res = '';
while (n > 0) {
if (n & 1) {
res += str;
}
n >>>= 1;
str += str;
}
return res;
2013-02-25 17:41:34 +00:00
};
2010-09-25 00:29:44 +00:00
exports.compact = function(array) {
2010-11-09 05:17:08 +00:00
var item, _i, _len, _results;
_results = [];
for (_i = 0, _len = array.length; _i < _len; _i++) {
2010-10-01 22:26:37 +00:00
item = array[_i];
2012-04-10 18:57:45 +00:00
if (item) {
_results.push(item);
}
}
2010-11-09 05:17:08 +00:00
return _results;
2010-09-25 00:18:47 +00:00
};
exports.count = function(string, substr) {
var num, pos;
num = pos = 0;
2012-04-10 18:57:45 +00:00
if (!substr.length) {
return 1 / 0;
}
while (pos = 1 + string.indexOf(substr, pos)) {
2010-09-25 00:18:47 +00:00
num++;
}
return num;
2010-09-25 00:18:47 +00:00
};
2010-09-25 00:29:44 +00:00
exports.merge = function(options, overrides) {
2010-09-25 00:18:47 +00:00
return extend(extend({}, options), overrides);
};
extend = exports.extend = function(object, properties) {
var key, val;
2010-10-01 22:26:37 +00:00
for (key in properties) {
val = properties[key];
2010-09-26 14:28:48 +00:00
object[key] = val;
}
2010-09-25 00:18:47 +00:00
return object;
};
exports.flatten = flatten = function(array) {
var element, flattened, _i, _len;
flattened = [];
for (_i = 0, _len = array.length; _i < _len; _i++) {
2010-10-01 22:26:37 +00:00
element = array[_i];
2010-09-29 00:12:37 +00:00
if (element instanceof Array) {
flattened = flattened.concat(flatten(element));
} else {
flattened.push(element);
}
}
return flattened;
};
2010-09-25 00:29:44 +00:00
exports.del = function(obj, key) {
var val;
val = obj[key];
delete obj[key];
return val;
2010-09-25 00:18:47 +00:00
};
2010-09-28 12:52:51 +00:00
exports.last = function(array, back) {
return array[array.length - (back || 0) - 1];
};
2011-12-14 15:39:20 +00:00
exports.some = (_ref = Array.prototype.some) != null ? _ref : function(fn) {
var e, _i, _len;
for (_i = 0, _len = this.length; _i < _len; _i++) {
e = this[_i];
if (fn(e)) {
return true;
}
}
return false;
};
buildLocationData = function(first, last) {
if (!last) {
return first;
} else {
return {
first_line: first.first_line,
first_column: first.first_column,
last_line: last.last_line,
last_column: last.last_column
};
}
};
exports.addLocationDataFn = function(first, last) {
return function(obj) {
if (((typeof obj) === 'object') && (!!obj['updateLocationDataIfMissing'])) {
obj.updateLocationDataIfMissing(buildLocationData(first, last));
}
return obj;
};
};
exports.locationDataToString = function(obj) {
var locationData;
if (("2" in obj) && ("first_line" in obj[2])) {
locationData = obj[2];
} else if ("first_line" in obj) {
locationData = obj;
}
if (locationData) {
return ("" + (locationData.first_line + 1) + ":" + (locationData.first_column + 1) + "-") + ("" + (locationData.last_line + 1) + ":" + (locationData.last_column + 1));
} else {
return "No location data";
}
};
2013-03-04 22:07:16 +00:00
exports.baseFileName = function(file, stripExt) {
var parts;
2013-03-04 22:07:16 +00:00
if (stripExt == null) {
stripExt = false;
}
parts = file.split('/');
file = parts[parts.length - 1];
2013-03-04 22:07:16 +00:00
if (!stripExt) {
return file;
}
parts = file.split('.');
parts.pop();
if (parts[parts.length - 1] === 'coffee') {
parts.pop();
}
return parts.join('.');
};
exports.isCoffee = function(file) {
return /\.((lit)?coffee|coffee\.md)$/.test(file);
};
exports.isLiterate = function(file) {
return /\.(litcoffee|coffee\.md)$/.test(file);
};
}).call(this);