Merging in satyr's helpers-refactor
This commit is contained in:
parent
e0ed254252
commit
45bd0854b6
|
@ -14,15 +14,15 @@
|
|||
return -1;
|
||||
}));
|
||||
exports.include = function(list, value) {
|
||||
return 0 <= indexOf(list, value);
|
||||
return indexOf(list, value) >= 0;
|
||||
};
|
||||
exports.starts = function(string, literal, start) {
|
||||
return literal === string.substr(start, literal.length);
|
||||
};
|
||||
exports.ends = function(string, literal, back) {
|
||||
var ll;
|
||||
ll = literal.length;
|
||||
return literal === string.substr(string.length - ll - (back || 0), ll);
|
||||
var len;
|
||||
len = literal.length;
|
||||
return literal === string.substr(string.length - len - (back || 0), len);
|
||||
};
|
||||
exports.compact = function(array) {
|
||||
var _i, _len, _ref, _result, item;
|
||||
|
@ -51,7 +51,7 @@
|
|||
_ref = properties;
|
||||
for (key in _ref) {
|
||||
val = _ref[key];
|
||||
(object[key] = val);
|
||||
object[key] = val;
|
||||
}
|
||||
return object;
|
||||
});
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
|
||||
# arrays, count characters, that sort of thing.
|
||||
|
||||
# Cross-engine indexOf, so that JScript can join the party.
|
||||
# Cross-engine `indexOf`, so that JScript can join the party. Use SpiderMonkey's
|
||||
# functional-style `indexOf`, if it's available.
|
||||
indexOf = exports.indexOf = Array.indexOf or
|
||||
if Array::indexOf
|
||||
(array, item, from) -> array.indexOf item, from
|
||||
|
@ -14,7 +15,8 @@ indexOf = exports.indexOf = Array.indexOf or
|
|||
-1
|
||||
|
||||
# Does a list include a value?
|
||||
exports.include = (list, value) -> 0 <= indexOf list, value
|
||||
exports.include = (list, value) ->
|
||||
indexOf(list, value) >= 0
|
||||
|
||||
# Peek at the beginning of a given string to see if it matches a sequence.
|
||||
exports.starts = (string, literal, start) ->
|
||||
|
@ -22,11 +24,12 @@ exports.starts = (string, literal, start) ->
|
|||
|
||||
# Peek at the end of a given string to see if it matches a sequence.
|
||||
exports.ends = (string, literal, back) ->
|
||||
ll = literal.length
|
||||
literal is string.substr string.length - ll - (back or 0), ll
|
||||
len = literal.length
|
||||
literal is string.substr string.length - len - (back or 0), len
|
||||
|
||||
# Trim out all falsy values from an array.
|
||||
exports.compact = (array) -> item for item in array when item
|
||||
exports.compact = (array) ->
|
||||
item for item in array when item
|
||||
|
||||
# Count the number of occurences of a character in a string.
|
||||
exports.count = (string, letter) ->
|
||||
|
@ -41,14 +44,16 @@ exports.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`
|
||||
# We use this to simulate Node's deprecated `process.mixin`.
|
||||
extend = exports.extend = (object, properties) ->
|
||||
(object[key] = val) for all key, val of properties
|
||||
for all key, val of properties
|
||||
object[key] = val
|
||||
object
|
||||
|
||||
# Return a flattened version of an array (nonrecursive).
|
||||
# Return a flattened version of an array (shallow and nonrecursive).
|
||||
# Handy for getting a list of `children` from the nodes.
|
||||
exports.flatten = (array) -> array.concat.apply [], array
|
||||
exports.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.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{ indexOf, include, starts, ends, compact, count, merge, extend, flatten, del
|
||||
} = require '../lib/helpers'
|
||||
{indexOf, include, starts, ends, compact, count, merge, extend, flatten, del} = require '../lib/helpers'
|
||||
|
||||
# Test `indexOf`
|
||||
array = [0..4]
|
||||
|
||||
ok indexOf(array, 0) is 0
|
||||
|
@ -8,11 +8,13 @@ ok indexOf(array, 2) is 2
|
|||
ok indexOf(array, 4) is 4
|
||||
ok indexOf(array, 6) is -1
|
||||
|
||||
# Test `include`
|
||||
ok include array, 0
|
||||
ok include array, 2
|
||||
ok include array, 4
|
||||
ok not include array, 6
|
||||
|
||||
# Test `starts`
|
||||
string = array.join ''
|
||||
|
||||
ok starts string, '012'
|
||||
|
@ -20,20 +22,24 @@ ok starts string, '34', 3
|
|||
ok not starts string, '42'
|
||||
ok not starts string, '42', 6
|
||||
|
||||
# Test `ends`
|
||||
ok ends string, '234'
|
||||
ok ends string, '01', 3
|
||||
ok not ends string, '42'
|
||||
ok not ends string, '42', 6
|
||||
|
||||
# Test `merge`
|
||||
object = {}
|
||||
merged = merge object, array
|
||||
|
||||
ok merged isnt object
|
||||
ok merged[3] is 3
|
||||
|
||||
# Test `extend`
|
||||
ok object is extend object, array
|
||||
ok object[3] is 3
|
||||
|
||||
# Test `flatten`
|
||||
ok "#{ flatten [0, [1, 2], 3, [4]] }" is "#{ array }"
|
||||
|
||||
ok 1 is del object, 1
|
||||
|
|
Loading…
Reference in New Issue