1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/helpers.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
var extend, flatten;
2010-09-24 20:29:44 -04:00
exports.starts = function(string, literal, start) {
2010-09-24 20:18:47 -04:00
return literal === string.substr(start, literal.length);
};
2010-09-24 20:29:44 -04:00
exports.ends = function(string, literal, back) {
2010-09-26 10:28:48 -04:00
var len;
len = literal.length;
return literal === string.substr(string.length - len - (back || 0), len);
2010-09-24 20:18:47 -04:00
};
2010-09-24 20:29:44 -04:00
exports.compact = function(array) {
var _i, _len, _result, item;
2010-10-01 18:26:37 -04:00
_result = [];
for (_i = 0, _len = array.length; _i < _len; _i++) {
item = array[_i];
2010-08-11 00:40:15 -04:00
if (item) {
_result.push(item);
}
}
return _result;
2010-09-24 20:18:47 -04:00
};
2010-09-24 20:29:44 -04:00
exports.count = function(string, letter) {
var num, pos;
num = pos = 0;
2010-09-28 08:52:51 -04:00
while (pos = 1 + string.indexOf(letter, pos)) {
2010-09-24 20:18:47 -04:00
num++;
}
return num;
2010-09-24 20:18:47 -04:00
};
2010-09-24 20:29:44 -04:00
exports.merge = function(options, overrides) {
2010-09-24 20:18:47 -04:00
return extend(extend({}, options), overrides);
};
extend = exports.extend = function(object, properties) {
var key, val;
2010-10-01 18:26:37 -04:00
for (key in properties) {
val = properties[key];
2010-09-26 10:28:48 -04:00
object[key] = val;
}
2010-09-24 20:18:47 -04:00
return object;
};
exports.flatten = flatten = function(array) {
var _i, _len, element, flattened;
flattened = [];
2010-10-01 18:26:37 -04:00
for (_i = 0, _len = array.length; _i < _len; _i++) {
element = array[_i];
2010-09-28 20:12:37 -04:00
if (element instanceof Array) {
flattened = flattened.concat(flatten(element));
} else {
flattened.push(element);
}
}
return flattened;
};
2010-09-24 20:29:44 -04:00
exports.del = function(obj, key) {
var val;
val = obj[key];
delete obj[key];
return val;
2010-09-24 20:18:47 -04:00
};
2010-09-28 08:52:51 -04:00
exports.last = function(array, back) {
return array[array.length - (back || 0) - 1];
};
}).call(this);