Define aliases inline.

This commit is contained in:
Ryan W Tenney
2010-10-05 21:10:14 -04:00
parent 644c5aa2ce
commit 2aec074aca

View File

@@ -59,10 +59,10 @@
// Collection Functions // Collection Functions
// -------------------- // --------------------
// The cornerstone, an `each` implementation. // The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects implementing `forEach`, arrays, and raw objects. // Handles objects implementing `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available. // Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.forEach = function(obj, iterator, context) { var each = _.each = _.forEach = function(obj, iterator, context) {
try { try {
if (nativeForEach && obj.forEach === nativeForEach) { if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context); obj.forEach(iterator, context);
@@ -92,7 +92,7 @@
// **Reduce** builds up a single result from a list of values, aka `inject`, // **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available. // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
_.reduce = function(obj, iterator, memo, context) { _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
if (nativeReduce && obj.reduce === nativeReduce) { if (nativeReduce && obj.reduce === nativeReduce) {
if (context) iterator = _.bind(iterator, context); if (context) iterator = _.bind(iterator, context);
return obj.reduce(iterator, memo); return obj.reduce(iterator, memo);
@@ -105,7 +105,7 @@
// The right-associative version of reduce, also known as `foldr`. Uses // The right-associative version of reduce, also known as `foldr`. Uses
// Delegates to **ECMAScript 5**'s native reduceRight if available. // Delegates to **ECMAScript 5**'s native reduceRight if available.
_.reduceRight = function(obj, iterator, memo, context) { _.reduceRight = _.foldr = function(obj, iterator, memo, context) {
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) { if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context); if (context) iterator = _.bind(iterator, context);
return obj.reduceRight(iterator, memo); return obj.reduceRight(iterator, memo);
@@ -128,7 +128,8 @@
// Return all the elements that pass a truth test. // Return all the elements that pass a truth test.
// Delegates to **ECMAScript 5**'s native `filter` if available. // Delegates to **ECMAScript 5**'s native `filter` if available.
_.filter = function(obj, iterator, context) { // Aliased as `select`
_.filter = _.select = function(obj, iterator, context) {
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context); if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
var results = []; var results = [];
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
@@ -148,7 +149,8 @@
// Determine whether all of the elements match a truth test. // Determine whether all of the elements match a truth test.
// Delegates to **ECMAScript 5**'s native `every` if available. // Delegates to **ECMAScript 5**'s native `every` if available.
_.every = function(obj, iterator, context) { // Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator = iterator || _.identity;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context); if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
var result = true; var result = true;
@@ -160,7 +162,8 @@
// Determine if at least one element in the object matches a truth test. // Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available. // Delegates to **ECMAScript 5**'s native `some` if available.
_.some = function(obj, iterator, context) { // Aliased as `any`.
_.some = _.any = function(obj, iterator, context) {
iterator = iterator || _.identity; iterator = iterator || _.identity;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
var result = false; var result = false;
@@ -260,7 +263,7 @@
// Get the first element of an array. Passing **n** will return the first N // Get the first element of an array. Passing **n** will return the first N
// values in the array. Aliased as `head`. The **guard** check allows it to work // values in the array. Aliased as `head`. The **guard** check allows it to work
// with `_.map`. // with `_.map`.
_.first = function(array, n, guard) { _.first = _.head = function(array, n, guard) {
return n && !guard ? slice.call(array, 0, n) : array[0]; return n && !guard ? slice.call(array, 0, n) : array[0];
}; };
@@ -268,7 +271,7 @@
// Especially useful on the arguments object. Passing an **index** will return // Especially useful on the arguments object. Passing an **index** will return
// the rest of the values in the array from that index onward. The **guard** // the rest of the values in the array from that index onward. The **guard**
// check allows it to work with `_.map`. // check allows it to work with `_.map`.
_.rest = function(array, index, guard) { _.rest = _.tail = function(array, index, guard) {
return slice.call(array, _.isUndefined(index) || guard ? 1 : index); return slice.call(array, _.isUndefined(index) || guard ? 1 : index);
}; };
@@ -307,8 +310,8 @@
}; };
// Produce an array that contains every item shared between all the // Produce an array that contains every item shared between all the
// passed-in arrays. // passed-in arrays. Aliased as `contains`.
_.intersect = function(array) { _.intersect = _.contains = function(array) {
var rest = slice.call(arguments, 1); var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) { return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) { return _.every(rest, function(other) {
@@ -447,7 +450,8 @@
}; };
// Return a sorted list of the function names available on the object. // Return a sorted list of the function names available on the object.
_.functions = function(obj) { // Aliased as `methods`
_.functions = _.methods = function(obj) {
return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort(); return _.filter(_.keys(obj), function(key){ return _.isFunction(obj[key]); }).sort();
}; };
@@ -648,20 +652,6 @@
return data ? func(data) : func; return data ? func(data) : func;
}; };
// Aliases:
// --------
_.each = _.forEach;
_.foldl = _.inject = _.reduce;
_.foldr = _.reduceRight;
_.select = _.filter;
_.all = _.every;
_.any = _.some;
_.contains = _.include;
_.head = _.first;
_.tail = _.rest;
_.methods = _.functions;
// The OOP Wrapper // The OOP Wrapper
// --------------- // ---------------