helpers: refactored and fixed comments
This commit is contained in:
parent
3df82a757d
commit
9005682cf1
100
lib/helpers.js
100
lib/helpers.js
|
@ -1,11 +1,10 @@
|
|||
(function() {
|
||||
var compact, count, del, ends, extend, flatten, helpers, include, indexOf, merge, starts;
|
||||
var extend, helpers, indexOf;
|
||||
helpers = (exports.helpers = {});
|
||||
helpers.indexOf = (indexOf = function(array, item, from) {
|
||||
indexOf = (helpers.indexOf = Array.indexOf || (Array.prototype.indexOf ? function(array, item, from) {
|
||||
return array.indexOf(item, from);
|
||||
} : 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];
|
||||
|
@ -14,19 +13,19 @@
|
|||
}
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
helpers.include = (include = function(list, value) {
|
||||
return indexOf(list, value) >= 0;
|
||||
});
|
||||
helpers.starts = (starts = function(string, literal, start) {
|
||||
return string.substring(start, (start || 0) + literal.length) === literal;
|
||||
});
|
||||
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) {
|
||||
}));
|
||||
helpers.include = function(list, value) {
|
||||
return 0 <= indexOf(list, value);
|
||||
};
|
||||
helpers.starts = function(string, literal, start) {
|
||||
return literal === string.substr(start, literal.length);
|
||||
};
|
||||
helpers.ends = function(string, literal, back) {
|
||||
var ll;
|
||||
ll = literal.length;
|
||||
return literal === string.substr(string.length - ll - (back || 0), ll);
|
||||
};
|
||||
helpers.compact = function(array) {
|
||||
var _i, _len, _ref, _result, item;
|
||||
_result = []; _ref = array;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
|
@ -36,61 +35,34 @@
|
|||
}
|
||||
}
|
||||
return _result;
|
||||
});
|
||||
helpers.count = (count = function(string, letter) {
|
||||
};
|
||||
helpers.count = function(string, letter) {
|
||||
var num, pos;
|
||||
num = 0;
|
||||
pos = indexOf(string, letter);
|
||||
while (pos !== -1) {
|
||||
num += 1;
|
||||
pos = indexOf(string, letter, pos + 1);
|
||||
num = (pos = 0);
|
||||
while (0 < (pos = 1 + string.indexOf(letter, pos))) {
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
});
|
||||
helpers.merge = (merge = function(options, overrides) {
|
||||
var _ref, fresh, key, val;
|
||||
fresh = {};
|
||||
_ref = options;
|
||||
};
|
||||
helpers.merge = function(options, overrides) {
|
||||
return extend(extend({}, options), overrides);
|
||||
};
|
||||
extend = (helpers.extend = function(object, properties) {
|
||||
var _ref, key, val;
|
||||
_ref = properties;
|
||||
for (key in _ref) {
|
||||
val = _ref[key];
|
||||
(fresh[key] = val);
|
||||
(object[key] = val);
|
||||
}
|
||||
if (overrides) {
|
||||
_ref = overrides;
|
||||
for (key in _ref) {
|
||||
val = _ref[key];
|
||||
(fresh[key] = val);
|
||||
}
|
||||
}
|
||||
return fresh;
|
||||
return object;
|
||||
});
|
||||
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];
|
||||
if (item instanceof Array) {
|
||||
memo = memo.concat(item);
|
||||
} else {
|
||||
memo.push(item);
|
||||
}
|
||||
}
|
||||
return memo;
|
||||
});
|
||||
helpers.del = (del = function(obj, key) {
|
||||
helpers.flatten = function(array) {
|
||||
return array.concat.apply([], array);
|
||||
};
|
||||
helpers.del = function(obj, key) {
|
||||
var val;
|
||||
val = obj[key];
|
||||
delete obj[key];
|
||||
return val;
|
||||
});
|
||||
};
|
||||
}).call(this);
|
||||
|
|
|
@ -4,64 +4,57 @@
|
|||
|
||||
helpers = exports.helpers = {}
|
||||
|
||||
# Cross-browser indexOf, so that IE can join the party.
|
||||
helpers.indexOf = indexOf = (array, item, from) ->
|
||||
return array.indexOf item, from if array.indexOf
|
||||
for other, index in array
|
||||
if other is item and (not from or (from <= index))
|
||||
return index
|
||||
-1
|
||||
# Cross-engine indexOf, so that JScript can join the party.
|
||||
indexOf = helpers.indexOf = Array.indexOf or
|
||||
if Array::indexOf
|
||||
(array, item, from) -> array.indexOf item, from
|
||||
else
|
||||
(array, item, from) ->
|
||||
for other, index in array
|
||||
if other is item and (not from or from <= index)
|
||||
return index
|
||||
-1
|
||||
|
||||
# Does a list include a value?
|
||||
helpers.include = include = (list, value) ->
|
||||
indexOf(list, value) >= 0
|
||||
helpers.include = (list, value) -> 0 <= indexOf list, value
|
||||
|
||||
# Peek at the beginning of a given string to see if it matches a sequence.
|
||||
helpers.starts = starts = (string, literal, start) ->
|
||||
string.substring(start, (start or 0) + literal.length) is literal
|
||||
helpers.starts = (string, literal, start) ->
|
||||
literal is string.substr start, literal.length
|
||||
|
||||
# Peek at the end of a given string to see if it matches a sequence.
|
||||
helpers.ends = ends = (string, literal, back) ->
|
||||
start = string.length - literal.length - (back ? 0)
|
||||
string.substring(start, start + literal.length) is literal
|
||||
helpers.ends = (string, literal, back) ->
|
||||
ll = literal.length
|
||||
literal is string.substr string.length - ll - (back or 0), ll
|
||||
|
||||
# Trim out all falsy values from an array.
|
||||
helpers.compact = compact = (array) -> item for item in array when item
|
||||
helpers.compact = (array) -> item for item in array when item
|
||||
|
||||
# Count the number of occurences of a character in a string.
|
||||
helpers.count = count = (string, letter) ->
|
||||
num = 0
|
||||
pos = indexOf string, letter
|
||||
while pos isnt -1
|
||||
num += 1
|
||||
pos = indexOf string, letter, pos + 1
|
||||
helpers.count = (string, letter) ->
|
||||
num = pos = 0
|
||||
num++ while 0 < pos = 1 + string.indexOf letter, pos
|
||||
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 = (options, overrides) ->
|
||||
fresh = {}
|
||||
(fresh[key] = val) for all key, val of options
|
||||
(fresh[key] = val) for all key, val of overrides if overrides
|
||||
fresh
|
||||
helpers.merge = (options, overrides) ->
|
||||
extend (extend {}, options), overrides
|
||||
|
||||
# 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 = (object, properties) ->
|
||||
extend = helpers.extend = (object, properties) ->
|
||||
(object[key] = val) for all key, val of properties
|
||||
object
|
||||
|
||||
# Return a completely flattened version of an array. Handy for getting a
|
||||
# list of `children` from the nodes.
|
||||
helpers.flatten = flatten = (array) ->
|
||||
memo = []
|
||||
for item in array
|
||||
if item instanceof Array then memo = memo.concat(item) else memo.push(item)
|
||||
memo
|
||||
# Return a flattened version of an array (nonrecursive).
|
||||
# Handy for getting a list of `children` from the nodes.
|
||||
helpers.flatten = (array) -> array.concat.apply [], array
|
||||
|
||||
# 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 = (obj, key) ->
|
||||
val = obj[key]
|
||||
helpers.del = (obj, key) ->
|
||||
val = obj[key]
|
||||
delete obj[key]
|
||||
val
|
||||
|
|
Loading…
Reference in New Issue