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

Fixed lingering CoffeeScript Compiler running live in Internet Explorer bugs. Implemented helpers.index_of and removed named functions. Ticket #366

This commit is contained in:
Jeremy Ashkenas 2010-05-14 23:40:04 -04:00
parent f84eb9ed47
commit dfb3a13246
16 changed files with 319 additions and 298 deletions

View file

@ -1,5 +1,5 @@
(function(){
var balanced_string, compact, count, del, extend, flatten, helpers, include, merge, starts;
var balanced_string, compact, count, del, extend, flatten, helpers, include, index_of, merge, starts;
var __hasProp = Object.prototype.hasOwnProperty;
// This file contains the common helper functions that we'd like to share among
// the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
@ -9,16 +9,31 @@
this.exports = this;
}
helpers = (exports.helpers = {});
// Cross-browser indexOf, so that IE can join the party.
helpers.index_of = (index_of = function(array, item, from) {
var _a, _b, index, other;
if (array.indexOf) {
return array.indexOf(item, from);
}
_a = array;
for (index = 0, _b = _a.length; index < _b; index++) {
other = _a[index];
if (other === item && (!from || (from <= index))) {
return index;
}
}
return -1;
});
// Does a list include a value?
helpers.include = (include = function include(list, value) {
return list.indexOf(value) >= 0;
helpers.include = (include = function(list, value) {
return index_of(list, value) >= 0;
});
// Peek at the beginning of a given string to see if it matches a sequence.
helpers.starts = (starts = function starts(string, literal, start) {
helpers.starts = (starts = function(string, literal, start) {
return string.substring(start, (start || 0) + literal.length) === literal;
});
// Trim out all falsy values from an array.
helpers.compact = (compact = function compact(array) {
helpers.compact = (compact = function(array) {
var _a, _b, _c, _d, item;
_a = []; _c = array;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
@ -28,20 +43,20 @@
return _a;
});
// Count the number of occurences of a character in a string.
helpers.count = (count = function count(string, letter) {
helpers.count = (count = function(string, letter) {
var num, pos;
num = 0;
pos = string.indexOf(letter);
pos = index_of(string, letter);
while (pos !== -1) {
num += 1;
pos = string.indexOf(letter, pos + 1);
pos = index_of(string, letter, pos + 1);
}
return num;
});
// Merge objects, returning a fresh copy with attributes from both sides.
// Used every time `BaseNode#compile` is called, to allow properties in the
// options hash to propagate down the tree without polluting other branches.
helpers.merge = (merge = function merge(options, overrides) {
helpers.merge = (merge = function(options, overrides) {
var _a, _b, fresh, key, val;
fresh = {};
_a = options;
@ -60,7 +75,7 @@
});
// Extend a source object with the properties of another object (shallow copy).
// We use this to simulate Node's deprecated `process.mixin`
helpers.extend = (extend = function extend(object, properties) {
helpers.extend = (extend = function(object, properties) {
var _a, _b, key, val;
_a = []; _b = properties;
for (key in _b) { if (__hasProp.call(_b, key)) {
@ -71,7 +86,7 @@
});
// Return a completely flattened version of an array. Handy for getting a
// list of `children` from the nodes.
helpers.flatten = (flatten = function flatten(array) {
helpers.flatten = (flatten = function(array) {
var _a, _b, _c, item, memo;
memo = [];
_b = array;
@ -83,7 +98,7 @@
});
// Delete a key from an object, returning the value. Useful when a node is
// looking for a particular method in an options hash.
helpers.del = (del = function del(obj, key) {
helpers.del = (del = function(obj, key) {
var val;
val = obj[key];
delete obj[key];
@ -93,7 +108,7 @@
// a series of delimiters, all of which must be nested correctly within the
// contents of the string. This method allows us to have strings within
// interpolations within strings, ad infinitum.
helpers.balanced_string = (balanced_string = function balanced_string(str, delimited, options) {
helpers.balanced_string = (balanced_string = function(str, delimited, options) {
var _a, _b, _c, _d, close, i, levels, open, pair, slash;
options = options || {};
slash = delimited[0][0] === '/';