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

246 lines
6.1 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, last, normalizePath, repeat, _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
};
exports.repeat = repeat = function(str, n) {
var res;
res = '';
while (n > 0) {
if (n & 1) {
res += str;
}
n >>>= 1;
str += str;
}
return res;
};
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
};
exports.last = last = function(array, back) {
2010-09-28 12:52:51 +00:00
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;
};
exports.invertLiterate = function(code) {
var line, lines, match;
lines = (function() {
var _i, _len, _ref1, _results;
_ref1 = code.split('\n');
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
line = _ref1[_i];
if (match = /^([ ]{4}|\t)/.exec(line)) {
_results.push(line.slice(match[0].length));
} else {
_results.push('# ' + line);
}
}
return _results;
})();
return lines.join('\n');
};
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);
};
exports.normalizePath = normalizePath = function(path, removeTrailingSlash) {
var i, newParts, part, parts, root, _i, _len;
if (removeTrailingSlash == null) {
removeTrailingSlash = false;
}
root = false;
parts = path.split('/');
newParts = [];
i = 0;
if (parts.length > 1 && parts[0] === '') {
parts.shift();
root = true;
}
for (i = _i = 0, _len = parts.length; _i < _len; i = ++_i) {
part = parts[i];
if (part === '.' || part === '') {
if ((i === parts.length - 1) && !removeTrailingSlash) {
newParts.push('');
}
} else if (part === '..') {
if (newParts.length === 0 || (newParts.length && last(newParts === '..'))) {
newParts.push('..');
} else {
newParts.pop();
}
} else {
newParts.push(part);
}
}
if (root) {
if (newParts.length === 0) {
return '/';
}
if (newParts.length[0] === '..') {
throw new Error("Invalid path: " + path);
}
newParts.unshift('');
}
return newParts.join('/');
};
exports.relativePath = function(from, to, cwd) {
var answer;
if (cwd == null) {
cwd = null;
}
if (cwd) {
from = cwd + "/" + from;
to = cwd + "/" + to;
}
from = (normalizePath(from)).split('/');
to = (normalizePath(to)).split('/');
while (from.length > 0 && to.length > 0 && from[0] === to[0]) {
from.shift();
to.shift();
}
if (from.length && from[0] === "..") {
throw new Error("'cwd' must be specified if 'from' references parent directory: " + (from.join('/')) + " -> " + (to.join('/')));
}
answer = repeat("../", from.length - 1);
return answer + ("" + (to.join('/')));
};
}).call(this);