underscore.coffee

#
#

Underscore.coffee (c) 2010 Jeremy Ashkenas, DocumentCloud Inc. Underscore is freely distributable under the terms of the MIT license. Portions of Underscore are inspired by or borrowed from Prototype.js, Oliver Steele's Functional, and John Resig's Micro-Templating. For all details and documentation: http://documentcloud.github.com/underscore/

undefined
#

Baseline setup

undefined
#

Establish the root object, window in the browser, or global on the server.

undefined
#

Save the previous value of the _ variable.

undefined
#

Establish the object that gets thrown to break out of a loop iteration. StopIteration is SOP on Mozilla.

undefined
#

Helper function to escape RegExp contents, because JS doesn't have one.

undefined
#

Save bytes in the minified (but not gzipped) version:

undefined
#

Create quick reference variables for speed access to core prototypes.

undefined
#

All ECMA5 native implementations we hope to use are declared here.

undefined
#

Create a safe reference to the Underscore object for use below.

undefined
#

Export the Underscore object for CommonJS.

undefined
#

Export Underscore to global scope.

undefined
#

Current version.

undefined
#

Collection Functions

undefined
#

The cornerstone, an each implementation. Handles objects implementing forEach, arrays, and raw objects.

undefined
#

Return the results of applying the iterator to each element. Use JavaScript 1.6's version of map, if possible.

undefined
#

Reduce builds up a single result from a list of values. Also known as inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.

undefined
#

The right-associative version of reduce, also known as foldr. Uses JavaScript 1.8's version of reduceRight, if available.

undefined
#

Return the first value which passes a truth test.

undefined
#

Return all the elements that pass a truth test. Use JavaScript 1.6's filter, if it exists.

undefined
#

Return all the elements for which a truth test fails.

undefined
#

Determine whether all of the elements match a truth test. Delegate to JavaScript 1.6's every, if it is present.

undefined
#

Determine if at least one element in the object matches a truth test. Use JavaScript 1.6's some, if it exists.

undefined
#

Determine if a given value is included in the array or object, based on ===.

undefined
#

Invoke a method with arguments on every item in a collection.

undefined
#

Convenience version of a common use case of map: fetching a property.

undefined
#

Return the maximum item or (item-based computation).

undefined
#

Return the minimum element (or element-based computation).

undefined
#

Sort the object's values by a criterion produced by an iterator.

undefined
#

Use a comparator function to figure out at what index an object should be inserted so as to maintain order. Uses binary search.

undefined
#

Convert anything iterable into a real, live array.

undefined
#

Return the number of elements in an object.

undefined
#

Array Functions

undefined
#

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.

undefined
#

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.

undefined
#

Get the last element of an array.

undefined
#

Trim out all falsy values from an array.

undefined
#

Return a completely flattened version of an array.

undefined
#

Return a version of the array that does not contain the specified value(s).

undefined
#

Produce a duplicate-free version of the array. If the array has already been sorted, you have the option of using a faster algorithm.

undefined
#

Produce an array that contains every item shared between all the passed-in arrays.

undefined
#

Zip together multiple lists into a single array -- elements that share an index go together.

undefined
#

If the browser doesn't supply us with indexOf (I'm looking at you, MSIE), we need this function. Return the position of the first occurence of an item in an array, or -1 if the item is not included in the array.

undefined
#

Provide JavaScript 1.6's lastIndexOf, delegating to the native function, if possible.

undefined
#

Generate an integer Array containing an arithmetic progression. A port of the native Python range function.

undefined
#

Function Functions

undefined
#

Create a function bound to a given object (assigning this, and arguments, optionally). Binding with arguments is also known as curry.

undefined
#

Bind all of an object's methods to that object. Useful for ensuring that all callbacks defined on an object belong to it.

undefined
#

Delays a function for the given number of milliseconds, and then calls it with the arguments supplied.

undefined
#

Memoize an expensive function by storing its results.

undefined
#

Defers a function, scheduling it to run after the current call stack has cleared.

undefined
#

Returns the first function passed as an argument to the second, allowing you to adjust arguments, run code before and after, and conditionally execute the original function.

undefined
#

Returns a function that is the composition of a list of functions, each consuming the return value of the function that follows.

undefined
#

Object Functions

undefined
#

Retrieve the names of an object's properties.

undefined
#

Retrieve the values of an object's properties.

undefined
#

Return a sorted list of the function names available in Underscore.

undefined
#

Extend a given object with all of the properties in a source object.

undefined
#

Create a (shallow-cloned) duplicate of an object.

undefined
#

Invokes interceptor with the obj, and then returns obj. The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.

undefined
#

Perform a deep comparison to check if two objects are equal.

undefined
#

Check object identity.

undefined
#

Different types?

undefined
#

Basic equality test (watch out for coercions).

undefined
#

One is falsy and the other truthy.

undefined
#

One of them implements an isEqual()?

undefined
#

Check dates' integer values.

undefined
#

Both are NaN?

undefined
#

Compare regular expressions.

undefined
#

If a is not an object by this point, we can't handle it.

undefined
#

Check for different array lengths before comparing contents.

undefined
#

Nothing else worked, deep compare the contents.

undefined
#

Different object sizes?

undefined
#

Recursive comparison of contents.

undefined
#

Is a given array or object empty?

undefined
#

Is a given value a DOM element?

undefined
#

Is a given value an array?

undefined
#

Is a given variable an arguments object?

undefined
#

Is the given value a function?

undefined
#

Is the given value a string?

undefined
#

Is a given value a number?

undefined
#

Is a given value a boolean?

undefined
#

Is a given value a Date?

undefined
#

Is the given value a regular expression?

undefined
#

Is the given value NaN -- this one is interesting. NaN != NaN, and isNaN(undefined) == true, so we make sure it's a number first.

undefined
#

Is a given value equal to null?

undefined
#

Is a given variable undefined?

undefined
#

Utility Functions

undefined
#

Run Underscore.js in noConflict mode, returning the _ variable to its previous owner. Returns a reference to the Underscore object.

undefined
#

Keep the identity function around for default iterators.

undefined
#

Run a function n times.

undefined
#

Break out of the middle of an iteration.

undefined
#

Add your own custom functions to the Underscore object, ensuring that they're correctly added to the OOP wrapper as well.

undefined
#

Generate a unique integer id (unique within the entire client session). Useful for temporary DOM ids.

undefined
#

By default, Underscore uses ERB-style template delimiters, change the following template settings to use alternative delimiters.

undefined
#

JavaScript templating a-la ERB, pilfered from John Resig's Secrets of the JavaScript Ninja, page 83. Single-quote fix from Rick Strahl. With alterations for arbitrary delimiters, and to preserve whitespace.

undefined
#

Aliases

undefined
#

Setup the OOP Wrapper

undefined
#

If Underscore is called as a function, it returns a wrapped object that can be used OO-style. This wrapper holds altered versions of all the underscore functions. Wrapped objects may be chained.

undefined
#

Helper function to continue chaining intermediate results.

undefined
#

A method to easily add functions to the OOP wrapper.

undefined
#

Add all of the Underscore functions to the wrapper object.

undefined
#

Add all mutator Array functions to the wrapper.

undefined
#

Add all accessor Array functions to the wrapper.

undefined
#

Start chaining a wrapped Underscore object.

undefined
#

Extracts the result from a wrapped and chained object.

undefined