Merging in satyr's helpers-refactor

This commit is contained in:
Jeremy Ashkenas
2010-09-26 10:28:48 -04:00
parent e0ed254252
commit 45bd0854b6
3 changed files with 27 additions and 16 deletions

View File

@@ -14,15 +14,15 @@
return -1; return -1;
})); }));
exports.include = function(list, value) { exports.include = function(list, value) {
return 0 <= indexOf(list, value); return indexOf(list, value) >= 0;
}; };
exports.starts = function(string, literal, start) { exports.starts = function(string, literal, start) {
return literal === string.substr(start, literal.length); return literal === string.substr(start, literal.length);
}; };
exports.ends = function(string, literal, back) { exports.ends = function(string, literal, back) {
var ll; var len;
ll = literal.length; len = literal.length;
return literal === string.substr(string.length - ll - (back || 0), ll); return literal === string.substr(string.length - len - (back || 0), len);
}; };
exports.compact = function(array) { exports.compact = function(array) {
var _i, _len, _ref, _result, item; var _i, _len, _ref, _result, item;
@@ -51,7 +51,7 @@
_ref = properties; _ref = properties;
for (key in _ref) { for (key in _ref) {
val = _ref[key]; val = _ref[key];
(object[key] = val); object[key] = val;
} }
return object; return object;
}); });

View File

@@ -2,7 +2,8 @@
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten # the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
# arrays, count characters, that sort of thing. # 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 indexOf = exports.indexOf = Array.indexOf or
if Array::indexOf if Array::indexOf
(array, item, from) -> array.indexOf item, from (array, item, from) -> array.indexOf item, from
@@ -14,7 +15,8 @@ indexOf = exports.indexOf = Array.indexOf or
-1 -1
# Does a list include a value? # 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. # Peek at the beginning of a given string to see if it matches a sequence.
exports.starts = (string, literal, start) -> 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. # Peek at the end of a given string to see if it matches a sequence.
exports.ends = (string, literal, back) -> exports.ends = (string, literal, back) ->
ll = literal.length len = literal.length
literal is string.substr string.length - ll - (back or 0), ll literal is string.substr string.length - len - (back or 0), len
# Trim out all falsy values from an array. # 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. # Count the number of occurences of a character in a string.
exports.count = (string, letter) -> exports.count = (string, letter) ->
@@ -41,14 +44,16 @@ exports.merge = (options, overrides) ->
extend (extend {}, options), overrides extend (extend {}, options), overrides
# Extend a source object with the properties of another object (shallow copy). # 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) -> extend = exports.extend = (object, properties) ->
(object[key] = val) for all key, val of properties for all key, val of properties
object[key] = val
object 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. # 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 # Delete a key from an object, returning the value. Useful when a node is
# looking for a particular method in an options hash. # looking for a particular method in an options hash.

View File

@@ -1,6 +1,6 @@
{ indexOf, include, starts, ends, compact, count, merge, extend, flatten, del {indexOf, include, starts, ends, compact, count, merge, extend, flatten, del} = require '../lib/helpers'
} = require '../lib/helpers'
# Test `indexOf`
array = [0..4] array = [0..4]
ok indexOf(array, 0) is 0 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, 4) is 4
ok indexOf(array, 6) is -1 ok indexOf(array, 6) is -1
# Test `include`
ok include array, 0 ok include array, 0
ok include array, 2 ok include array, 2
ok include array, 4 ok include array, 4
ok not include array, 6 ok not include array, 6
# Test `starts`
string = array.join '' string = array.join ''
ok starts string, '012' ok starts string, '012'
@@ -20,20 +22,24 @@ ok starts string, '34', 3
ok not starts string, '42' ok not starts string, '42'
ok not starts string, '42', 6 ok not starts string, '42', 6
# Test `ends`
ok ends string, '234' ok ends string, '234'
ok ends string, '01', 3 ok ends string, '01', 3
ok not ends string, '42' ok not ends string, '42'
ok not ends string, '42', 6 ok not ends string, '42', 6
# Test `merge`
object = {} object = {}
merged = merge object, array merged = merge object, array
ok merged isnt object ok merged isnt object
ok merged[3] is 3 ok merged[3] is 3
# Test `extend`
ok object is extend object, array ok object is extend object, array
ok object[3] is 3 ok object[3] is 3
# Test `flatten`
ok "#{ flatten [0, [1, 2], 3, [4]] }" is "#{ array }" ok "#{ flatten [0, [1, 2], 3, [4]] }" is "#{ array }"
ok 1 is del object, 1 ok 1 is del object, 1