Skip to content

Commit

Permalink
Define aliases inline.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantenney committed Oct 6, 2010
1 parent 644c5aa commit 2aec074
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
// Collection Functions
// --------------------

// The cornerstone, an `each` implementation.
// The cornerstone, an `each` implementation, aka `forEach`.
// Handles objects implementing `forEach`, arrays, and raw objects.
// 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 {
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
Expand Down Expand Up @@ -92,7 +92,7 @@

// **Reduce** builds up a single result from a list of values, aka `inject`,
// 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 (context) iterator = _.bind(iterator, context);
return obj.reduce(iterator, memo);
Expand All @@ -105,7 +105,7 @@

// The right-associative version of reduce, also known as `foldr`. Uses
// 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 (context) iterator = _.bind(iterator, context);
return obj.reduceRight(iterator, memo);
Expand All @@ -128,7 +128,8 @@

// Return all the elements that pass a truth test.
// 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);
var results = [];
each(obj, function(value, index, list) {
Expand All @@ -148,7 +149,8 @@

// Determine whether all of the elements match a truth test.
// 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;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
var result = true;
Expand All @@ -160,7 +162,8 @@

// Determine if at least one element in the object matches a truth test.
// 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;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
var result = false;
Expand Down Expand Up @@ -260,15 +263,15 @@
// 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
// with `_.map`.
_.first = function(array, n, guard) {
_.first = _.head = function(array, n, guard) {
return n && !guard ? slice.call(array, 0, n) : array[0];
};

// Returns everything but the first entry of the array. Aliased as `tail`.
// 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**
// 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);
};

Expand Down Expand Up @@ -307,8 +310,8 @@
};

// Produce an array that contains every item shared between all the
// passed-in arrays.
_.intersect = function(array) {
// passed-in arrays. Aliased as `contains`.
_.intersect = _.contains = function(array) {
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
Expand Down Expand Up @@ -447,7 +450,8 @@
};

// 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();
};

Expand Down Expand Up @@ -648,20 +652,6 @@
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
// ---------------

Expand Down

0 comments on commit 2aec074

Please sign in to comment.