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

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2010-07-25 03:15:12 -04:00
(function() {
var compact, count, del, ends, extend, flatten, helpers, include, indexOf, merge, starts;
helpers = (exports.helpers = {});
helpers.indexOf = (indexOf = function(array, item, from) {
var _len, _ref, index, other;
if (array.indexOf) {
return array.indexOf(item, from);
}
_ref = array;
for (index = 0, _len = _ref.length; index < _len; index++) {
other = _ref[index];
if (other === item && (!from || (from <= index))) {
return index;
}
}
return -1;
});
helpers.include = (include = function(list, value) {
return indexOf(list, value) >= 0;
2010-03-10 09:47:02 -05:00
});
helpers.starts = (starts = function(string, literal, start) {
return string.substring(start, (start || 0) + literal.length) === literal;
2010-03-10 09:47:02 -05:00
});
helpers.ends = (ends = function(string, literal, back) {
var start;
start = string.length - literal.length - ((typeof back !== "undefined" && back !== null) ? back : 0);
return string.substring(start, start + literal.length) === literal;
});
helpers.compact = (compact = function(array) {
var _i, _len, _ref, _result, item;
_result = []; _ref = array;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
2010-08-11 00:40:15 -04:00
if (item) {
_result.push(item);
}
}
return _result;
2010-03-10 09:47:02 -05:00
});
helpers.count = (count = function(string, letter) {
var num, pos;
num = 0;
pos = indexOf(string, letter);
while (pos !== -1) {
num += 1;
pos = indexOf(string, letter, pos + 1);
}
return num;
2010-03-10 09:47:02 -05:00
});
helpers.merge = (merge = function(options, overrides) {
var _ref, fresh, key, val;
fresh = {};
_ref = options;
for (key in _ref) {
val = _ref[key];
2010-03-23 00:18:50 -04:00
(fresh[key] = val);
}
if (overrides) {
_ref = overrides;
for (key in _ref) {
val = _ref[key];
2010-03-23 00:18:50 -04:00
(fresh[key] = val);
}
}
return fresh;
2010-03-10 09:47:02 -05:00
});
helpers.extend = (extend = function(object, properties) {
var _ref, _result, key, val;
_result = []; _ref = properties;
for (key in _ref) {
val = _ref[key];
_result.push(object[key] = val);
}
return _result;
});
helpers.flatten = (flatten = function(array) {
var _i, _len, _ref, item, memo;
memo = [];
_ref = array;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
item = _ref[_i];
2010-08-11 00:40:15 -04:00
if (item instanceof Array) {
memo = memo.concat(item);
} else {
memo.push(item);
}
}
return memo;
2010-03-10 09:47:02 -05:00
});
helpers.del = (del = function(obj, key) {
var val;
val = obj[key];
delete obj[key];
return val;
2010-03-10 09:47:02 -05:00
});
}).call(this);