Update vendor/underscore to v1.6.0.

This commit is contained in:
John-David Dalton
2014-02-10 23:00:19 -08:00
parent f090af2292
commit 4b448a877c
13 changed files with 724 additions and 547 deletions

View File

@@ -24,7 +24,6 @@ A list of upcoming features is available on our [roadmap](https://github.com/lod
## Features *not* in Underscore
* ~100% [code coverage](https://coveralls.io/r/lodash)
* AMD loader support ([curl](https://github.com/cujojs/curl), [dojo](http://dojotoolkit.org/), [requirejs](http://requirejs.org/), etc.)
* Module collections for [AMD](https://github.com/lodash/lodash-amd/tree/2.4.1), [Node.js](https://npmjs.org/package/lodash-node), & as [npm packages](https://npmjs.org/browse/keyword/lodash-modularized)
* [Semantic versioning](http://semver.org/) support for releases
* [_(…)](http://lodash.com/docs#_) supports intuitive chaining
@@ -32,14 +31,13 @@ A list of upcoming features is available on our [roadmap](https://github.com/lod
* [_.bindKey](http://lodash.com/docs#bindKey) for binding [*“lazy”*](http://michaux.ca/articles/lazy-function-definition-pattern) defined methods
* [_.clone](http://lodash.com/docs#clone) supports shallow cloning of `Date` & `RegExp` objects
* [_.cloneDeep](http://lodash.com/docs#cloneDeep) for deep cloning arrays & objects
* [_.constant](http://lodash.com/docs#constant) & [_.property](http://lodash.com/docs#property) function generators for composing functions
* [_.contains](http://lodash.com/docs#contains) accepts a `fromIndex`
* [_.create](http://lodash.com/docs#create) for easier object inheritance
* [_.callback](http://lodash.com/docs#createCallback) for extending callbacks in methods & mixins
* [_.curry](http://lodash.com/docs#curry) for creating [curried](http://hughfdjackson.com/javascript/2013/07/06/why-curry-helps/) functions
* [_.debounce](http://lodash.com/docs#debounce) & [_.throttle](http://lodash.com/docs#throttle) accept additional `options` for more control
* [_.findIndex](http://lodash.com/docs#findIndex) & [_.findKey](http://lodash.com/docs#findKey) for finding indexes & keys
* [_.forEach](http://lodash.com/docs#forEach) is chainable & supports exiting early
* [_.forEach](http://lodash.com/docs#forEach) supports exiting early
* [_.forIn](http://lodash.com/docs#forIn) for iterating own & inherited properties
* [_.forOwn](http://lodash.com/docs#forOwn) for iterating own properties
* [_.isPlainObject](http://lodash.com/docs#isPlainObject) for checking if values are created by `Object`
@@ -47,7 +45,6 @@ A list of upcoming features is available on our [roadmap](https://github.com/lod
* [_.memoize](http://lodash.com/docs#memoize) exposes the `cache` of memoized functions
* [_.merge](http://lodash.com/docs#merge) for a deep [_.extend](http://lodash.com/docs#extend)
* [_.noop](http://lodash.com/docs#noop) for function placeholders
* [_.now](http://lodash.com/docs#now) as a cross-browser `Date.now` alternative
* [_.parseInt](http://lodash.com/docs#parseInt) for consistent behavior
* [_.pull](http://lodash.com/docs#pull) & [_.remove](http://lodash.com/docs#remove) for mutating arrays
* [_.random](http://lodash.com/docs#random) supports returning floating-point numbers

View File

@@ -268,7 +268,7 @@
* `curry`, `debounce`, `defaults`, `defer`, `delay`, `difference`, `filter`,
* `flatten`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`,
* `forOwnRight`, `functions`, `groupBy`, `indexBy`, `initial`, `intersection`,
* `invert`, `invoke`, `keys`, `map`, `mapValues`, `match`, `max`, `memoize`,
* `invert`, `invoke`, `keys`, `map`, `mapValues`, `matches`, `max`, `memoize`,
* `merge`, `min`, `noop`, `object`, `omit`, `once`, `pairs`, `partial`,
* `partialRight`, `pick`, `pluck`, `property`, `pull`, `push`, `range`,
* `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`,
@@ -2304,33 +2304,6 @@
}
}
/**
* Examines each element in a `collection`, returning the first that
* has the given properties. When checking `properties`, this method
* performs a deep comparison between values to determine if they are
* equivalent to each other.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Object} properties The object of property values to filter by.
* @returns {*} Returns the found element, else `undefined`.
* @example
*
* var food = [
* { 'name': 'apple', 'organic': false, 'type': 'fruit' },
* { 'name': 'banana', 'organic': true, 'type': 'fruit' },
* { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
* ];
*
* _.findWhere(food, { 'type': 'vegetable' });
* // => { 'name': 'beet', 'organic': false, 'type': 'vegetable' }
*/
function findWhere(object, properties) {
return where(object, properties, true);
}
/**
* Iterates over elements of a collection, executing the callback for each
* element. The callback is bound to `thisArg` and invoked with three arguments;
@@ -2371,6 +2344,7 @@
} else {
baseEach(collection, callback);
}
return collection;
}
/**
@@ -2705,6 +2679,54 @@
return result;
}
/**
* Creates an array of elements split into two groups, the first of which
* contains elements the callback returns truey for, while the second of which
* contains elements the callback returns falsey for. The callback is bound
* to `thisArg` and invoked with three arguments; (value, index|key, collection).
*
* If a property name is provided for `callback` the created "_.pluck" style
* callback will return the property value of the given element.
*
* If an object is provided for `callback` the created "_.where" style callback
* will return `true` for elements that have the properties of the given object,
* else `false`.
*
* @static
* @memberOf _
* @category Collections
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|Object|string} [callback=identity] The function called
* per iteration. If a property name or object is provided it will be used
* to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Array} Returns a new array of grouped elements.
* @example
*
* _.partition([1, 2, 3], function(num) { return num % 2; });
* // => [[1, 3], [2]]
*
* _.partition([1.2, 2.3, 3.4], function(num) { return this.floor(num) % 2; }, Math);
* // => [[1, 3], [2]]
*
* var characters = [
* { 'name': 'barney', 'age': 36 },
* { 'name': 'fred', 'age': 40, 'blocked': true },
* { 'name': 'pebbles', 'age': 1 }
* ];
*
* // using "_.where" callback shorthand
* _.map(_.partition(characters, { 'age': 1 }), function(array) { return _.pluck(array, 'name'); });
* // => [['pebbles'], ['barney', 'fred']]
*
* // using "_.pluck" callback shorthand
* _.map(_.partition(characters, 'blocked'), function(array) { return _.pluck(array, 'name'); });
* // => [['fred'], ['barney', 'pebbles']]
*/
var partition = createAggregator(function(result, value, key) {
result[key ? 0 : 1].push(value);
}, true);
/**
* Retrieves the value of a specified property from all elements in the collection.
*
@@ -3122,11 +3144,7 @@
* _.where(characters, { 'pets': ['dino'] });
* // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
*/
function where(collection, properties, first) {
return (first && isEmpty(properties))
? undefined
: (first ? find : filter)(collection, properties);
}
var where = filter;
/*--------------------------------------------------------------------------*/
@@ -4628,6 +4646,27 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a function that returns `value`.
*
* @static
* @memberOf _
* @category Utilities
* @param {*} value The value to return from the new function.
* @returns {Function} Returns the new function.
* @example
*
* var object = { 'name': 'fred' };
* var getter = _.constant(object);
* getter() === object;
* // => true
*/
function constant(value) {
return function() {
return value;
};
}
/**
* Produces a callback bound to an optional `thisArg`. If `func` is a property
* name the created callback will return the property value for a given element.
@@ -4667,7 +4706,7 @@
func || baseCreateCallback(func, thisArg, argCount);
}
// handle "_.pluck" and "_.where" style callback shorthands
return type != 'object' ? property(func) : match(func);
return type != 'object' ? property(func) : matches(func);
}
/**
@@ -4705,21 +4744,21 @@
* { 'name': 'barney', 'age': 36 }
* ];
*
* var matchAge = _.match({ 'age': 36 });
* var matchesAge = _.matches({ 'age': 36 });
*
* _.filter(characters, matchAge);
* _.filter(characters, matchesAge);
* // => [{ 'name': 'barney', 'age': 36 }]
*
* _.find(characters, matchAge);
* _.find(characters, matchesAge);
* // => { 'name': 'barney', 'age': 36 }
*/
function match(source) {
function matches(source) {
source || (source = {});
var props = keys(source);
return function(object) {
var length = props.length,
result = false;
result = true;
while (length--) {
var key = props[length];
@@ -5010,6 +5049,7 @@
lodash.chain = chain;
lodash.compact = compact;
lodash.compose = compose;
lodash.constant = constant;
lodash.countBy = countBy;
lodash.debounce = debounce;
lodash.defaults = defaults;
@@ -5028,6 +5068,7 @@
lodash.invoke = invoke;
lodash.keys = keys;
lodash.map = map;
lodash.matches = matches;
lodash.max = max;
lodash.memoize = memoize;
lodash.min = min;
@@ -5035,8 +5076,10 @@
lodash.once = once;
lodash.pairs = pairs;
lodash.partial = partial;
lodash.partition = partition;
lodash.pick = pick;
lodash.pluck = pluck;
lodash.property = property;
lodash.range = range;
lodash.reject = reject;
lodash.rest = rest;
@@ -5095,6 +5138,7 @@
lodash.lastIndexOf = lastIndexOf;
lodash.mixin = mixin;
lodash.noConflict = noConflict;
lodash.now = now;
lodash.random = random;
lodash.reduce = reduce;
lodash.reduceRight = reduceRight;
@@ -5110,7 +5154,7 @@
lodash.all = every;
lodash.any = some;
lodash.detect = find;
lodash.findWhere = findWhere;
lodash.findWhere = find;
lodash.foldl = reduce;
lodash.foldr = reduceRight;
lodash.include = contains;

View File

@@ -3,38 +3,39 @@
* Lo-Dash 2.4.1 (Custom Build) lodash.com/license | Underscore.js 1.5.2 underscorejs.org/LICENSE
* Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js`
*/
;(function(){function n(n){var r=[];if(!L(n))return r;for(var t in n)Ur.call(n,t)&&r.push(t);return r}function r(n,r){if(L(n))for(var t in n)if(r(n[t],t,n)===sr)break}function t(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++t<e;)if(n[t]===r)return t;return-1}function e(n,r){var t;n:{t=n.f;var e=r.f;if(t!==e){if(t>e||typeof t=="undefined"){t=1;break n}if(t<e||typeof e=="undefined"){t=-1;break n}}t=0}return t||n.g-r.g}function u(n){return Er[n]}function o(n){return"\\"+Sr[n]}function i(n){return Or[n]}function f(n){return n instanceof f?n:new a(n)
}function a(n,r){this.__chain__=!!r,this.__wrapped__=n}function l(n){function r(){if(u){var n=O(u);Vr.apply(n,arguments)}if(this instanceof r){var o=c(t.prototype),n=t.apply(o,n||arguments);return L(n)?n:o}return t.apply(e,n||arguments)}var t=n[0],e=n[3],u=n[4];return r}function c(n){return L(n)?Jr(n):{}}function p(n,r,t){if(typeof n!="function")return nr;if(typeof r=="undefined"||!("prototype"in n))return n;switch(t){case 1:return function(t){return n.call(r,t)};case 2:return function(t,e){return n.call(r,t,e)
};case 3:return function(t,e,u){return n.call(r,t,e,u)};case 4:return function(t,e,u,o){return n.call(r,t,e,u,o)}}return P(n,r)}function s(n){function r(){var n=a?o:this;if(i){var v=O(i);Vr.apply(v,arguments)}if(f||p){v||(v=O(arguments)),f&&Vr.apply(v,f);var y=arguments.length;if(p&&y<u)return e|=lr,e&=~cr,g||(e&=~(or|ir)),n=Yr(0,u-y),s([t,e,n,o,v])}return v||(v=arguments),l&&(t=n[h]),this instanceof r?(n=c(t.prototype),v=t.apply(n,v),L(v)?v:n):t.apply(n,v)}var t=n[0],e=n[1],u=n[2],o=n[3],i=n[4],f=n[5],a=e&or,l=e&ir,p=e&fr,g=e&ar,h=t;
return r}function g(n,r){for(var t=-1,e=w(),u=n?n.length:0,o=[];++t<u;){var i=n[t];0>e(r,i)&&o.push(i)}return o}function h(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(;++t<e&&r(n[t],t,n)!==sr;);else for(var t=-1,e=at(n),u=e.length;++t<u;){var o=e[t];if(r(n[o],o,n)===sr)break}}function v(n,r){var t=n?n.length:0;if(typeof t=="number")for(;t--&&r(n[t],t,n)!==sr;);else for(var t=at(n),e=t.length;e--;){var u=t[e];if(r(n[u],u,n)===sr)break}}function y(n,r,t,e){e=(e||0)-1;for(var u=n?n.length:0,o=[];++e<u;){var i=n[e];
if(i&&typeof i=="object"&&typeof i.length=="number"&&(ft(i)||x(i))){r||(i=y(i,r,t));var f=-1,a=i.length,l=o.length;for(o.length+=a;++f<a;)o[l++]=i[f]}else t||o.push(i)}return o}function m(n,t,e,u){if(n===t)return 0!==n||1/n==1/t;var o=typeof n,i=typeof t;if(n===n&&(!n||"function"!=o&&"object"!=o)&&(!t||"function"!=i&&"object"!=i))return false;if(null==n||null==t)return n===t;if(i=Dr.call(n),o=Dr.call(t),i!=o)return false;switch(i){case dr:case wr:return+n==+t;case jr:return n!=+n?t!=+t:0==n?1/n==1/t:n==+t;
case Tr:case Ar:return n==t+""}if(o=i==_r,!o){var a=n instanceof f,l=t instanceof f;if(a||l)return m(a?n.__wrapped__:n,l?t.__wrapped__:t,e,u);if(i!=xr)return false;if(i=Ur.call(n,"constructor"),a=Ur.call(t,"constructor"),i!==a||!i&&(i=n.constructor,a=t.constructor,i!=a&&!(K(i)&&i instanceof i&&K(a)&&a instanceof a)&&"constructor"in n&&"constructor"in t))return false}for(e||(e=[]),u||(u=[]),i=e.length;i--;)if(e[i]==n)return u[i]==t;var c=true,p=0;if(e.push(n),u.push(t),o){if(p=t.length,c=p==n.length)for(;p--&&(c=m(n[p],t[p],e,u)););}else r(t,function(r,t,o){return Ur.call(o,t)?(p++,!(c=Ur.call(n,t)&&m(n[t],r,e,u))&&sr):void 0
}),c&&r(n,function(n,r,t){return Ur.call(t,r)?!(c=-1<--p)&&sr:void 0});return e.pop(),u.pop(),c}function b(n,r,t){for(var e=-1,u=w(),o=n?n.length:0,i=[],f=t?[]:i;++e<o;){var a=n[e],l=t?t(a,e,n):a;(r?!e||f[f.length-1]!==l:0>u(f,l))&&(t&&f.push(l),i.push(a))}return i}function _(n){return function(r,t,e){var u={};t=Z(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++e<o;){var i=r[e];n(u,i,t(i,e,r),r)}else h(r,function(r,e,o){n(u,r,t(r,e,o),o)});return u}}function d(n,r,t,e,u){var o,i=r&ir,f=r&lr,a=r&cr;
if(!i&&!K(n))throw new TypeError;return f&&!u.length&&(r&=~lr,u=false),a&&!o.length&&(r&=~cr,o=false),null==t?t=i?0:n.length:0>t&&(t=0),n=[n,r,t,e,u,o],r==or||r==(or|lr)?l(n):s(n)}function w(){var n=(n=f.indexOf)===A?t:n;return n}function j(n){return typeof n=="function"&&Wr.test(Pr.call(n))}function x(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Dr.call(n)==br||false}function T(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=Z(r,t,3);++o<u&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[0]:ur;
return O(n,0,0<e?e:0)}function A(n,r,e){var u=n?n.length:0;if(typeof e=="number")e=0>e?Yr(0,u+e):e||0;else if(e)return e=k(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function E(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=Z(r,t,3);++u<o&&r(n[u],u,n);)e++}else e=null==r||t?1:0<r?r:0;return O(n,e)}function O(n,r,t){var e=-1,u=n?n.length:0;for(typeof r=="undefined"?r=0:0>r?r=Yr(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Yr(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++e<u;)t[e]=n[r+e];
return t}function k(n,r,t,e){var u=0,o=n?n.length:u;for(t=t?Z(t,e,1):nr,r=t(r);u<o;)e=u+o>>>1,t(n[e])<r?u=e+1:o=e;return u}function S(n,r,t,e){var u=typeof r;return"boolean"!=u&&null!=r&&(e=t,t=r,r=false,"number"!=u&&"string"!=u||!e||e[t]!==n||(t=null)),null!=t&&(t=Z(t,e,3)),b(n,r,t)}function N(n,r){var t=w(),e=n?n.length:0,u=false;return e&&typeof e=="number"?u=-1<t(n,r):h(n,function(n){return(u=n===r)&&sr}),u}function q(n,r,t){var e=true;r=Z(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++t<u;)if(!r(n[t],t,n))return false
}else h(n,function(n,t,u){return!(e=!!r(n,t,u))&&sr});return e}function F(n,r,t){var e=[];r=Z(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++t<u;){var o=n[t];r(o,t,n)&&e.push(o)}else h(n,function(n,t,u){r(n,t,u)&&e.push(n)});return e}function B(n,r,t){r=Z(r,t,3),t=-1;var e=n?n.length:0;if(typeof e!="number"){var u;return h(n,function(n,t,e){return r(n,t,e)?(u=n,sr):void 0}),u}for(;++t<e;){var o=n[t];if(r(o,t,n))return o}}function R(n,r,t){var e=-1,u=n?n.length:0;if(r=r&&typeof t=="undefined"?r:p(r,t,3),typeof u=="number")for(;++e<u&&r(n[e],e,n)!==sr;);else h(n,r)
}function $(n,r,t){var e=-1,u=n?n.length:0;if(r=Z(r,t,3),typeof u=="number")for(var o=Array(u);++e<u;)o[e]=r(n[e],e,n);else o=[],h(n,function(n,t,u){o[++e]=r(n,t,u)});return o}function I(n,r,t){var e=-1/0,u=e,o=typeof r;"number"!=o&&"string"!=o||!t||t[r]!==n||(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++o<i;)t=n[o],t>u&&(u=t);else r=Z(r,t,3),h(n,function(n,t,o){t=r(n,t,o),t>e&&(e=t,u=n)});return u}function M(n,r,t,e){var u=3>arguments.length;r=Z(r,e,4);var o=-1,i=n?n.length:0;
if(typeof i=="number")for(u&&i&&(t=n[++o]);++o<i;)t=r(t,n[o],o,n);else h(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)});return t}function D(n,r,t,e){var u=3>arguments.length;return r=Z(r,e,4),v(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)}),t}function W(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return h(n,function(n){var t;t=++r,t=0+Cr(rt()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function z(n,r,t){var e;r=Z(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++t<u;)if(r(n[t],t,n))return true
}else h(n,function(n,t,u){return(e=r(n,t,u))&&sr});return!!e}function C(n,r,t){return t&&J(r)?ur:(t?B:F)(n,r)}function P(n,r){if(3>arguments.length)return d(n,or,null,r);if(n)var t=n[pr]?n[pr][2]:n.length,e=O(arguments,2),t=t-e.length;return d(n,or|lr,t,r,e)}function U(n,r,t){function e(){c&&clearTimeout(c),i=c=p=ur,(h||g!==r)&&(s=lt(),f=n.apply(l,o),c||i||(o=l=null))}function u(){var t=r-(lt()-a);0<t?c=setTimeout(u,t):(i&&clearTimeout(i),t=p,i=c=p=ur,t&&(s=lt(),f=n.apply(l,o),c||i||(o=l=null)))}var o,i,f,a,l,c,p,s=0,g=false,h=true;
if(!K(n))throw new TypeError;if(r=Yr(0,r)||0,true===t)var v=true,h=false;else L(t)&&(v=t.leading,g="maxWait"in t&&(Yr(r,t.maxWait)||0),h="trailing"in t?t.trailing:h);return function(){if(o=arguments,a=lt(),l=this,p=h&&(c||!v),false===g)var t=v&&!c;else{i||v||(s=a);var y=g-(a-s),m=0>=y;m?(i&&(i=clearTimeout(i)),s=a,f=n.apply(l,o)):i||(i=setTimeout(e,y))}return m&&c?c=clearTimeout(c):c||r===g||(c=setTimeout(u,r)),t&&(m=true,f=n.apply(l,o)),!m||c||i||(o=l=null),f}}function V(n,r,t){if(!n)return n;var e=arguments,u=0,o=e.length,i=typeof t;
for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u<o;)if(r=e[u])for(var f in r)n[f]=r[f];return n}function G(n,r,t){if(!n)return n;var e=arguments,u=0,o=e.length,i=typeof t;for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u<o;)if(r=e[u])for(var f in r)"undefined"==typeof n[f]&&(n[f]=r[f]);return n}function H(n){var t=[];return r(n,function(n,r){K(n)&&t.push(r)}),t.sort()}function J(n){if(!n)return true;if(ft(n)||X(n))return!n.length;for(var r in n)if(Ur.call(n,r))return false;return true}function K(n){return typeof n=="function"
}function L(n){var r=typeof n;return n&&("function"==r||"object"==r)||false}function Q(n){var r=typeof n;return"number"==r||n&&"object"==r&&Dr.call(n)==jr||false}function X(n){return typeof n=="string"||n&&typeof n=="object"&&Dr.call(n)==Ar||false}function Y(n){for(var r=-1,t=at(n),e=t.length,u=Array(e);++r<e;)u[r]=n[t[r]];return u}function Z(n,r,t){var e=typeof n;return"function"==e||null==n?(typeof r=="undefined"||!("prototype"in n))&&n||p(n,r,t):"object"!=e?er(n):rr(n)}function nr(n){return n}function rr(n){n||(n={});
var r=at(n);return function(t){for(var e=r.length,u=false;e--&&(u=r[e],u=Ur.call(t,u)&&t[u]===n[u]););return u}}function tr(n){for(var r=-1,t=H(n),e=t.length;++r<e;){var u=t[r];f.prototype[u]=function(){var r=f[u]=n[u];return function(){var n=[this.__wrapped__];return Vr.apply(n,arguments),n=r.apply(f,n),this.__chain__?new a(n,true):n}}()}}function er(n){return function(r){return r[n]}}var ur,or=1,ir=2,fr=4,ar=8,lr=16,cr=32,pr="__lodash@2.4.1__",sr=pr+"breaker__",gr=0,hr=/&(?:amp|lt|gt|quot|#x27);/g,vr=/[&<>"']/g,yr=/($^)/,mr=/['\n\r\t\u2028\u2029\\]/g,br="[object Arguments]",_r="[object Array]",dr="[object Boolean]",wr="[object Date]",jr="[object Number]",xr="[object Object]",Tr="[object RegExp]",Ar="[object String]",Er={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;"},Or={"&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&#x27;":"'"},kr={"function":true,object:true},Sr={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},Nr=kr[typeof window]&&window||this,qr=kr[typeof exports]&&exports&&!exports.nodeType&&exports,Fr=kr[typeof module]&&module&&!module.nodeType&&module,Br=qr&&Fr&&typeof global=="object"&&global;
!Br||Br.global!==Br&&Br.window!==Br&&Br.self!==Br||(Nr=Br);var Rr=Fr&&Fr.exports===qr&&qr,$r=Array.prototype,Ir=Object.prototype,Mr=Nr._,Dr=Ir.toString,Wr=RegExp("^"+(Dr+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),zr=Math.ceil,Cr=Math.floor,Pr=Function.prototype.toString,Ur=Ir.hasOwnProperty,Vr=$r.push,Gr=Ir.propertyIsEnumerable,Hr=$r.splice,Jr=j(Jr=Object.create)&&Jr,Kr=j(Kr=Array.isArray)&&Kr,Lr=Nr.isFinite,Qr=Nr.isNaN,Xr=j(Xr=Object.keys)&&Xr,Yr=Math.max,Zr=Math.min,nt=j(nt=Date.now)&&nt,rt=Math.random;
a.prototype=f.prototype;var tt={};!function(){var n={0:1,length:1};tt.spliceObjects=(Hr.call(n,0,1),!n[0])}(1),f.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Jr||(c=function(){function n(){}return function(r){if(L(r)){n.prototype=r;var t=new n;n.prototype=null}return t||Nr.Object()}}()),x(arguments)||(x=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ur.call(n,"callee")&&!Gr.call(n,"callee")||false});var et=_(function(n,r,t){Ur.call(n,t)?n[t]++:n[t]=1
}),ut=_(function(n,r,t){Ur.call(n,t)?n[t].push(r):n[t]=[r]}),ot=_(function(n,r,t){n[t]=r}),it=$,ft=Kr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Dr.call(n)==_r||false};K(/x/)&&(K=function(n){return typeof n=="function"&&"[object Function]"==Dr.call(n)});var at=Xr?function(n){return L(n)?Xr(n):[]}:n,lt=nt||function(){return(new Date).getTime()};f.after=function(n,r){if(!K(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},f.bind=P,f.bindAll=function(n){for(var r=1<arguments.length?y(arguments,true,false,1):H(n),t=-1,e=r.length;++t<e;){var u=r[t];
n[u]=d(n[u],or,null,n)}return n},f.chain=function(n){return n=new a(n),n.__chain__=true,n},f.compact=function(n){for(var r=-1,t=n?n.length:0,e=0,u=[];++r<t;){var o=n[r];o&&(u[e++]=o)}return u},f.compose=function(){for(var n=arguments,r=n.length,t=r;t--;)if(!K(n[t]))throw new TypeError;return function(){for(var t=r-1,e=n[t].apply(this,arguments);t--;)e=n[t].call(this,e);return e}},f.countBy=et,f.debounce=U,f.defaults=G,f.defer=function(n){if(!K(n))throw new TypeError;var r=O(arguments,1);return setTimeout(function(){n.apply(ur,r)
},1)},f.delay=function(n,r){if(!K(n))throw new TypeError;var t=O(arguments,2);return setTimeout(function(){n.apply(ur,t)},r)},f.difference=function(n){return g(n,y(arguments,true,true,1))},f.filter=F,f.flatten=function(n,r,t){var e=typeof r;return"number"!=e&&"string"!=e||!t||t[r]!==n||(r=false),y(n,r)},f.forEach=R,f.functions=H,f.groupBy=ut,f.indexBy=ot,f.initial=function(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=u;for(r=Z(r,t,3);o--&&r(n[o],o,n);)e++}else e=null==r||t?1:r||e;
return e=u-e,O(n,0,0<e?e:0)},f.intersection=function(){for(var n=[],r=-1,t=arguments.length;++r<t;){var e=arguments[r];(ft(e)||x(e))&&n.push(e)}var u=n[0],o=-1,i=w(),f=u?u.length:0,a=[];n:for(;++o<f;)if(e=u[o],0>i(a,e)){for(r=t;--r;)if(0>i(n[r],e))continue n;a.push(e)}return a},f.invert=function(n){for(var r=-1,t=at(n),e=t.length,u={};++r<e;){var o=t[r];u[n[o]]=o}return u},f.invoke=function(n,r){var t=-1,e=typeof r=="function",u=n?n.length:0,o=Array(typeof u=="number"?u:0);if(3>arguments.length&&typeof u=="number")for(;++t<u;){var i=n[t];
o[t]=e?r.call(i):i[r]()}else{var f=O(arguments,2);h(n,function(n){o[++t]=(e?r:n[r]).apply(n,f)})}return o},f.keys=at,f.map=$,f.max=I,f.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):"_"+arguments[0];return Ur.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},f.min=function(n,r,t){var e=1/0,u=e,o=typeof r;"number"!=o&&"string"!=o||!t||t[r]!==n||(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++o<i;)t=n[o],t<u&&(u=t);else r=Z(r,t,3),h(n,function(n,t,o){t=r(n,t,o),t<e&&(e=t,u=n)
});return u},f.omit=function(n){for(var t=y(arguments,true,false,1),e=t.length,u={};e--;)t[e]=t[e]+"";var o=[];r(n,function(n,r){o.push(r)});for(var i=-1,o=g(o,t),e=o.length;++i<e;)t=o[i],u[t]=n[t];return u},f.once=function(n){var r,t;if(!K(n))throw new TypeError;return function(){return r?t:(r=true,t=n.apply(this,arguments),n=null,t)}},f.pairs=function(n){for(var r=-1,t=at(n),e=t.length,u=Array(e);++r<e;){var o=t[r];u[r]=[o,n[o]]}return u},f.partial=function(n){if(n)var r=n[pr]?n[pr][2]:n.length,t=O(arguments,1),r=r-t.length;
return d(n,lr,r,null,t)},f.pick=function(n){for(var r=-1,t=y(arguments,true,false,1),e=t.length,u={};++r<e;){var o=t[r];o in n&&(u[o]=n[o])}return u},f.pluck=it,f.range=function(n,r,t){n=+n||0,t=+t||1,null==r&&(r=n,n=0);var e=-1;r=Yr(0,zr((r-n)/t));for(var u=Array(r);++e<r;)u[e]=n,n+=t;return u},f.reject=function(n,r,t){return r=Z(r,t,3),F(n,function(n,t,e){return!r(n,t,e)})},f.rest=E,f.shuffle=W,f.sortBy=function(n,r,t){var u=-1,o=n?n.length:0,i=Array(typeof o=="number"?o:0);for(r=Z(r,t,3),h(n,function(n,t,e){i[++u]={f:r(n,t,e),g:u,h:n}
}),o=i.length,i.sort(e);o--;)i[o]=i[o].h;return i},f.tap=function(n,r){return r(n),n},f.throttle=function(n,r,t){var e=true,u=true;if(!K(n))throw new TypeError;return false===t?e=false:L(t)&&(e="leading"in t?t.leading:e,u="trailing"in t?t.trailing:u),t={},t.leading=e,t.maxWait=r,t.trailing=u,U(n,r,t)},f.times=function(n,r,t){n=-1<(n=+n)?n:0;var e=-1,u=Array(n);for(r=p(r,t,1);++e<n;)u[e]=r(e);return u},f.toArray=function(n){return ft(n)?O(n):n&&typeof n.length=="number"?$(n):Y(n)},f.union=function(){return b(y(arguments,true,true))
},f.uniq=S,f.values=Y,f.where=C,f.without=function(n){return g(n,O(arguments,1))},f.wrap=function(n,r){return d(r,lr,null,null,[n])},f.zip=function(){for(var n=-1,r=I(it(arguments,"length")),t=Array(0>r?0:r);++n<r;)t[n]=it(arguments,n);return t},f.collect=$,f.drop=E,f.each=R,f.extend=V,f.methods=H,f.object=function(n,r){var t=-1,e=n?n.length:0,u={};for(r||!e||ft(n[0])||(r=[]);++t<e;){var o=n[t];r?u[o]=r[t]:o&&(u[o[0]]=o[1])}return u},f.select=F,f.tail=E,f.unique=S,f.clone=function(n){return L(n)?ft(n)?O(n):V({},n):n
},f.contains=N,f.escape=function(n){return null==n?"":(n+"").replace(vr,u)},f.every=q,f.find=B,f.has=function(n,r){return n?Ur.call(n,r):false},f.identity=nr,f.indexOf=A,f.isArguments=x,f.isArray=ft,f.isBoolean=function(n){return true===n||false===n||n&&typeof n=="object"&&Dr.call(n)==dr||false},f.isDate=function(n){return n&&typeof n=="object"&&Dr.call(n)==wr||false},f.isElement=function(n){return n&&1===n.nodeType||false},f.isEmpty=J,f.isEqual=function(n,r){return m(n,r)},f.isFinite=function(n){return Lr(n)&&!Qr(parseFloat(n))
},f.isFunction=K,f.isNaN=function(n){return Q(n)&&n!=+n},f.isNull=function(n){return null===n},f.isNumber=Q,f.isObject=L,f.isRegExp=function(n){var r=typeof n;return n&&("function"==r||"object"==r)&&Dr.call(n)==Tr||false},f.isString=X,f.isUndefined=function(n){return typeof n=="undefined"},f.lastIndexOf=function(n,r,t){var e=n?n.length:0;for(typeof t=="number"&&(e=(0>t?Yr(0,e+t):Zr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},f.mixin=tr,f.noConflict=function(){return Nr._=Mr,this},f.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Cr(rt()*(r-n+1))
},f.reduce=M,f.reduceRight=D,f.result=function(n,r){if(null!=n){var t=n[r];return K(t)?n[r]():t}},f.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:at(n).length},f.some=z,f.sortedIndex=k,f.template=function(n,r,t){var e=f,u=e.templateSettings;n=(n||"")+"",t=G({},t,u);var i=0,a="__p+='",u=t.variable;n.replace(RegExp((t.escape||yr).source+"|"+(t.interpolate||yr).source+"|"+(t.evaluate||yr).source+"|$","g"),function(r,t,e,u,f){return a+=n.slice(i,f).replace(mr,o),t&&(a+="'+_.escape("+t+")+'"),u&&(a+="';"+u+";\n__p+='"),e&&(a+="'+((__t=("+e+"))==null?'':__t)+'"),i=f+r.length,r
}),a+="';",u||(u="obj",a="with("+u+"||{}){"+a+"}"),a="function("+u+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}";try{var l=Function("_","return "+a)(e)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},f.unescape=function(n){return null==n?"":(n+="",0>n.indexOf(";")?n:n.replace(hr,i))},f.uniqueId=function(n){var r=++gr+"";return n?n+r:r},f.all=q,f.any=z,f.detect=B,f.findWhere=function(n,r){return C(n,r,true)},f.foldl=M,f.foldr=D,f.include=N,f.inject=M,f.first=T,f.last=function(n,r,t){var e=0,u=n?n.length:0;
if(typeof r!="number"&&null!=r){var o=u;for(r=Z(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:ur;return e=u-e,O(n,0<e?e:0)},f.sample=function(n,r,t){return n&&typeof n.length!="number"&&(n=Y(n)),null==r||t?n?n[0+Cr(rt()*(n.length-1-0+1))]:ur:(n=W(n),n.length=Zr(Yr(0,r),n.length),n)},f.take=T,f.head=T,tr(V({},f)),f.VERSION="2.4.1",f.prototype.chain=function(){return this.__chain__=true,this},f.prototype.value=function(){return this.__wrapped__},h("pop push reverse shift sort splice unshift".split(" "),function(n){var r=$r[n];
f.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),tt.spliceObjects||0!==n.length||delete n[0],this}}),h(["concat","join","slice"],function(n){var r=$r[n];f.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new a(n),n.__chain__=true),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(Nr._=f, define(function(){return f})):qr&&Fr?Rr?(Fr.exports=f)._=f:qr._=f:Nr._=f}).call(this);
;(function(){function n(n){var r=[];if(!J(n))return r;for(var t in n)Cr.call(n,t)&&r.push(t);return r}function r(n,r){if(J(n))for(var t in n)if(r(n[t],t,n)===cr)break}function t(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++t<e;)if(n[t]===r)return t;return-1}function e(n,r){var t;n:{t=n.f;var e=r.f;if(t!==e){if(t>e||typeof t=="undefined"){t=1;break n}if(t<e||typeof e=="undefined"){t=-1;break n}}t=0}return t||n.g-r.g}function u(n){return Tr[n]}function o(n){return"\\"+Or[n]}function i(n){return Ar[n]}function f(n){return n instanceof f?n:new a(n)
}function a(n,r){this.__chain__=!!r,this.__wrapped__=n}function l(n){function r(){if(u){var n=O(u);Pr.apply(n,arguments)}if(this instanceof r){var o=c(t.prototype),n=t.apply(o,n||arguments);return J(n)?n:o}return t.apply(e,n||arguments)}var t=n[0],e=n[3],u=n[4];return r}function c(n){return J(n)?Gr(n):{}}function p(n,r,t){if(typeof n!="function")return Y;if(typeof r=="undefined"||!("prototype"in n))return n;switch(t){case 1:return function(t){return n.call(r,t)};case 2:return function(t,e){return n.call(r,t,e)
};case 3:return function(t,e,u){return n.call(r,t,e,u)};case 4:return function(t,e,u,o){return n.call(r,t,e,u,o)}}return C(n,r)}function s(n){function r(){var n=a?o:this;if(i){var v=O(i);Pr.apply(v,arguments)}if(f||p){v||(v=O(arguments)),f&&Pr.apply(v,f);var y=arguments.length;if(p&&y<u)return e|=fr,e&=~ar,g||(e&=~(er|ur)),n=Qr(0,u-y),s([t,e,n,o,v])}return v||(v=arguments),l&&(t=n[h]),this instanceof r?(n=c(t.prototype),v=t.apply(n,v),J(v)?v:n):t.apply(n,v)}var t=n[0],e=n[1],u=n[2],o=n[3],i=n[4],f=n[5],a=e&er,l=e&ur,p=e&or,g=e&ir,h=t;
return r}function g(n,r){for(var t=-1,e=w(),u=n?n.length:0,o=[];++t<u;){var i=n[t];0>e(r,i)&&o.push(i)}return o}function h(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(;++t<e&&r(n[t],t,n)!==cr;);else for(var t=-1,e=at(n),u=e.length;++t<u;){var o=e[t];if(r(n[o],o,n)===cr)break}}function v(n,r){var t=n?n.length:0;if(typeof t=="number")for(;t--&&r(n[t],t,n)!==cr;);else for(var t=at(n),e=t.length;e--;){var u=t[e];if(r(n[u],u,n)===cr)break}}function y(n,r,t,e){e=(e||0)-1;for(var u=n?n.length:0,o=[];++e<u;){var i=n[e];
if(i&&typeof i=="object"&&typeof i.length=="number"&&(ft(i)||x(i))){r||(i=y(i,r,t));var f=-1,a=i.length,l=o.length;for(o.length+=a;++f<a;)o[l++]=i[f]}else t||o.push(i)}return o}function m(n,t,e,u){if(n===t)return 0!==n||1/n==1/t;var o=typeof n,i=typeof t;if(n===n&&(!n||"function"!=o&&"object"!=o)&&(!t||"function"!=i&&"object"!=i))return false;if(null==n||null==t)return n===t;if(i=Ir.call(n),o=Ir.call(t),i!=o)return false;switch(i){case br:case _r:return+n==+t;case dr:return n!=+n?t!=+t:0==n?1/n==1/t:n==+t;
case jr:case xr:return n==t+""}if(o=i==mr,!o){var a=n instanceof f,l=t instanceof f;if(a||l)return m(a?n.__wrapped__:n,l?t.__wrapped__:t,e,u);if(i!=wr)return false;if(i=Cr.call(n,"constructor"),a=Cr.call(t,"constructor"),i!==a||!i&&(i=n.constructor,a=t.constructor,i!=a&&!(H(i)&&i instanceof i&&H(a)&&a instanceof a)&&"constructor"in n&&"constructor"in t))return false}for(e||(e=[]),u||(u=[]),i=e.length;i--;)if(e[i]==n)return u[i]==t;var c=true,p=0;if(e.push(n),u.push(t),o){if(p=t.length,c=p==n.length)for(;p--&&(c=m(n[p],t[p],e,u)););}else r(t,function(r,t,o){return Cr.call(o,t)?(p++,!(c=Cr.call(n,t)&&m(n[t],r,e,u))&&cr):void 0
}),c&&r(n,function(n,r,t){return Cr.call(t,r)?!(c=-1<--p)&&cr:void 0});return e.pop(),u.pop(),c}function b(n,r,t){for(var e=-1,u=w(),o=n?n.length:0,i=[],f=t?[]:i;++e<o;){var a=n[e],l=t?t(a,e,n):a;(r?!e||f[f.length-1]!==l:0>u(f,l))&&(t&&f.push(l),i.push(a))}return i}function _(n,r){return function(t,e,u){var o=r?[[],[]]:{};e=X(e,u,3),u=-1;var i=t?t.length:0;if(typeof i=="number")for(;++u<i;){var f=t[u];n(o,f,e(f,u,t),t)}else h(t,function(r,t,u){n(o,r,e(r,t,u),u)});return o}}function d(n,r,t,e,u){var o,i=r&ur,f=r&fr,a=r&ar;
if(!i&&!H(n))throw new TypeError;return f&&!u.length&&(r&=~fr,u=false),a&&!o.length&&(r&=~ar,o=false),null==t?t=i?0:n.length:0>t&&(t=0),n=[n,r,t,e,u,o],r==er||r==(er|fr)?l(n):s(n)}function w(){var n=(n=f.indexOf)===A?t:n;return n}function j(n){return typeof n=="function"&&Mr.test(zr.call(n))}function x(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ir.call(n)==yr||false}function T(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=-1;for(r=X(r,t,3);++o<u&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[0]:tr;
return O(n,0,0<e?e:0)}function A(n,r,e){var u=n?n.length:0;if(typeof e=="number")e=0>e?Qr(0,u+e):e||0;else if(e)return e=k(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function E(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=X(r,t,3);++u<o&&r(n[u],u,n);)e++}else e=null==r||t?1:0<r?r:0;return O(n,e)}function O(n,r,t){var e=-1,u=n?n.length:0;for(typeof r=="undefined"?r=0:0>r?r=Qr(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Qr(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++e<u;)t[e]=n[r+e];
return t}function k(n,r,t,e){var u=0,o=n?n.length:u;for(t=t?X(t,e,1):Y,r=t(r);u<o;)e=u+o>>>1,t(n[e])<r?u=e+1:o=e;return u}function S(n,r,t,e){var u=typeof r;return"boolean"!=u&&null!=r&&(e=t,t=r,r=false,"number"!=u&&"string"!=u||!e||e[t]!==n||(t=null)),null!=t&&(t=X(t,e,3)),b(n,r,t)}function N(n,r){var t=w(),e=n?n.length:0,u=false;return e&&typeof e=="number"?u=-1<t(n,r):h(n,function(n){return(u=n===r)&&cr}),u}function q(n,r,t){var e=true;r=X(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++t<u;)if(!r(n[t],t,n))return false
}else h(n,function(n,t,u){return!(e=!!r(n,t,u))&&cr});return e}function F(n,r,t){var e=[];r=X(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++t<u;){var o=n[t];r(o,t,n)&&e.push(o)}else h(n,function(n,t,u){r(n,t,u)&&e.push(n)});return e}function B(n,r,t){r=X(r,t,3),t=-1;var e=n?n.length:0;if(typeof e!="number"){var u;return h(n,function(n,t,e){return r(n,t,e)?(u=n,cr):void 0}),u}for(;++t<e;){var o=n[t];if(r(o,t,n))return o}}function R(n,r,t){var e=-1,u=n?n.length:0;if(r=r&&typeof t=="undefined"?r:p(r,t,3),typeof u=="number")for(;++e<u&&r(n[e],e,n)!==cr;);else h(n,r);
return n}function $(n,r,t){var e=-1,u=n?n.length:0;if(r=X(r,t,3),typeof u=="number")for(var o=Array(u);++e<u;)o[e]=r(n[e],e,n);else o=[],h(n,function(n,t,u){o[++e]=r(n,t,u)});return o}function I(n,r,t){var e=-1/0,u=e,o=typeof r;"number"!=o&&"string"!=o||!t||t[r]!==n||(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++o<i;)t=n[o],t>u&&(u=t);else r=X(r,t,3),h(n,function(n,t,o){t=r(n,t,o),t>e&&(e=t,u=n)});return u}function M(n,r,t,e){var u=3>arguments.length;r=X(r,e,4);var o=-1,i=n?n.length:0;
if(typeof i=="number")for(u&&i&&(t=n[++o]);++o<i;)t=r(t,n[o],o,n);else h(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)});return t}function D(n,r,t,e){var u=3>arguments.length;return r=X(r,e,4),v(n,function(n,e,o){t=u?(u=false,n):r(t,n,e,o)}),t}function W(n){var r=-1,t=n?n.length:0,e=Array(typeof t=="number"?t:0);return h(n,function(n){var t;t=++r,t=0+Wr(Zr()*(t-0+1)),e[r]=e[t],e[t]=n}),e}function z(n,r,t){var e;r=X(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number"){for(;++t<u;)if(r(n[t],t,n))return true
}else h(n,function(n,t,u){return(e=r(n,t,u))&&cr});return!!e}function C(n,r){if(3>arguments.length)return d(n,er,null,r);if(n)var t=n[lr]?n[lr][2]:n.length,e=O(arguments,2),t=t-e.length;return d(n,er|fr,t,r,e)}function P(n,r,t){function e(){c&&clearTimeout(c),i=c=p=tr,(h||g!==r)&&(s=lt(),f=n.apply(l,o),c||i||(o=l=null))}function u(){var t=r-(lt()-a);0<t?c=setTimeout(u,t):(i&&clearTimeout(i),t=p,i=c=p=tr,t&&(s=lt(),f=n.apply(l,o),c||i||(o=l=null)))}var o,i,f,a,l,c,p,s=0,g=false,h=true;if(!H(n))throw new TypeError;
if(r=Qr(0,r)||0,true===t)var v=true,h=false;else J(t)&&(v=t.leading,g="maxWait"in t&&(Qr(r,t.maxWait)||0),h="trailing"in t?t.trailing:h);return function(){if(o=arguments,a=lt(),l=this,p=h&&(c||!v),false===g)var t=v&&!c;else{i||v||(s=a);var y=g-(a-s),m=0>=y;m?(i&&(i=clearTimeout(i)),s=a,f=n.apply(l,o)):i||(i=setTimeout(e,y))}return m&&c?c=clearTimeout(c):c||r===g||(c=setTimeout(u,r)),t&&(m=true,f=n.apply(l,o)),!m||c||i||(o=l=null),f}}function U(n,r,t){if(!n)return n;var e=arguments,u=0,o=e.length,i=typeof t;for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u<o;)if(r=e[u])for(var f in r)n[f]=r[f];
return n}function V(n,r,t){if(!n)return n;var e=arguments,u=0,o=e.length,i=typeof t;for("number"!=i&&"string"!=i||!e[3]||e[3][t]!==r||(o=2);++u<o;)if(r=e[u])for(var f in r)"undefined"==typeof n[f]&&(n[f]=r[f]);return n}function G(n){var t=[];return r(n,function(n,r){H(n)&&t.push(r)}),t.sort()}function H(n){return typeof n=="function"}function J(n){var r=typeof n;return n&&("function"==r||"object"==r)||false}function K(n){var r=typeof n;return"number"==r||n&&"object"==r&&Ir.call(n)==dr||false}function L(n){return typeof n=="string"||n&&typeof n=="object"&&Ir.call(n)==xr||false
}function Q(n){for(var r=-1,t=at(n),e=t.length,u=Array(e);++r<e;)u[r]=n[t[r]];return u}function X(n,r,t){var e=typeof n;return"function"==e||null==n?(typeof r=="undefined"||!("prototype"in n))&&n||p(n,r,t):"object"!=e?rr(n):Z(n)}function Y(n){return n}function Z(n){n||(n={});var r=at(n);return function(t){for(var e=r.length,u=true;e--&&(u=r[e],u=Cr.call(t,u)&&t[u]===n[u]););return u}}function nr(n){for(var r=-1,t=G(n),e=t.length;++r<e;){var u=t[r];f.prototype[u]=function(){var r=f[u]=n[u];return function(){var n=[this.__wrapped__];
return Pr.apply(n,arguments),n=r.apply(f,n),this.__chain__?new a(n,true):n}}()}}function rr(n){return function(r){return r[n]}}var tr,er=1,ur=2,or=4,ir=8,fr=16,ar=32,lr="__lodash@2.4.1__",cr=lr+"breaker__",pr=0,sr=/&(?:amp|lt|gt|quot|#x27);/g,gr=/[&<>"']/g,hr=/($^)/,vr=/['\n\r\t\u2028\u2029\\]/g,yr="[object Arguments]",mr="[object Array]",br="[object Boolean]",_r="[object Date]",dr="[object Number]",wr="[object Object]",jr="[object RegExp]",xr="[object String]",Tr={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;"},Ar={"&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&#x27;":"'"},Er={"function":true,object:true},Or={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},kr=Er[typeof window]&&window||this,Sr=Er[typeof exports]&&exports&&!exports.nodeType&&exports,Nr=Er[typeof module]&&module&&!module.nodeType&&module,qr=Sr&&Nr&&typeof global=="object"&&global;
!qr||qr.global!==qr&&qr.window!==qr&&qr.self!==qr||(kr=qr);var Fr=Nr&&Nr.exports===Sr&&Sr,Br=Array.prototype,Rr=Object.prototype,$r=kr._,Ir=Rr.toString,Mr=RegExp("^"+(Ir+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/toString|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$"),Dr=Math.ceil,Wr=Math.floor,zr=Function.prototype.toString,Cr=Rr.hasOwnProperty,Pr=Br.push,Ur=Rr.propertyIsEnumerable,Vr=Br.splice,Gr=j(Gr=Object.create)&&Gr,Hr=j(Hr=Array.isArray)&&Hr,Jr=kr.isFinite,Kr=kr.isNaN,Lr=j(Lr=Object.keys)&&Lr,Qr=Math.max,Xr=Math.min,Yr=j(Yr=Date.now)&&Yr,Zr=Math.random;
a.prototype=f.prototype;var nt={};!function(){var n={0:1,length:1};nt.spliceObjects=(Vr.call(n,0,1),!n[0])}(1),f.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Gr||(c=function(){function n(){}return function(r){if(J(r)){n.prototype=r;var t=new n;n.prototype=null}return t||kr.Object()}}()),x(arguments)||(x=function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Cr.call(n,"callee")&&!Ur.call(n,"callee")||false});var rt=_(function(n,r,t){Cr.call(n,t)?n[t]++:n[t]=1
}),tt=_(function(n,r,t){Cr.call(n,t)?n[t].push(r):n[t]=[r]}),et=_(function(n,r,t){n[t]=r}),ut=_(function(n,r,t){n[t?0:1].push(r)},true),ot=$,it=F,ft=Hr||function(n){return n&&typeof n=="object"&&typeof n.length=="number"&&Ir.call(n)==mr||false};H(/x/)&&(H=function(n){return typeof n=="function"&&"[object Function]"==Ir.call(n)});var at=Lr?function(n){return J(n)?Lr(n):[]}:n,lt=Yr||function(){return(new Date).getTime()};f.after=function(n,r){if(!H(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0
}},f.bind=C,f.bindAll=function(n){for(var r=1<arguments.length?y(arguments,true,false,1):G(n),t=-1,e=r.length;++t<e;){var u=r[t];n[u]=d(n[u],er,null,n)}return n},f.chain=function(n){return n=new a(n),n.__chain__=true,n},f.compact=function(n){for(var r=-1,t=n?n.length:0,e=0,u=[];++r<t;){var o=n[r];o&&(u[e++]=o)}return u},f.compose=function(){for(var n=arguments,r=n.length,t=r;t--;)if(!H(n[t]))throw new TypeError;return function(){for(var t=r-1,e=n[t].apply(this,arguments);t--;)e=n[t].call(this,e);return e
}},f.constant=function(n){return function(){return n}},f.countBy=rt,f.debounce=P,f.defaults=V,f.defer=function(n){if(!H(n))throw new TypeError;var r=O(arguments,1);return setTimeout(function(){n.apply(tr,r)},1)},f.delay=function(n,r){if(!H(n))throw new TypeError;var t=O(arguments,2);return setTimeout(function(){n.apply(tr,t)},r)},f.difference=function(n){return g(n,y(arguments,true,true,1))},f.filter=F,f.flatten=function(n,r,t){var e=typeof r;return"number"!=e&&"string"!=e||!t||t[r]!==n||(r=false),y(n,r)
},f.forEach=R,f.functions=G,f.groupBy=tt,f.indexBy=et,f.initial=function(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=u;for(r=X(r,t,3);o--&&r(n[o],o,n);)e++}else e=null==r||t?1:r||e;return e=u-e,O(n,0,0<e?e:0)},f.intersection=function(){for(var n=[],r=-1,t=arguments.length;++r<t;){var e=arguments[r];(ft(e)||x(e))&&n.push(e)}var u=n[0],o=-1,i=w(),f=u?u.length:0,a=[];n:for(;++o<f;)if(e=u[o],0>i(a,e)){for(r=t;--r;)if(0>i(n[r],e))continue n;a.push(e)}return a},f.invert=function(n){for(var r=-1,t=at(n),e=t.length,u={};++r<e;){var o=t[r];
u[n[o]]=o}return u},f.invoke=function(n,r){var t=-1,e=typeof r=="function",u=n?n.length:0,o=Array(typeof u=="number"?u:0);if(3>arguments.length&&typeof u=="number")for(;++t<u;){var i=n[t];o[t]=e?r.call(i):i[r]()}else{var f=O(arguments,2);h(n,function(n){o[++t]=(e?r:n[r]).apply(n,f)})}return o},f.keys=at,f.map=$,f.matches=Z,f.max=I,f.memoize=function(n,r){var t={};return function(){var e=r?r.apply(this,arguments):"_"+arguments[0];return Cr.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},f.min=function(n,r,t){var e=1/0,u=e,o=typeof r;
"number"!=o&&"string"!=o||!t||t[r]!==n||(r=null);var o=-1,i=n?n.length:0;if(null==r&&typeof i=="number")for(;++o<i;)t=n[o],t<u&&(u=t);else r=X(r,t,3),h(n,function(n,t,o){t=r(n,t,o),t<e&&(e=t,u=n)});return u},f.omit=function(n){for(var t=y(arguments,true,false,1),e=t.length,u={};e--;)t[e]=t[e]+"";var o=[];r(n,function(n,r){o.push(r)});for(var i=-1,o=g(o,t),e=o.length;++i<e;)t=o[i],u[t]=n[t];return u},f.once=function(n){var r,t;if(!H(n))throw new TypeError;return function(){return r?t:(r=true,t=n.apply(this,arguments),n=null,t)
}},f.pairs=function(n){for(var r=-1,t=at(n),e=t.length,u=Array(e);++r<e;){var o=t[r];u[r]=[o,n[o]]}return u},f.partial=function(n){if(n)var r=n[lr]?n[lr][2]:n.length,t=O(arguments,1),r=r-t.length;return d(n,fr,r,null,t)},f.partition=ut,f.pick=function(n){for(var r=-1,t=y(arguments,true,false,1),e=t.length,u={};++r<e;){var o=t[r];o in n&&(u[o]=n[o])}return u},f.pluck=ot,f.property=rr,f.range=function(n,r,t){n=+n||0,t=+t||1,null==r&&(r=n,n=0);var e=-1;r=Qr(0,Dr((r-n)/t));for(var u=Array(r);++e<r;)u[e]=n,n+=t;
return u},f.reject=function(n,r,t){return r=X(r,t,3),F(n,function(n,t,e){return!r(n,t,e)})},f.rest=E,f.shuffle=W,f.sortBy=function(n,r,t){var u=-1,o=n?n.length:0,i=Array(typeof o=="number"?o:0);for(r=X(r,t,3),h(n,function(n,t,e){i[++u]={f:r(n,t,e),g:u,h:n}}),o=i.length,i.sort(e);o--;)i[o]=i[o].h;return i},f.tap=function(n,r){return r(n),n},f.throttle=function(n,r,t){var e=true,u=true;if(!H(n))throw new TypeError;return false===t?e=false:J(t)&&(e="leading"in t?t.leading:e,u="trailing"in t?t.trailing:u),t={},t.leading=e,t.maxWait=r,t.trailing=u,P(n,r,t)
},f.times=function(n,r,t){n=-1<(n=+n)?n:0;var e=-1,u=Array(n);for(r=p(r,t,1);++e<n;)u[e]=r(e);return u},f.toArray=function(n){return ft(n)?O(n):n&&typeof n.length=="number"?$(n):Q(n)},f.union=function(){return b(y(arguments,true,true))},f.uniq=S,f.values=Q,f.where=it,f.without=function(n){return g(n,O(arguments,1))},f.wrap=function(n,r){return d(r,fr,null,null,[n])},f.zip=function(){for(var n=-1,r=I(ot(arguments,"length")),t=Array(0>r?0:r);++n<r;)t[n]=ot(arguments,n);return t},f.collect=$,f.drop=E,f.each=R,f.extend=U,f.methods=G,f.object=function(n,r){var t=-1,e=n?n.length:0,u={};
for(r||!e||ft(n[0])||(r=[]);++t<e;){var o=n[t];r?u[o]=r[t]:o&&(u[o[0]]=o[1])}return u},f.select=F,f.tail=E,f.unique=S,f.clone=function(n){return J(n)?ft(n)?O(n):U({},n):n},f.contains=N,f.escape=function(n){return null==n?"":(n+"").replace(gr,u)},f.every=q,f.find=B,f.has=function(n,r){return n?Cr.call(n,r):false},f.identity=Y,f.indexOf=A,f.isArguments=x,f.isArray=ft,f.isBoolean=function(n){return true===n||false===n||n&&typeof n=="object"&&Ir.call(n)==br||false},f.isDate=function(n){return n&&typeof n=="object"&&Ir.call(n)==_r||false
},f.isElement=function(n){return n&&1===n.nodeType||false},f.isEmpty=function(n){if(!n)return true;if(ft(n)||L(n))return!n.length;for(var r in n)if(Cr.call(n,r))return false;return true},f.isEqual=function(n,r){return m(n,r)},f.isFinite=function(n){return Jr(n)&&!Kr(parseFloat(n))},f.isFunction=H,f.isNaN=function(n){return K(n)&&n!=+n},f.isNull=function(n){return null===n},f.isNumber=K,f.isObject=J,f.isRegExp=function(n){var r=typeof n;return n&&("function"==r||"object"==r)&&Ir.call(n)==jr||false},f.isString=L,f.isUndefined=function(n){return typeof n=="undefined"
},f.lastIndexOf=function(n,r,t){var e=n?n.length:0;for(typeof t=="number"&&(e=(0>t?Qr(0,e+t):Xr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},f.mixin=nr,f.noConflict=function(){return kr._=$r,this},f.now=lt,f.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0,n+Wr(Zr()*(r-n+1))},f.reduce=M,f.reduceRight=D,f.result=function(n,r){if(null!=n){var t=n[r];return H(t)?n[r]():t}},f.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:at(n).length},f.some=z,f.sortedIndex=k,f.template=function(n,r,t){var e=f,u=e.templateSettings;
n=(n||"")+"",t=V({},t,u);var i=0,a="__p+='",u=t.variable;n.replace(RegExp((t.escape||hr).source+"|"+(t.interpolate||hr).source+"|"+(t.evaluate||hr).source+"|$","g"),function(r,t,e,u,f){return a+=n.slice(i,f).replace(vr,o),t&&(a+="'+_.escape("+t+")+'"),u&&(a+="';"+u+";\n__p+='"),e&&(a+="'+((__t=("+e+"))==null?'':__t)+'"),i=f+r.length,r}),a+="';",u||(u="obj",a="with("+u+"||{}){"+a+"}"),a="function("+u+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+a+"return __p}";
try{var l=Function("_","return "+a)(e)}catch(c){throw c.source=a,c}return r?l(r):(l.source=a,l)},f.unescape=function(n){return null==n?"":(n+="",0>n.indexOf(";")?n:n.replace(sr,i))},f.uniqueId=function(n){var r=++pr+"";return n?n+r:r},f.all=q,f.any=z,f.detect=B,f.findWhere=B,f.foldl=M,f.foldr=D,f.include=N,f.inject=M,f.first=T,f.last=function(n,r,t){var e=0,u=n?n.length:0;if(typeof r!="number"&&null!=r){var o=u;for(r=X(r,t,3);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n?n[u-1]:tr;return e=u-e,O(n,0<e?e:0)
},f.sample=function(n,r,t){return n&&typeof n.length!="number"&&(n=Q(n)),null==r||t?n?n[0+Wr(Zr()*(n.length-1-0+1))]:tr:(n=W(n),n.length=Xr(Qr(0,r),n.length),n)},f.take=T,f.head=T,nr(U({},f)),f.VERSION="2.4.1",f.prototype.chain=function(){return this.__chain__=true,this},f.prototype.value=function(){return this.__wrapped__},h("pop push reverse shift sort splice unshift".split(" "),function(n){var r=Br[n];f.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),nt.spliceObjects||0!==n.length||delete n[0],this
}}),h(["concat","join","slice"],function(n){var r=Br[n];f.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new a(n),n.__chain__=true),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(kr._=f, define(function(){return f})):Sr&&Nr?Fr?(Nr.exports=f)._=f:Sr._=f:kr._=f}).call(this);

View File

@@ -56,7 +56,7 @@
'throws an error for empty arrays with no initial value'
],
'where': [
'Only get the first object matched.'
'4'
]
},
'Functions': {
@@ -67,6 +67,12 @@
'bindAll': [
'throws an error for bindAll with no functions named'
],
'partial': [
'can partially apply with placeholders',
'accepts more arguments than the number of placeholders',
'accepts fewer arguments than the number of placeholders',
'unfilled placeholders are undefined'
],
'throttle repeatedly with results': true,
'more throttle does not trigger leading call when leading is set to false': true,
'throttle does not trigger trailing call when trailing is set to false': true,
@@ -76,13 +82,6 @@
'isEqual': [
'Died on test #60',
'Died on test #63'
],
'keys': [
'throws an error for `null` values',
'throws an error for `undefined` values',
'throws an error for number primitives',
'throws an error for string primitives',
'throws an error for boolean primitives'
]
},
'Utility': {

View File

@@ -1,4 +1,4 @@
Copyright (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative
Copyright (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative
Reporters & Editors
Permission is hereby granted, free of charge, to any person

View File

@@ -1,11 +1,11 @@
$(document).ready(function() {
(function() {
module("Arrays");
module('Arrays');
test("first", function() {
test('first', function() {
equal(_.first([1,2,3]), 1, 'can pull out the first element of an array');
equal(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
equal(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
equal(_.first([1,2,3], 0).join(', '), '', 'can pass an index to first');
equal(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
equal(_.first([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to first');
var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);
@@ -16,12 +16,13 @@ $(document).ready(function() {
equal(result.join(','), '1,2', 'aliased as take');
equal(_.first(null), undefined, 'handles nulls');
strictEqual(_.first([1, 2, 3], -1).length, 0);
});
test("rest", function() {
test('rest', function() {
var numbers = [1, 2, 3, 4];
equal(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
equal(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
equal(_.rest(numbers).join(', '), '2, 3, 4', 'working rest()');
equal(_.rest(numbers, 0).join(', '), '1, 2, 3, 4', 'working rest(0)');
equal(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');
var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);
equal(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
@@ -31,18 +32,18 @@ $(document).ready(function() {
equal(result.join(', '), '2, 3, 4', 'aliased as drop and works on arguments object');
});
test("initial", function() {
equal(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
equal(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
test('initial', function() {
equal(_.initial([1,2,3,4,5]).join(', '), '1, 2, 3, 4', 'working initial()');
equal(_.initial([1,2,3,4],2).join(', '), '1, 2', 'initial can take an index');
var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);
equal(result.join(", "), "1, 2, 3", 'initial works on arguments object');
equal(result.join(', '), '1, 2, 3', 'initial works on arguments object');
result = _.map([[1,2,3],[1,2,3]], _.initial);
equal(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
});
test("last", function() {
test('last', function() {
equal(_.last([1,2,3]), 3, 'can pull out the last element of an array');
equal(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
equal(_.last([1,2,3], 0).join(', '), '', 'can pass an index to last');
equal(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
equal(_.last([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to last');
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
@@ -51,15 +52,16 @@ $(document).ready(function() {
equal(result.join(','), '3,3', 'works well with _.map');
equal(_.last(null), undefined, 'handles nulls');
strictEqual(_.last([1, 2, 3], -1).length, 0);
});
test("compact", function() {
test('compact', function() {
equal(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
var result = (function(){ return _.compact(arguments).length; })(0, 1, false, 2, false, 3);
equal(result, 3, 'works on an arguments object');
});
test("flatten", function() {
test('flatten', function() {
var list = [1, [2], [3, [[[4]]]]];
deepEqual(_.flatten(list), [1,2,3,4], 'can flatten nested arrays');
deepEqual(_.flatten(list, true), [1,2,3,[[[4]]]], 'can shallowly flatten nested arrays');
@@ -69,7 +71,7 @@ $(document).ready(function() {
deepEqual(_.flatten(list, true), [1, 2, 3, [4]], 'can shallowly flatten arrays containing only other arrays');
});
test("without", function() {
test('without', function() {
var list = [1, 2, 1, 0, 3, 1, 4];
equal(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
@@ -80,7 +82,17 @@ $(document).ready(function() {
ok(_.without(list, list[0]).length == 1, 'ditto.');
});
test("uniq", function() {
test('partition', function() {
var list = [0, 1, 2, 3, 4, 5];
function inspect(x) { return x instanceof Array ? '[' + _.map(x, inspect) + ']' : '' + x; }
equal(inspect(_.partition(list, function(x) { return x < 4; })), '[[0,1,2,3],[4,5]]', 'handles bool return values');
equal(inspect(_.partition(list, function(x) { return x & 1; })), '[[1,3,5],[0,2,4]]', 'handles 0 and 1 return values');
equal(inspect(_.partition(list, function(x) { return x - 3; })), '[[0,1,2,4,5],[3]]', 'handles other numeric return values');
equal(inspect(_.partition(list, function(x) { return x > 1 ? null : true; })), '[[0,1],[2,3,4,5]]', 'handles null return values');
equal(inspect(_.partition(list, function(x) { if(x < 2) return true; })), '[[0,1],[2,3,4,5]]', 'handles undefined return values');
});
test('uniq', function() {
var list = [1, 2, 1, 3, 1, 4];
equal(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
@@ -101,7 +113,7 @@ $(document).ready(function() {
equal(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
});
test("intersection", function() {
test('intersection', function() {
var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
equal(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
equal(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
@@ -111,7 +123,7 @@ $(document).ready(function() {
equal(_.intersection(theSixStooges, leaders).join(''), 'moe', 'returns a duplicate-free array');
});
test("union", function() {
test('union', function() {
var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
equal(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
@@ -127,7 +139,7 @@ $(document).ready(function() {
deepEqual(result, [null, 1, 2, 3]);
});
test("difference", function() {
test('difference', function() {
var result = _.difference([1, 2, 3], [2, 30, 40]);
equal(result.join(' '), '1 3', 'takes the difference of two arrays');
@@ -167,7 +179,7 @@ $(document).ready(function() {
ok(_.isEqual(_.object(null), {}), 'handles nulls');
});
test("indexOf", function() {
test('indexOf', function() {
var numbers = [1, 2, 3];
numbers.indexOf = null;
equal(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
@@ -193,7 +205,7 @@ $(document).ready(function() {
equal(index, 7, 'supports the fromIndex argument');
});
test("lastIndexOf", function() {
test('lastIndexOf', function() {
var numbers = [1, 0, 1];
equal(_.lastIndexOf(numbers, 1), 2);
@@ -210,7 +222,7 @@ $(document).ready(function() {
equal(index, 1, 'supports the fromIndex argument');
});
test("range", function() {
test('range', function() {
equal(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
equal(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
equal(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a &amp; b, a&lt;b generates an array of elements a,a+1,a+2,...,b-2,b-1');
@@ -221,4 +233,4 @@ $(document).ready(function() {
equal(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
});
});
})();

View File

@@ -1,8 +1,8 @@
$(document).ready(function() {
(function() {
module("Chaining");
module('Chaining');
test("map/flatten/reduce", function() {
test('map/flatten/reduce', function() {
var lyrics = [
"I'm a lumberjack and I'm okay",
"I sleep all night and I work all day",
@@ -20,7 +20,7 @@ $(document).ready(function() {
ok(counts.a == 16 && counts.e == 10, 'counted all the letters in the song');
});
test("select/reject/sortBy", function() {
test('select/reject/sortBy', function() {
var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers = _(numbers).chain().select(function(n) {
return n % 2 === 0;
@@ -29,10 +29,10 @@ $(document).ready(function() {
}).sortBy(function(n) {
return -n;
}).value();
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
equal(numbers.join(', '), '10, 6, 2', 'filtered and reversed the numbers');
});
test("select/reject/sortBy in functional style", function() {
test('select/reject/sortBy in functional style', function() {
var numbers = [1,2,3,4,5,6,7,8,9,10];
numbers = _.chain(numbers).select(function(n) {
return n % 2 === 0;
@@ -41,10 +41,10 @@ $(document).ready(function() {
}).sortBy(function(n) {
return -n;
}).value();
equal(numbers.join(', '), "10, 6, 2", "filtered and reversed the numbers");
equal(numbers.join(', '), '10, 6, 2', 'filtered and reversed the numbers');
});
test("reverse/concat/unshift/pop/map", function() {
test('reverse/concat/unshift/pop/map', function() {
var numbers = [1,2,3,4,5];
numbers = _(numbers).chain()
.reverse()
@@ -53,13 +53,13 @@ $(document).ready(function() {
.pop()
.map(function(n){ return n * 2; })
.value();
equal(numbers.join(', '), "34, 10, 8, 6, 4, 2, 10, 10", 'can chain together array functions.');
equal(numbers.join(', '), '34, 10, 8, 6, 4, 2, 10, 10', 'can chain together array functions.');
});
test("chaining works in small stages", function() {
test('chaining works in small stages', function() {
var o = _([1, 2, 3, 4]).chain();
deepEqual(o.filter(function(i) { return i < 3; }).value(), [1, 2]);
deepEqual(o.filter(function(i) { return i > 2; }).value(), [3, 4]);
});
});
})();

View File

@@ -1,8 +1,8 @@
$(document).ready(function() {
(function() {
module("Collections");
module('Collections');
test("each", function() {
test('each', function() {
_.each([1, 2, 3], function(num, i) {
equal(num, i + 1, 'each iterators provide value and iteration count');
});
@@ -19,7 +19,7 @@ $(document).ready(function() {
var obj = {one : 1, two : 2, three : 3};
obj.constructor.prototype.four = 4;
_.each(obj, function(value, key){ answers.push(key); });
equal(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
equal(answers.join(', '), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
delete obj.constructor.prototype.four;
var answer = null;
@@ -29,6 +29,12 @@ $(document).ready(function() {
answers = 0;
_.each(null, function(){ ++answers; });
equal(answers, 0, 'handles a null properly');
_.each(false, function(){});
var a = [1, 2, 3];
strictEqual(_.each(a, function(){}), a);
strictEqual(_.each(null, function(){}), null);
});
test('map', function() {
@@ -49,11 +55,10 @@ $(document).ready(function() {
deepEqual(ids, ['id1', 'id2'], 'Can use collection methods on NodeLists.');
}
var ids = _.map($('#map-test').children(), function(n){ return n.id; });
deepEqual(ids, ['id1', 'id2'], 'Can use collection methods on jQuery Array-likes.');
var ids = _.map(document.images, function(n){ return n.id; });
ok(ids[0] == 'chart_image', 'can use collection methods on HTMLCollections');
var ids = _.map({length: 2, 0: {id: '1'}, 1: {id: '2'}}, function(n){
return n.id;
});
deepEqual(ids, ['1', '2'], 'Can use collection methods on Array-likes.');
var ifnull = _.map(null, function(){});
ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');
@@ -93,13 +98,13 @@ $(document).ready(function() {
});
test('reduceRight', function() {
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
var list = _.reduceRight(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }, '');
equal(list, 'bazbarfoo', 'can perform right folds');
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
var list = _.foldr(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; }, '');
equal(list, 'bazbarfoo', 'aliased as "foldr"');
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
var list = _.foldr(['foo', 'bar', 'baz'], function(memo, str){ return memo + str; });
equal(list, 'bazbarfoo', 'default initial value');
var ifnull;
@@ -175,10 +180,10 @@ $(document).ready(function() {
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
equal(odds.join(', '), '1, 3, 5', 'rejected each even number');
var context = "obj";
var context = 'obj';
var evens = _.reject([1, 2, 3, 4, 5, 6], function(num){
equal(context, "obj");
equal(context, 'obj');
return num % 2 != 0;
}, context);
equal(evens.join(', '), '2, 4, 6', 'rejected each odd number');
@@ -239,13 +244,13 @@ $(document).ready(function() {
return 42;
};
var list = [[5, 1, 7], [3, 2, 1]];
var s = "foo";
equal(s.call(), 42, "call function exists");
var s = 'foo';
equal(s.call(), 42, 'call function exists');
var result = _.invoke(list, 'sort');
equal(result[0].join(', '), '1, 5, 7', 'first array sorted');
equal(result[1].join(', '), '1, 2, 3', 'second array sorted');
delete String.prototype.call;
equal(s.call, undefined, "call function removed");
equal(s.call, undefined, 'call function removed');
});
test('pluck', function() {
@@ -261,11 +266,8 @@ $(document).ready(function() {
result = _.where(list, {b: 2});
equal(result.length, 2);
equal(result[0].a, 1);
result = _.where(list, {a: 1}, true);
equal(result.b, 2, "Only get the first object matched.")
result = _.where(list, {a: 1}, false);
equal(result.length, 3);
result = _.where(list, {});
equal(result.length, list.length);
});
test('findWhere', function() {
@@ -276,10 +278,10 @@ $(document).ready(function() {
deepEqual(result, {a: 1, b: 4});
result = _.findWhere(list, {c:1})
ok(_.isUndefined(result), "undefined when not found");
ok(_.isUndefined(result), 'undefined when not found');
result = _.findWhere([], {c:1});
ok(_.isUndefined(result), "undefined when searching empty list");
ok(_.isUndefined(result), 'undefined when searching empty list');
});
test('max', function() {
@@ -292,7 +294,7 @@ $(document).ready(function() {
equal(-Infinity, _.max([]), 'Maximum value of an empty array');
equal(_.max({'a': 'a'}), -Infinity, 'Maximum value of a non-numeric collection');
equal(299999, _.max(_.range(1,300000)), "Maximum value of a too-big array");
equal(299999, _.max(_.range(1,300000)), 'Maximum value of a too-big array');
});
test('min', function() {
@@ -309,7 +311,7 @@ $(document).ready(function() {
var then = new Date(0);
equal(_.min([now, then]), then);
equal(1, _.min(_.range(1,300000)), "Minimum value of a too-big array");
equal(1, _.min(_.range(1,300000)), 'Minimum value of a too-big array');
});
test('sortBy', function() {
@@ -320,7 +322,7 @@ $(document).ready(function() {
var list = [undefined, 4, 1, undefined, 3, 2];
equal(_.sortBy(list, _.identity).join(','), '1,2,3,4,,', 'sortBy with undefined values');
var list = ["one", "two", "three", "four", "five"];
var list = ['one', 'two', 'three', 'four', 'five'];
var sorted = _.sortBy(list, 'length');
equal(sorted.join(' '), 'one two four five three', 'sorted by length');
@@ -346,6 +348,9 @@ $(document).ready(function() {
});
deepEqual(actual, collection, 'sortBy should be stable');
var list = ['q', 'w', 'e', 'r', 't', 'y'];
strictEqual(_.sortBy(list).join(''), 'eqrtwy', 'uses _.identity if iterator is not specified');
});
test('groupBy', function() {
@@ -353,7 +358,7 @@ $(document).ready(function() {
ok('0' in parity && '1' in parity, 'created a group for each value');
equal(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
var grouped = _.groupBy(list, 'length');
equal(grouped['3'].join(' '), 'one two six ten');
equal(grouped['4'].join(' '), 'four five nine');
@@ -390,7 +395,7 @@ $(document).ready(function() {
equal(parity['true'], 4);
equal(parity['false'], 5);
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
var grouped = _.indexBy(list, 'length');
equal(grouped['3'], 'ten');
equal(grouped['4'], 'nine');
@@ -408,7 +413,7 @@ $(document).ready(function() {
equal(parity['true'], 2);
equal(parity['false'], 3);
var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
var grouped = _.countBy(list, 'length');
equal(grouped['3'], 4);
equal(grouped['4'], 3);
@@ -468,6 +473,7 @@ $(document).ready(function() {
notStrictEqual(_.sample([], 5), [], 'sampling empty array with a number returns an empty array');
notStrictEqual(_.sample([1, 2, 3], 0), [], 'sampling an array with 0 picks returns an empty array');
deepEqual(_.sample([1, 2], -1), [], 'sampling a negative number of picks returns an empty array');
ok(_.contains([1, 2, 3], _.sample({a: 1, b: 2, c: 3})), 'sample one value from an object');
});
test('toArray', function() {
@@ -491,7 +497,7 @@ $(document).ready(function() {
test('size', function() {
equal(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');
equal(_.size([1, 2, 3]), 3, 'can compute the size of an array');
equal(_.size($('<div>').add('<span>').add('<span>')), 3, 'can compute the size of jQuery objects');
equal(_.size({length: 3, 0: 0, 1: 0, 2: 0}), 3, 'can compute the size of Array-likes');
var func = function() {
return _.size(arguments);
@@ -505,4 +511,4 @@ $(document).ready(function() {
equal(_.size(null), 0, 'handles nulls');
});
});
})();

View File

@@ -1,10 +1,10 @@
$(document).ready(function() {
(function() {
module("Functions");
module('Functions');
test("bind", function() {
test('bind', function() {
var context = {name : 'moe'};
var func = function(arg) { return "name: " + (this.name || arg); };
var func = function(arg) { return 'name: ' + (this.name || arg); };
var bound = _.bind(func, context);
equal(bound(), 'name: moe', 'can bind a function to a context');
@@ -33,22 +33,32 @@ $(document).ready(function() {
// These tests are only meaningful when using a browser without a native bind function
// To test this with a modern browser, set underscore's nativeBind to undefined
var F = function () { return this; };
var Boundf = _.bind(F, {hello: "moe curly"});
var Boundf = _.bind(F, {hello: 'moe curly'});
var newBoundf = new Boundf();
equal(newBoundf.hello, undefined, "function should not be bound to the context, to comply with ECMAScript 5");
equal(Boundf().hello, "moe curly", "When called without the new operator, it's OK to be bound to the context");
ok(newBoundf instanceof F, "a bound instance is an instance of the original function");
equal(newBoundf.hello, undefined, 'function should not be bound to the context, to comply with ECMAScript 5');
equal(Boundf().hello, 'moe curly', "When called without the new operator, it's OK to be bound to the context");
ok(newBoundf instanceof F, 'a bound instance is an instance of the original function');
});
test("partial", function() {
test('partial', function() {
var obj = {name: 'moe'};
var func = function() { return this.name + ' ' + _.toArray(arguments).join(' '); };
obj.func = _.partial(func, 'a', 'b');
equal(obj.func('c', 'd'), 'moe a b c d', 'can partially apply');
obj.func = _.partial(func, _, 'b', _, 'd');
equal(obj.func('a', 'c'), 'moe a b c d', 'can partially apply with placeholders');
func = _.partial(function() { return arguments.length; }, _, 'b', _, 'd');
equal(func('a', 'c', 'e'), 5, 'accepts more arguments than the number of placeholders');
equal(func('a'), 4, 'accepts fewer arguments than the number of placeholders');
func = _.partial(function() { return typeof arguments[2]; }, _, 'b', _, 'd');
equal(func('a'), 'undefined', 'unfilled placeholders are undefined');
});
test("bindAll", function() {
test('bindAll', function() {
var curly = {name : 'curly'}, moe = {
name : 'moe',
getName : function() { return 'name: ' + this.name; },
@@ -74,7 +84,7 @@ $(document).ready(function() {
equal(curly.sayHi(), 'hi: moe');
});
test("memoize", function() {
test('memoize', function() {
var fib = function(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
};
@@ -90,59 +100,59 @@ $(document).ready(function() {
equal(fastO('toString'), 'toString', 'checks hasOwnProperty');
});
asyncTest("delay", 2, function() {
asyncTest('delay', 2, function() {
var delayed = false;
_.delay(function(){ delayed = true; }, 100);
setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);
setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);
});
asyncTest("defer", 1, function() {
asyncTest('defer', 1, function() {
var deferred = false;
_.defer(function(bool){ deferred = bool; }, true);
_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
_.delay(function(){ ok(deferred, 'deferred the function'); start(); }, 50);
});
asyncTest("throttle", 2, function() {
asyncTest('throttle', 2, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 32);
throttledIncr(); throttledIncr();
equal(counter, 1, "incr was called immediately");
_.delay(function(){ equal(counter, 2, "incr was throttled"); start(); }, 64);
equal(counter, 1, 'incr was called immediately');
_.delay(function(){ equal(counter, 2, 'incr was throttled'); start(); }, 64);
});
asyncTest("throttle arguments", 2, function() {
asyncTest('throttle arguments', 2, function() {
var value = 0;
var update = function(val){ value = val; };
var throttledUpdate = _.throttle(update, 32);
throttledUpdate(1); throttledUpdate(2);
_.delay(function(){ throttledUpdate(3); }, 64);
equal(value, 1, "updated to latest value");
_.delay(function(){ equal(value, 3, "updated to latest value"); start(); }, 96);
equal(value, 1, 'updated to latest value');
_.delay(function(){ equal(value, 3, 'updated to latest value'); start(); }, 96);
});
asyncTest("throttle once", 2, function() {
asyncTest('throttle once', 2, function() {
var counter = 0;
var incr = function(){ return ++counter; };
var throttledIncr = _.throttle(incr, 32);
var result = throttledIncr();
_.delay(function(){
equal(result, 1, "throttled functions return their value");
equal(counter, 1, "incr was called once"); start();
equal(result, 1, 'throttled functions return their value');
equal(counter, 1, 'incr was called once'); start();
}, 64);
});
asyncTest("throttle twice", 1, function() {
asyncTest('throttle twice', 1, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 32);
throttledIncr(); throttledIncr();
_.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 64);
_.delay(function(){ equal(counter, 2, 'incr was called twice'); start(); }, 64);
});
asyncTest("more throttling", 3, function() {
asyncTest('more throttling', 3, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 30);
@@ -156,7 +166,7 @@ $(document).ready(function() {
}, 85);
});
asyncTest("throttle repeatedly with results", 6, function() {
asyncTest('throttle repeatedly with results', 6, function() {
var counter = 0;
var incr = function(){ return ++counter; };
var throttledIncr = _.throttle(incr, 100);
@@ -168,17 +178,17 @@ $(document).ready(function() {
_.delay(saveResult, 160);
_.delay(saveResult, 230);
_.delay(function() {
equal(results[0], 1, "incr was called once");
equal(results[1], 1, "incr was throttled");
equal(results[2], 1, "incr was throttled");
equal(results[3], 2, "incr was called twice");
equal(results[4], 2, "incr was throttled");
equal(results[5], 3, "incr was called trailing");
equal(results[0], 1, 'incr was called once');
equal(results[1], 1, 'incr was throttled');
equal(results[2], 1, 'incr was throttled');
equal(results[3], 2, 'incr was called twice');
equal(results[4], 2, 'incr was throttled');
equal(results[5], 3, 'incr was called trailing');
start();
}, 300);
});
asyncTest("throttle triggers trailing call when invoked repeatedly", 2, function() {
asyncTest('throttle triggers trailing call when invoked repeatedly', 2, function() {
var counter = 0;
var limit = 48;
var incr = function(){ counter++; };
@@ -197,7 +207,7 @@ $(document).ready(function() {
}, 96);
});
asyncTest("throttle does not trigger leading call when leading is set to false", 2, function() {
asyncTest('throttle does not trigger leading call when leading is set to false', 2, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 60, {leading: false});
@@ -211,7 +221,7 @@ $(document).ready(function() {
}, 96);
});
asyncTest("more throttle does not trigger leading call when leading is set to false", 3, function() {
asyncTest('more throttle does not trigger leading call when leading is set to false', 3, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 100, {leading: false});
@@ -232,7 +242,7 @@ $(document).ready(function() {
}, 350);
});
asyncTest("one more throttle with leading: false test", 2, function() {
asyncTest('one more throttle with leading: false test', 2, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 100, {leading: false});
@@ -247,7 +257,7 @@ $(document).ready(function() {
}, 200);
});
asyncTest("throttle does not trigger trailing call when trailing is set to false", 4, function() {
asyncTest('throttle does not trigger trailing call when trailing is set to false', 4, function() {
var counter = 0;
var incr = function(){ counter++; };
var throttledIncr = _.throttle(incr, 60, {trailing: false});
@@ -268,16 +278,16 @@ $(document).ready(function() {
}, 96);
});
asyncTest("debounce", 1, function() {
asyncTest('debounce', 1, function() {
var counter = 0;
var incr = function(){ counter++; };
var debouncedIncr = _.debounce(incr, 32);
debouncedIncr(); debouncedIncr();
_.delay(debouncedIncr, 16);
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);
_.delay(function(){ equal(counter, 1, 'incr was debounced'); start(); }, 96);
});
asyncTest("debounce asap", 4, function() {
asyncTest('debounce asap', 4, function() {
var a, b;
var counter = 0;
var incr = function(){ return ++counter; };
@@ -290,21 +300,21 @@ $(document).ready(function() {
_.delay(debouncedIncr, 16);
_.delay(debouncedIncr, 32);
_.delay(debouncedIncr, 48);
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 128);
_.delay(function(){ equal(counter, 1, 'incr was debounced'); start(); }, 128);
});
asyncTest("debounce asap recursively", 2, function() {
asyncTest('debounce asap recursively', 2, function() {
var counter = 0;
var debouncedIncr = _.debounce(function(){
counter++;
if (counter < 10) debouncedIncr();
}, 32, true);
debouncedIncr();
equal(counter, 1, "incr was called immediately");
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);
equal(counter, 1, 'incr was called immediately');
_.delay(function(){ equal(counter, 1, 'incr was debounced'); start(); }, 96);
});
test("once", function() {
test('once', function() {
var num = 0;
var increment = _.once(function(){ num++; });
increment();
@@ -312,7 +322,7 @@ $(document).ready(function() {
equal(num, 1);
});
test("Recursive onced function.", 1, function() {
test('Recursive onced function.', 1, function() {
var f = _.once(function(){
ok(true);
f();
@@ -320,15 +330,15 @@ $(document).ready(function() {
f();
});
test("wrap", function() {
var greet = function(name){ return "hi: " + name; };
test('wrap', function() {
var greet = function(name){ return 'hi: ' + name; };
var backwards = _.wrap(greet, function(func, name){ return func(name) + ' ' + name.split('').reverse().join(''); });
equal(backwards('moe'), 'hi: moe eom', 'wrapped the salutation function');
var inner = function(){ return "Hello "; };
var obj = {name : "Moe"};
var inner = function(){ return 'Hello '; };
var obj = {name : 'Moe'};
obj.hi = _.wrap(inner, function(fn){ return fn() + this.name; });
equal(obj.hi(), "Hello Moe");
equal(obj.hi(), 'Hello Moe');
var noop = function(){};
var wrapped = _.wrap(noop, function(fn){ return Array.prototype.slice.call(arguments, 0); });
@@ -336,8 +346,8 @@ $(document).ready(function() {
deepEqual(ret, [noop, ['whats', 'your'], 'vector', 'victor']);
});
test("compose", function() {
var greet = function(name){ return "hi: " + name; };
test('compose', function() {
var greet = function(name){ return 'hi: ' + name; };
var exclaim = function(sentence){ return sentence + '!'; };
var composed = _.compose(exclaim, greet);
equal(composed('moe'), 'hi: moe!', 'can compose a function that takes another');
@@ -346,7 +356,7 @@ $(document).ready(function() {
equal(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
});
test("after", function() {
test('after', function() {
var testAfter = function(afterAmount, timesCalled) {
var afterCalled = 0;
var after = _.after(afterAmount, function() {
@@ -356,10 +366,10 @@ $(document).ready(function() {
return afterCalled;
};
equal(testAfter(5, 5), 1, "after(N) should fire after being called N times");
equal(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
equal(testAfter(0, 0), 0, "after(0) should not fire immediately");
equal(testAfter(0, 1), 1, "after(0) should fire when first invoked");
equal(testAfter(5, 5), 1, 'after(N) should fire after being called N times');
equal(testAfter(5, 4), 0, 'after(N) should not fire unless called N times');
equal(testAfter(0, 0), 0, 'after(0) should not fire immediately');
equal(testAfter(0, 1), 1, 'after(0) should fire when first invoked');
});
});
})();

View File

@@ -1,30 +1,30 @@
$(document).ready(function() {
(function() {
module("Objects");
module('Objects');
test("keys", function() {
test('keys', function() {
equal(_.keys({one : 1, two : 2}).join(', '), 'one, two', 'can extract the keys from an object');
// the test above is not safe because it relies on for-in enumeration order
var a = []; a[1] = 0;
equal(_.keys(a).join(', '), '1', 'is not fooled by sparse arrays; see issue #95');
raises(function() { _.keys(null); }, TypeError, 'throws an error for `null` values');
raises(function() { _.keys(void 0); }, TypeError, 'throws an error for `undefined` values');
raises(function() { _.keys(1); }, TypeError, 'throws an error for number primitives');
raises(function() { _.keys('a'); }, TypeError, 'throws an error for string primitives');
raises(function() { _.keys(true); }, TypeError, 'throws an error for boolean primitives');
deepEqual(_.keys(null), []);
deepEqual(_.keys(void 0), []);
deepEqual(_.keys(1), []);
deepEqual(_.keys('a'), []);
deepEqual(_.keys(true), []);
});
test("values", function() {
test('values', function() {
equal(_.values({one: 1, two: 2}).join(', '), '1, 2', 'can extract the values from an object');
equal(_.values({one: 1, two: 2, length: 3}).join(', '), '1, 2, 3', '... even when one of them is "length"');
});
test("pairs", function() {
test('pairs', function() {
deepEqual(_.pairs({one: 1, two: 2}), [['one', 1], ['two', 2]], 'can convert an object into pairs');
deepEqual(_.pairs({one: 1, two: 2, length: 3}), [['one', 1], ['two', 2], ['length', 3]], '... even when one of them is "length"');
});
test("invert", function() {
test('invert', function() {
var obj = {first: 'Moe', second: 'Larry', third: 'Curly'};
equal(_.keys(_.invert(obj)).join(' '), 'Moe Larry Curly', 'can invert an object');
ok(_.isEqual(_.invert(_.invert(obj)), obj), 'two inverts gets you back where you started');
@@ -33,7 +33,7 @@ $(document).ready(function() {
ok(_.invert(obj)['3'] == 'length', 'can invert an object with "length"')
});
test("functions", function() {
test('functions', function() {
var obj = {a : 'dash', b : _.map, c : (/yo/), d : _.reduce};
ok(_.isEqual(['b', 'd'], _.functions(obj)), 'can grab the function names of any passed-in object');
@@ -42,7 +42,7 @@ $(document).ready(function() {
equal(_.functions(new Animal).join(''), 'run', 'also looks up functions on the prototype');
});
test("extend", function() {
test('extend', function() {
var result;
equal(_.extend({}, {a:'b'}).a, 'b', 'can extend an object with the attributes of another');
equal(_.extend({a:'x'}, {a:'b'}).a, 'b', 'properties in source override destination');
@@ -62,7 +62,7 @@ $(document).ready(function() {
equal(result.a, 1, 'should not error on `null` or `undefined` sources');
});
test("pick", function() {
test('pick', function() {
var result;
result = _.pick({a:1, b:2, c:3}, 'a', 'c');
ok(_.isEqual(result, {a:1, c:3}), 'can restrict properties to those named');
@@ -76,7 +76,7 @@ $(document).ready(function() {
ok(_.isEqual(_.pick(new Obj, 'a', 'c'), {a:1, c: 3}), 'include prototype props');
});
test("omit", function() {
test('omit', function() {
var result;
result = _.omit({a:1, b:2, c:3}, 'b');
ok(_.isEqual(result, {a:1, c:3}), 'can omit a single named property');
@@ -90,9 +90,9 @@ $(document).ready(function() {
ok(_.isEqual(_.omit(new Obj, 'b'), {a:1, c: 3}), 'include prototype props');
});
test("defaults", function() {
test('defaults', function() {
var result;
var options = {zero: 0, one: 1, empty: "", nan: NaN, nothing: null};
var options = {zero: 0, one: 1, empty: '', nan: NaN, nothing: null};
_.defaults(options, {zero: 1, one: 10, twenty: 20, nothing: 'str'});
equal(options.zero, 0, 'value exists');
@@ -100,10 +100,10 @@ $(document).ready(function() {
equal(options.twenty, 20, 'default applied');
equal(options.nothing, null, "null isn't overridden");
_.defaults(options, {empty: "full"}, {nan: "nan"}, {word: "word"}, {word: "dog"});
equal(options.empty, "", 'value exists');
_.defaults(options, {empty: 'full'}, {nan: 'nan'}, {word: 'word'}, {word: 'dog'});
equal(options.empty, '', 'value exists');
ok(_.isNaN(options.nan), "NaN isn't overridden");
equal(options.word, "word", 'new value is added, first one wins');
equal(options.word, 'word', 'new value is added, first one wins');
try {
options = {};
@@ -113,7 +113,7 @@ $(document).ready(function() {
equal(options.a, 1, 'should not error on `null` or `undefined` sources');
});
test("clone", function() {
test('clone', function() {
var moe = {name : 'moe', lucky : [13, 27, 34]};
var clone = _.clone(moe);
equal(clone.name, 'moe', 'the clone as the attributes of the original');
@@ -129,7 +129,7 @@ $(document).ready(function() {
equal(_.clone(null), null, 'non objects should not be changed by clone');
});
test("isEqual", function() {
test('isEqual', function() {
function First() {
this.value = 1;
}
@@ -140,133 +140,134 @@ $(document).ready(function() {
Second.prototype.value = 2;
// Basic equality and identity comparisons.
ok(_.isEqual(null, null), "`null` is equal to `null`");
ok(_.isEqual(), "`undefined` is equal to `undefined`");
ok(_.isEqual(null, null), '`null` is equal to `null`');
ok(_.isEqual(), '`undefined` is equal to `undefined`');
ok(!_.isEqual(0, -0), "`0` is not equal to `-0`");
ok(!_.isEqual(-0, 0), "Commutative equality is implemented for `0` and `-0`");
ok(!_.isEqual(null, undefined), "`null` is not equal to `undefined`");
ok(!_.isEqual(undefined, null), "Commutative equality is implemented for `null` and `undefined`");
ok(!_.isEqual(0, -0), '`0` is not equal to `-0`');
ok(!_.isEqual(-0, 0), 'Commutative equality is implemented for `0` and `-0`');
ok(!_.isEqual(null, undefined), '`null` is not equal to `undefined`');
ok(!_.isEqual(undefined, null), 'Commutative equality is implemented for `null` and `undefined`');
// String object and primitive comparisons.
ok(_.isEqual("Curly", "Curly"), "Identical string primitives are equal");
ok(_.isEqual(new String("Curly"), new String("Curly")), "String objects with identical primitive values are equal");
ok(_.isEqual(new String("Curly"), "Curly"), "String primitives and their corresponding object wrappers are equal");
ok(_.isEqual("Curly", new String("Curly")), "Commutative equality is implemented for string objects and primitives");
ok(_.isEqual('Curly', 'Curly'), 'Identical string primitives are equal');
ok(_.isEqual(new String('Curly'), new String('Curly')), 'String objects with identical primitive values are equal');
ok(_.isEqual(new String('Curly'), 'Curly'), 'String primitives and their corresponding object wrappers are equal');
ok(_.isEqual('Curly', new String('Curly')), 'Commutative equality is implemented for string objects and primitives');
ok(!_.isEqual("Curly", "Larry"), "String primitives with different values are not equal");
ok(!_.isEqual(new String("Curly"), new String("Larry")), "String objects with different primitive values are not equal");
ok(!_.isEqual(new String("Curly"), {toString: function(){ return "Curly"; }}), "String objects and objects with a custom `toString` method are not equal");
ok(!_.isEqual('Curly', 'Larry'), 'String primitives with different values are not equal');
ok(!_.isEqual(new String('Curly'), new String('Larry')), 'String objects with different primitive values are not equal');
ok(!_.isEqual(new String('Curly'), {toString: function(){ return 'Curly'; }}), 'String objects and objects with a custom `toString` method are not equal');
// Number object and primitive comparisons.
ok(_.isEqual(75, 75), "Identical number primitives are equal");
ok(_.isEqual(new Number(75), new Number(75)), "Number objects with identical primitive values are equal");
ok(_.isEqual(75, new Number(75)), "Number primitives and their corresponding object wrappers are equal");
ok(_.isEqual(new Number(75), 75), "Commutative equality is implemented for number objects and primitives");
ok(!_.isEqual(new Number(0), -0), "`new Number(0)` and `-0` are not equal");
ok(!_.isEqual(0, new Number(-0)), "Commutative equality is implemented for `new Number(0)` and `-0`");
ok(_.isEqual(75, 75), 'Identical number primitives are equal');
ok(_.isEqual(new Number(75), new Number(75)), 'Number objects with identical primitive values are equal');
ok(_.isEqual(75, new Number(75)), 'Number primitives and their corresponding object wrappers are equal');
ok(_.isEqual(new Number(75), 75), 'Commutative equality is implemented for number objects and primitives');
ok(!_.isEqual(new Number(0), -0), '`new Number(0)` and `-0` are not equal');
ok(!_.isEqual(0, new Number(-0)), 'Commutative equality is implemented for `new Number(0)` and `-0`');
ok(!_.isEqual(new Number(75), new Number(63)), "Number objects with different primitive values are not equal");
ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), "Number objects and objects with a `valueOf` method are not equal");
ok(!_.isEqual(new Number(75), new Number(63)), 'Number objects with different primitive values are not equal');
ok(!_.isEqual(new Number(63), {valueOf: function(){ return 63; }}), 'Number objects and objects with a `valueOf` method are not equal');
// Comparisons involving `NaN`.
ok(_.isEqual(NaN, NaN), "`NaN` is equal to `NaN`");
ok(!_.isEqual(61, NaN), "A number primitive is not equal to `NaN`");
ok(!_.isEqual(new Number(79), NaN), "A number object is not equal to `NaN`");
ok(!_.isEqual(Infinity, NaN), "`Infinity` is not equal to `NaN`");
ok(_.isEqual(NaN, NaN), '`NaN` is equal to `NaN`');
ok(!_.isEqual(61, NaN), 'A number primitive is not equal to `NaN`');
ok(!_.isEqual(new Number(79), NaN), 'A number object is not equal to `NaN`');
ok(!_.isEqual(Infinity, NaN), '`Infinity` is not equal to `NaN`');
// Boolean object and primitive comparisons.
ok(_.isEqual(true, true), "Identical boolean primitives are equal");
ok(_.isEqual(new Boolean, new Boolean), "Boolean objects with identical primitive values are equal");
ok(_.isEqual(true, new Boolean(true)), "Boolean primitives and their corresponding object wrappers are equal");
ok(_.isEqual(new Boolean(true), true), "Commutative equality is implemented for booleans");
ok(!_.isEqual(new Boolean(true), new Boolean), "Boolean objects with different primitive values are not equal");
ok(_.isEqual(true, true), 'Identical boolean primitives are equal');
ok(_.isEqual(new Boolean, new Boolean), 'Boolean objects with identical primitive values are equal');
ok(_.isEqual(true, new Boolean(true)), 'Boolean primitives and their corresponding object wrappers are equal');
ok(_.isEqual(new Boolean(true), true), 'Commutative equality is implemented for booleans');
ok(!_.isEqual(new Boolean(true), new Boolean), 'Boolean objects with different primitive values are not equal');
// Common type coercions.
ok(!_.isEqual(true, new Boolean(false)), "Boolean objects are not equal to the boolean primitive `true`");
ok(!_.isEqual("75", 75), "String and number primitives with like values are not equal");
ok(!_.isEqual(new Number(63), new String(63)), "String and number objects with like values are not equal");
ok(!_.isEqual(75, "75"), "Commutative equality is implemented for like string and number values");
ok(!_.isEqual(0, ""), "Number and string primitives with like values are not equal");
ok(!_.isEqual(1, true), "Number and boolean primitives with like values are not equal");
ok(!_.isEqual(new Boolean(false), new Number(0)), "Boolean and number objects with like values are not equal");
ok(!_.isEqual(false, new String("")), "Boolean primitives and string objects with like values are not equal");
ok(!_.isEqual(12564504e5, new Date(2009, 9, 25)), "Dates and their corresponding numeric primitive values are not equal");
ok(!_.isEqual(new Boolean(false), true), '`new Boolean(false)` is not equal to `true`');
ok(!_.isEqual('75', 75), 'String and number primitives with like values are not equal');
ok(!_.isEqual(new Number(63), new String(63)), 'String and number objects with like values are not equal');
ok(!_.isEqual(75, '75'), 'Commutative equality is implemented for like string and number values');
ok(!_.isEqual(0, ''), 'Number and string primitives with like values are not equal');
ok(!_.isEqual(1, true), 'Number and boolean primitives with like values are not equal');
ok(!_.isEqual(new Boolean(false), new Number(0)), 'Boolean and number objects with like values are not equal');
ok(!_.isEqual(false, new String('')), 'Boolean primitives and string objects with like values are not equal');
ok(!_.isEqual(12564504e5, new Date(2009, 9, 25)), 'Dates and their corresponding numeric primitive values are not equal');
// Dates.
ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), "Date objects referencing identical times are equal");
ok(!_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), "Date objects referencing different times are not equal");
ok(_.isEqual(new Date(2009, 9, 25), new Date(2009, 9, 25)), 'Date objects referencing identical times are equal');
ok(!_.isEqual(new Date(2009, 9, 25), new Date(2009, 11, 13)), 'Date objects referencing different times are not equal');
ok(!_.isEqual(new Date(2009, 11, 13), {
getTime: function(){
return 12606876e5;
}
}), "Date objects and objects with a `getTime` method are not equal");
ok(!_.isEqual(new Date("Curly"), new Date("Curly")), "Invalid dates are not equal");
}), 'Date objects and objects with a `getTime` method are not equal');
ok(!_.isEqual(new Date('Curly'), new Date('Curly')), 'Invalid dates are not equal');
// Functions.
ok(!_.isEqual(First, Second), "Different functions with identical bodies and source code representations are not equal");
ok(!_.isEqual(First, Second), 'Different functions with identical bodies and source code representations are not equal');
// RegExps.
ok(_.isEqual(/(?:)/gim, /(?:)/gim), "RegExps with equivalent patterns and flags are equal");
ok(!_.isEqual(/(?:)/g, /(?:)/gi), "RegExps with equivalent patterns and different flags are not equal");
ok(!_.isEqual(/Moe/gim, /Curly/gim), "RegExps with different patterns and equivalent flags are not equal");
ok(!_.isEqual(/(?:)/gi, /(?:)/g), "Commutative equality is implemented for RegExps");
ok(!_.isEqual(/Curly/g, {source: "Larry", global: true, ignoreCase: false, multiline: false}), "RegExps and RegExp-like objects are not equal");
ok(_.isEqual(/(?:)/gim, /(?:)/gim), 'RegExps with equivalent patterns and flags are equal');
ok(_.isEqual(/(?:)/gi, /(?:)/ig), 'Flag order is not significant');
ok(!_.isEqual(/(?:)/g, /(?:)/gi), 'RegExps with equivalent patterns and different flags are not equal');
ok(!_.isEqual(/Moe/gim, /Curly/gim), 'RegExps with different patterns and equivalent flags are not equal');
ok(!_.isEqual(/(?:)/gi, /(?:)/g), 'Commutative equality is implemented for RegExps');
ok(!_.isEqual(/Curly/g, {source: 'Larry', global: true, ignoreCase: false, multiline: false}), 'RegExps and RegExp-like objects are not equal');
// Empty arrays, array-like objects, and object literals.
ok(_.isEqual({}, {}), "Empty object literals are equal");
ok(_.isEqual([], []), "Empty array literals are equal");
ok(_.isEqual([{}], [{}]), "Empty nested arrays and objects are equal");
ok(!_.isEqual({length: 0}, []), "Array-like objects and arrays are not equal.");
ok(!_.isEqual([], {length: 0}), "Commutative equality is implemented for array-like objects");
ok(_.isEqual({}, {}), 'Empty object literals are equal');
ok(_.isEqual([], []), 'Empty array literals are equal');
ok(_.isEqual([{}], [{}]), 'Empty nested arrays and objects are equal');
ok(!_.isEqual({length: 0}, []), 'Array-like objects and arrays are not equal.');
ok(!_.isEqual([], {length: 0}), 'Commutative equality is implemented for array-like objects');
ok(!_.isEqual({}, []), "Object literals and array literals are not equal");
ok(!_.isEqual([], {}), "Commutative equality is implemented for objects and arrays");
ok(!_.isEqual({}, []), 'Object literals and array literals are not equal');
ok(!_.isEqual([], {}), 'Commutative equality is implemented for objects and arrays');
// Arrays with primitive and object values.
ok(_.isEqual([1, "Larry", true], [1, "Larry", true]), "Arrays containing identical primitives are equal");
ok(_.isEqual([(/Moe/g), new Date(2009, 9, 25)], [(/Moe/g), new Date(2009, 9, 25)]), "Arrays containing equivalent elements are equal");
ok(_.isEqual([1, 'Larry', true], [1, 'Larry', true]), 'Arrays containing identical primitives are equal');
ok(_.isEqual([(/Moe/g), new Date(2009, 9, 25)], [(/Moe/g), new Date(2009, 9, 25)]), 'Arrays containing equivalent elements are equal');
// Multi-dimensional arrays.
var a = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
var b = [new Number(47), false, "Larry", /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
ok(_.isEqual(a, b), "Arrays containing nested arrays and objects are recursively compared");
var a = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
var b = [new Number(47), false, 'Larry', /Moe/, new Date(2009, 11, 13), ['running', 'biking', new String('programming')], {a: 47}];
ok(_.isEqual(a, b), 'Arrays containing nested arrays and objects are recursively compared');
// Overwrite the methods defined in ES 5.1 section 15.4.4.
a.forEach = a.map = a.filter = a.every = a.indexOf = a.lastIndexOf = a.some = a.reduce = a.reduceRight = null;
b.join = b.pop = b.reverse = b.shift = b.slice = b.splice = b.concat = b.sort = b.unshift = null;
// Array elements and properties.
ok(_.isEqual(a, b), "Arrays containing equivalent elements and different non-numeric properties are equal");
a.push("White Rocks");
ok(!_.isEqual(a, b), "Arrays of different lengths are not equal");
a.push("East Boulder");
b.push("Gunbarrel Ranch", "Teller Farm");
ok(!_.isEqual(a, b), "Arrays of identical lengths containing different elements are not equal");
ok(_.isEqual(a, b), 'Arrays containing equivalent elements and different non-numeric properties are equal');
a.push('White Rocks');
ok(!_.isEqual(a, b), 'Arrays of different lengths are not equal');
a.push('East Boulder');
b.push('Gunbarrel Ranch', 'Teller Farm');
ok(!_.isEqual(a, b), 'Arrays of identical lengths containing different elements are not equal');
// Sparse arrays.
ok(_.isEqual(Array(3), Array(3)), "Sparse arrays of identical lengths are equal");
ok(!_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are not equal when both are empty");
ok(_.isEqual(Array(3), Array(3)), 'Sparse arrays of identical lengths are equal');
ok(!_.isEqual(Array(3), Array(6)), 'Sparse arrays of different lengths are not equal when both are empty');
// Simple objects.
ok(_.isEqual({a: "Curly", b: 1, c: true}, {a: "Curly", b: 1, c: true}), "Objects containing identical primitives are equal");
ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, b: new Date(2009, 11, 13)}), "Objects containing equivalent members are equal");
ok(!_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), "Objects of identical sizes with different values are not equal");
ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), "Objects of identical sizes with different property names are not equal");
ok(!_.isEqual({a: 1, b: 2}, {a: 1}), "Objects of different sizes are not equal");
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), "Commutative equality is implemented for objects");
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), "Objects with identical keys and different values are not equivalent");
ok(_.isEqual({a: 'Curly', b: 1, c: true}, {a: 'Curly', b: 1, c: true}), 'Objects containing identical primitives are equal');
ok(_.isEqual({a: /Curly/g, b: new Date(2009, 11, 13)}, {a: /Curly/g, b: new Date(2009, 11, 13)}), 'Objects containing equivalent members are equal');
ok(!_.isEqual({a: 63, b: 75}, {a: 61, b: 55}), 'Objects of identical sizes with different values are not equal');
ok(!_.isEqual({a: 63, b: 75}, {a: 61, c: 55}), 'Objects of identical sizes with different property names are not equal');
ok(!_.isEqual({a: 1, b: 2}, {a: 1}), 'Objects of different sizes are not equal');
ok(!_.isEqual({a: 1}, {a: 1, b: 2}), 'Commutative equality is implemented for objects');
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), 'Objects with identical keys and different values are not equivalent');
// `A` contains nested objects and arrays.
a = {
name: new String("Moe Howard"),
name: new String('Moe Howard'),
age: new Number(77),
stooge: true,
hobbies: ["acting"],
hobbies: ['acting'],
film: {
name: "Sing a Song of Six Pants",
name: 'Sing a Song of Six Pants',
release: new Date(1947, 9, 30),
stars: [new String("Larry Fine"), "Shemp Howard"],
stars: [new String('Larry Fine'), 'Shemp Howard'],
minutes: new Number(16),
seconds: 54
}
@@ -274,81 +275,81 @@ $(document).ready(function() {
// `B` contains equivalent nested objects and arrays.
b = {
name: new String("Moe Howard"),
name: new String('Moe Howard'),
age: new Number(77),
stooge: true,
hobbies: ["acting"],
hobbies: ['acting'],
film: {
name: "Sing a Song of Six Pants",
name: 'Sing a Song of Six Pants',
release: new Date(1947, 9, 30),
stars: [new String("Larry Fine"), "Shemp Howard"],
stars: [new String('Larry Fine'), 'Shemp Howard'],
minutes: new Number(16),
seconds: 54
}
};
ok(_.isEqual(a, b), "Objects with nested equivalent members are recursively compared");
ok(_.isEqual(a, b), 'Objects with nested equivalent members are recursively compared');
// Instances.
ok(_.isEqual(new First, new First), "Object instances are equal");
ok(!_.isEqual(new First, new Second), "Objects with different constructors and identical own properties are not equal");
ok(!_.isEqual({value: 1}, new First), "Object instances and objects sharing equivalent properties are not equal");
ok(!_.isEqual({value: 2}, new Second), "The prototype chain of objects should not be examined");
ok(_.isEqual(new First, new First), 'Object instances are equal');
ok(!_.isEqual(new First, new Second), 'Objects with different constructors and identical own properties are not equal');
ok(!_.isEqual({value: 1}, new First), 'Object instances and objects sharing equivalent properties are not equal');
ok(!_.isEqual({value: 2}, new Second), 'The prototype chain of objects should not be examined');
// Circular Arrays.
(a = []).push(a);
(b = []).push(b);
ok(_.isEqual(a, b), "Arrays containing circular references are equal");
a.push(new String("Larry"));
b.push(new String("Larry"));
ok(_.isEqual(a, b), "Arrays containing circular references and equivalent properties are equal");
a.push("Shemp");
b.push("Curly");
ok(!_.isEqual(a, b), "Arrays containing circular references and different properties are not equal");
ok(_.isEqual(a, b), 'Arrays containing circular references are equal');
a.push(new String('Larry'));
b.push(new String('Larry'));
ok(_.isEqual(a, b), 'Arrays containing circular references and equivalent properties are equal');
a.push('Shemp');
b.push('Curly');
ok(!_.isEqual(a, b), 'Arrays containing circular references and different properties are not equal');
// More circular arrays #767.
a = ["everything is checked but", "this", "is not"];
a = ['everything is checked but', 'this', 'is not'];
a[1] = a;
b = ["everything is checked but", ["this", "array"], "is not"];
ok(!_.isEqual(a, b), "Comparison of circular references with non-circular references are not equal");
b = ['everything is checked but', ['this', 'array'], 'is not'];
ok(!_.isEqual(a, b), 'Comparison of circular references with non-circular references are not equal');
// Circular Objects.
a = {abc: null};
b = {abc: null};
a.abc = a;
b.abc = b;
ok(_.isEqual(a, b), "Objects containing circular references are equal");
ok(_.isEqual(a, b), 'Objects containing circular references are equal');
a.def = 75;
b.def = 75;
ok(_.isEqual(a, b), "Objects containing circular references and equivalent properties are equal");
ok(_.isEqual(a, b), 'Objects containing circular references and equivalent properties are equal');
a.def = new Number(75);
b.def = new Number(63);
ok(!_.isEqual(a, b), "Objects containing circular references and different properties are not equal");
ok(!_.isEqual(a, b), 'Objects containing circular references and different properties are not equal');
// More circular objects #767.
a = {everything: "is checked", but: "this", is: "not"};
a = {everything: 'is checked', but: 'this', is: 'not'};
a.but = a;
b = {everything: "is checked", but: {that:"object"}, is: "not"};
ok(!_.isEqual(a, b), "Comparison of circular references with non-circular object references are not equal");
b = {everything: 'is checked', but: {that:'object'}, is: 'not'};
ok(!_.isEqual(a, b), 'Comparison of circular references with non-circular object references are not equal');
// Cyclic Structures.
a = [{abc: null}];
b = [{abc: null}];
(a[0].abc = a).push(a);
(b[0].abc = b).push(b);
ok(_.isEqual(a, b), "Cyclic structures are equal");
a[0].def = "Larry";
b[0].def = "Larry";
ok(_.isEqual(a, b), "Cyclic structures containing equivalent properties are equal");
a[0].def = new String("Larry");
b[0].def = new String("Curly");
ok(!_.isEqual(a, b), "Cyclic structures containing different properties are not equal");
ok(_.isEqual(a, b), 'Cyclic structures are equal');
a[0].def = 'Larry';
b[0].def = 'Larry';
ok(_.isEqual(a, b), 'Cyclic structures containing equivalent properties are equal');
a[0].def = new String('Larry');
b[0].def = new String('Curly');
ok(!_.isEqual(a, b), 'Cyclic structures containing different properties are not equal');
// Complex Circular References.
a = {foo: {b: {foo: {c: {foo: null}}}}};
b = {foo: {b: {foo: {c: {foo: null}}}}};
a.foo.b.foo.c.foo = a;
b.foo.b.foo.c.foo = b;
ok(_.isEqual(a, b), "Cyclic structures with nested and identically-named properties are equal");
ok(_.isEqual(a, b), 'Cyclic structures with nested and identically-named properties are equal');
// Chaining.
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'Chained objects containing different values are not equal');
@@ -359,9 +360,22 @@ $(document).ready(function() {
// Objects from another frame.
ok(_.isEqual({}, iObject));
// Objects without a `constructor` property
if (Object.create) {
a = Object.create(null, {x: {value: 1, enumerable: true}});
b = {x: 1};
ok(_.isEqual(a, b));
}
function Foo() { this.a = 1; }
Foo.prototype.constructor = null;
var other = { 'a': 1 };
strictEqual(_.isEqual(new Foo, other), false);
});
test("isEmpty", function() {
test('isEmpty', function() {
ok(!_([1]).isEmpty(), '[1] is not empty');
ok(_.isEmpty([]), '[] is empty');
ok(!_.isEmpty({one : 1}), '{one : 1} is not empty');
@@ -379,14 +393,15 @@ $(document).ready(function() {
// Setup remote variables for iFrame tests.
var iframe = document.createElement('iframe');
jQuery(iframe).appendTo(document.body);
var iDoc = iframe.contentDocument || iframe.contentWindow.document;
iframe.frameBorder = iframe.height = iframe.width = 0
document.body.appendChild(iframe);
var iDoc = (iDoc = iframe.contentDocument || iframe.contentWindow).document || iDoc;
iDoc.write(
"<script>\
parent.iElement = document.createElement('div');\
'<script>\
parent.iElement = document.createElement("div");\
parent.iArguments = (function(){ return arguments; })(1, 2, 3);\
parent.iArray = [1, 2, 3];\
parent.iString = new String('hello');\
parent.iString = new String("hello");\
parent.iNumber = new Number(100);\
parent.iFunction = (function(){});\
parent.iDate = new Date();\
@@ -396,17 +411,17 @@ $(document).ready(function() {
parent.iBoolean = new Boolean(false);\
parent.iUndefined = undefined;\
parent.iObject = {};\
</script>"
</script>'
);
iDoc.close();
test("isElement", function() {
test('isElement', function() {
ok(!_.isElement('div'), 'strings are not dom elements');
ok(_.isElement($('html')[0]), 'the html tag is a DOM element');
ok(_.isElement(document.body), 'the body tag is a DOM element');
ok(_.isElement(iElement), 'even from another frame');
});
test("isArguments", function() {
test('isArguments', function() {
var args = (function(){ return arguments; })(1, 2, 3);
ok(!_.isArguments('string'), 'a string is not an arguments object');
ok(!_.isArguments(_.isArguments), 'a function is not an arguments object');
@@ -416,10 +431,10 @@ $(document).ready(function() {
ok(_.isArguments(iArguments), 'even from another frame');
});
test("isObject", function() {
test('isObject', function() {
ok(_.isObject(arguments), 'the arguments object is object');
ok(_.isObject([1, 2, 3]), 'and arrays');
ok(_.isObject($('html')[0]), 'and DOM element');
ok(_.isObject(document.body), 'and DOM element');
ok(_.isObject(iElement), 'even from another frame');
ok(_.isObject(function () {}), 'and functions');
ok(_.isObject(iFunction), 'even from another frame');
@@ -431,23 +446,23 @@ $(document).ready(function() {
ok(_.isObject(new String('string')), 'but new String()');
});
test("isArray", function() {
test('isArray', function() {
ok(!_.isArray(undefined), 'undefined vars are not arrays');
ok(!_.isArray(arguments), 'the arguments object is not an array');
ok(_.isArray([1, 2, 3]), 'but arrays are');
ok(_.isArray(iArray), 'even from another frame');
});
test("isString", function() {
var obj = new String("I am a string object");
test('isString', function() {
var obj = new String('I am a string object');
ok(!_.isString(document.body), 'the document body is not a string');
ok(_.isString([1, 2, 3].join(', ')), 'but strings are');
ok(_.isString(iString), 'even from another frame');
ok(_.isString("I am a string literal"), 'string literals are');
ok(_.isString('I am a string literal'), 'string literals are');
ok(_.isString(obj), 'so are String objects');
});
test("isNumber", function() {
test('isNumber', function() {
ok(!_.isNumber('string'), 'a string is not a number');
ok(!_.isNumber(arguments), 'the arguments object is not a number');
ok(!_.isNumber(undefined), 'undefined is not a number');
@@ -458,11 +473,11 @@ $(document).ready(function() {
ok(!_.isNumber('1'), 'numeric strings are not numbers');
});
test("isBoolean", function() {
test('isBoolean', function() {
ok(!_.isBoolean(2), 'a number is not a boolean');
ok(!_.isBoolean("string"), 'a string is not a boolean');
ok(!_.isBoolean("false"), 'the string "false" is not a boolean');
ok(!_.isBoolean("true"), 'the string "true" is not a boolean');
ok(!_.isBoolean('string'), 'a string is not a boolean');
ok(!_.isBoolean('false'), 'the string "false" is not a boolean');
ok(!_.isBoolean('true'), 'the string "true" is not a boolean');
ok(!_.isBoolean(arguments), 'the arguments object is not a boolean');
ok(!_.isBoolean(undefined), 'undefined is not a boolean');
ok(!_.isBoolean(NaN), 'NaN is not a boolean');
@@ -472,7 +487,7 @@ $(document).ready(function() {
ok(_.isBoolean(iBoolean), 'even from another frame');
});
test("isFunction", function() {
test('isFunction', function() {
ok(!_.isFunction(undefined), 'undefined vars are not functions');
ok(!_.isFunction([1, 2, 3]), 'arrays are not functions');
ok(!_.isFunction('moe'), 'strings are not functions');
@@ -481,36 +496,36 @@ $(document).ready(function() {
ok(_.isFunction(function(){}), 'even anonymous ones');
});
test("isDate", function() {
test('isDate', function() {
ok(!_.isDate(100), 'numbers are not dates');
ok(!_.isDate({}), 'objects are not dates');
ok(_.isDate(new Date()), 'but dates are');
ok(_.isDate(iDate), 'even from another frame');
});
test("isRegExp", function() {
test('isRegExp', function() {
ok(!_.isRegExp(_.identity), 'functions are not RegExps');
ok(_.isRegExp(/identity/), 'but RegExps are');
ok(_.isRegExp(iRegExp), 'even from another frame');
});
test("isFinite", function() {
ok(!_.isFinite(undefined), 'undefined is not Finite');
ok(!_.isFinite(null), 'null is not Finite');
ok(!_.isFinite(NaN), 'NaN is not Finite');
ok(!_.isFinite(Infinity), 'Infinity is not Finite');
ok(!_.isFinite(-Infinity), '-Infinity is not Finite');
test('isFinite', function() {
ok(!_.isFinite(undefined), 'undefined is not finite');
ok(!_.isFinite(null), 'null is not finite');
ok(!_.isFinite(NaN), 'NaN is not finite');
ok(!_.isFinite(Infinity), 'Infinity is not finite');
ok(!_.isFinite(-Infinity), '-Infinity is not finite');
ok(_.isFinite('12'), 'Numeric strings are numbers');
ok(!_.isFinite('1a'), 'Non numeric strings are not numbers');
ok(!_.isFinite(''), 'Empty strings are not numbers');
var obj = new Number(5);
ok(_.isFinite(obj), 'Number instances can be finite');
ok(_.isFinite(0), '0 is Finite');
ok(_.isFinite(123), 'Ints are Finite');
ok(_.isFinite(-12.44), 'Floats are Finite');
ok(_.isFinite(0), '0 is finite');
ok(_.isFinite(123), 'Ints are finite');
ok(_.isFinite(-12.44), 'Floats are finite');
});
test("isNaN", function() {
test('isNaN', function() {
ok(!_.isNaN(undefined), 'undefined is not NaN');
ok(!_.isNaN(null), 'null is not NaN');
ok(!_.isNaN(0), '0 is not NaN');
@@ -519,14 +534,14 @@ $(document).ready(function() {
ok(_.isNaN(new Number(NaN)), 'wrapped NaN is still NaN');
});
test("isNull", function() {
test('isNull', function() {
ok(!_.isNull(undefined), 'undefined is not null');
ok(!_.isNull(NaN), 'NaN is not null');
ok(_.isNull(null), 'but null is');
ok(_.isNull(iNull), 'even from another frame');
});
test("isUndefined", function() {
test('isUndefined', function() {
ok(!_.isUndefined(1), 'numbers are defined');
ok(!_.isUndefined(null), 'null is defined');
ok(!_.isUndefined(false), 'false is defined');
@@ -537,8 +552,8 @@ $(document).ready(function() {
});
if (window.ActiveXObject) {
test("IE host objects", function() {
var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
test('IE host objects', function() {
var xml = new ActiveXObject('Msxml2.DOMDocument.3.0');
ok(!_.isNumber(xml));
ok(!_.isBoolean(xml));
ok(!_.isNaN(xml));
@@ -548,12 +563,12 @@ $(document).ready(function() {
});
}
test("tap", function() {
test('tap', function() {
var intercepted = null;
var interceptor = function(obj) { intercepted = obj; };
var returned = _.tap(1, interceptor);
equal(intercepted, 1, "passes tapped object to interceptor");
equal(returned, 1, "returns tapped object");
equal(intercepted, 1, 'passes tapped object to interceptor');
equal(returned, 1, 'returns tapped object');
returned = _([1,2,3]).chain().
map(function(n){ return n * 2; }).
@@ -564,14 +579,22 @@ $(document).ready(function() {
});
test("has", function () {
var obj = {foo: "bar", func: function () {} };
ok (_.has(obj, "foo"), "has() checks that the object has a property.");
ok (_.has(obj, "baz") == false, "has() returns false if the object doesn't have the property.");
ok (_.has(obj, "func"), "has() works for functions too.");
obj.hasOwnProperty = null;
ok (_.has(obj, "foo"), "has() works even when the hasOwnProperty method is deleted.");
var child = {};
child.prototype = obj;
ok (_.has(child, "foo") == false, "has() does not check the prototype chain for a property.")
var obj = {foo: "bar", func: function () {} };
ok(_.has(obj, "foo"), "has() checks that the object has a property.");
ok(_.has(obj, "baz") == false, "has() returns false if the object doesn't have the property.");
ok(_.has(obj, "func"), "has() works for functions too.");
obj.hasOwnProperty = null;
ok(_.has(obj, "foo"), "has() works even when the hasOwnProperty method is deleted.");
var child = {};
child.prototype = obj;
ok(_.has(child, "foo") == false, "has() does not check the prototype chain for a property.")
});
});
test("matches", function() {
var moe = {name: 'Moe Howard', hair: true},
curly = {name: 'Curly Howard', hair: false},
stooges = [moe, curly];
ok(_.find(stooges, _.matches({hair: false})) === curly, "returns a predicate that can be used by finding functions.")
ok(_.find(stooges, _.matches(moe)) === moe, "can be used to locate an object exists in a collection.")
})
})();

View File

@@ -1,8 +1,8 @@
$(document).ready(function() {
(function() {
var templateSettings;
module("Utility", {
module('Utility', {
setup: function() {
templateSettings = _.clone(_.templateSettings);
@@ -14,54 +14,69 @@ $(document).ready(function() {
});
test("#750 - Return _ instance.", 2, function() {
test('#750 - Return _ instance.', 2, function() {
var instance = _([]);
ok(_(instance) === instance);
ok(new _(instance) === instance);
});
test("identity", function() {
test('identity', function() {
var moe = {name : 'moe'};
equal(_.identity(moe), moe, 'moe is the same as his identity');
});
test("random", function() {
test('constant', function() {
var moe = {name : 'moe'};
equal(_.constant(moe)(), moe, 'should create a function that returns moe');
});
test('property', function() {
var moe = {name : 'moe'};
equal(_.property('name')(moe), 'moe', 'should return the property with the given name');
});
test('random', function() {
var array = _.range(1000);
var min = Math.pow(2, 31);
var max = Math.pow(2, 62);
ok(_.every(array, function() {
return _.random(min, max) >= min;
}), "should produce a random number greater than or equal to the minimum number");
}), 'should produce a random number greater than or equal to the minimum number');
ok(_.some(array, function() {
return _.random(Number.MAX_VALUE) > 0;
}), "should produce a random number when passed `Number.MAX_VALUE`");
}), 'should produce a random number when passed `Number.MAX_VALUE`');
});
test("uniqueId", function() {
test('now', function() {
var diff = _.now() - new Date().getTime();
ok(diff <= 0 && diff > -5, 'Produces the correct time in milliseconds');//within 5ms
});
test('uniqueId', function() {
var ids = [], i = 0;
while(i++ < 100) ids.push(_.uniqueId());
equal(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
});
test("times", function() {
test('times', function() {
var vals = [];
_.times(3, function (i) { vals.push(i); });
ok(_.isEqual(vals, [0,1,2]), "is 0 indexed");
ok(_.isEqual(vals, [0,1,2]), 'is 0 indexed');
//
vals = [];
_(3).times(function(i) { vals.push(i); });
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
ok(_.isEqual(vals, [0,1,2]), 'works as a wrapper');
// collects return values
ok(_.isEqual([0, 1, 2], _.times(3, function(i) { return i; })), "collects return values");
ok(_.isEqual([0, 1, 2], _.times(3, function(i) { return i; })), 'collects return values');
deepEqual(_.times(0, _.identity), []);
deepEqual(_.times(-1, _.identity), []);
deepEqual(_.times(parseFloat('-Infinity'), _.identity), []);
});
test("mixin", function() {
test('mixin', function() {
_.mixin({
myReverse: function(string) {
return string.split('').reverse().join('');
@@ -71,60 +86,60 @@ $(document).ready(function() {
equal(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');
});
test("_.escape", function() {
equal(_.escape("Curly & Moe"), "Curly &amp; Moe");
test('_.escape', function() {
equal(_.escape('Curly & Moe'), 'Curly &amp; Moe');
equal(_.escape('<a href="http://moe.com">Curly & Moe\'s</a>'), '&lt;a href=&quot;http://moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;/a&gt;');
equal(_.escape("Curly &amp; Moe"), "Curly &amp;amp; Moe");
equal(_.escape('Curly &amp; Moe'), 'Curly &amp;amp; Moe');
equal(_.escape(null), '');
});
test("_.unescape", function() {
var string = "Curly & Moe";
equal(_.unescape("Curly &amp; Moe"), string);
test('_.unescape', function() {
var string = 'Curly & Moe';
equal(_.unescape('Curly &amp; Moe'), string);
equal(_.unescape('&lt;a href=&quot;http://moe.com&quot;&gt;Curly &amp; Moe&#x27;s&lt;/a&gt;'), '<a href="http://moe.com">Curly & Moe\'s</a>');
equal(_.unescape("Curly &amp;amp; Moe"), "Curly &amp; Moe");
equal(_.unescape('Curly &amp;amp; Moe'), 'Curly &amp; Moe');
equal(_.unescape(null), '');
equal(_.unescape(_.escape(string)), string);
});
test("template", function() {
test('template', function() {
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
var result = basicTemplate({thing : 'This'});
equal(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
var sansSemicolonTemplate = _.template("A <% this %> B");
equal(sansSemicolonTemplate(), "A B");
var sansSemicolonTemplate = _.template('A <% this %> B');
equal(sansSemicolonTemplate(), 'A B');
var backslashTemplate = _.template("<%= thing %> is \\ridanculous");
equal(backslashTemplate({thing: 'This'}), "This is \\ridanculous");
var backslashTemplate = _.template('<%= thing %> is \\ridanculous');
equal(backslashTemplate({thing: 'This'}), 'This is \\ridanculous');
var escapeTemplate = _.template('<%= a ? "checked=\\"checked\\"" : "" %>');
equal(escapeTemplate({a: true}), 'checked="checked"', 'can handle slash escapes in interpolations.');
var fancyTemplate = _.template("<ul><% \
var fancyTemplate = _.template('<ul><% \
for (var key in people) { \
%><li><%= people[key] %></li><% } %></ul>");
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
%><li><%= people[key] %></li><% } %></ul>');
result = fancyTemplate({people : {moe : 'Moe', larry : 'Larry', curly : 'Curly'}});
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
var escapedCharsInJavascriptTemplate = _.template("<ul><% _.each(numbers.split('\\n'), function(item) { %><li><%= item %></li><% }) %></ul>");
result = escapedCharsInJavascriptTemplate({numbers: "one\ntwo\nthree\nfour"});
equal(result, "<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>", 'Can use escaped characters (e.g. \\n) in Javascript');
var escapedCharsInJavascriptTemplate = _.template('<ul><% _.each(numbers.split("\\n"), function(item) { %><li><%= item %></li><% }) %></ul>');
result = escapedCharsInJavascriptTemplate({numbers: 'one\ntwo\nthree\nfour'});
equal(result, '<ul><li>one</li><li>two</li><li>three</li><li>four</li></ul>', 'Can use escaped characters (e.g. \\n) in JavaScript');
var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class=\"thumbnail\" rel=\"<%= p %>\"></div><% }); %>");
var namespaceCollisionTemplate = _.template('<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class="thumbnail" rel="<%= p %>"></div><% }); %>');
result = namespaceCollisionTemplate({
pageCount: 3,
thumbnails: {
1: "p1-thumbnail.gif",
2: "p2-thumbnail.gif",
3: "p3-thumbnail.gif"
1: 'p1-thumbnail.gif',
2: 'p2-thumbnail.gif',
3: 'p3-thumbnail.gif'
}
});
equal(result, "3 p3-thumbnail.gif <div class=\"thumbnail\" rel=\"p1-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p2-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p3-thumbnail.gif\"></div>");
equal(result, '3 p3-thumbnail.gif <div class="thumbnail" rel="p1-thumbnail.gif"></div><div class="thumbnail" rel="p2-thumbnail.gif"></div><div class="thumbnail" rel="p3-thumbnail.gif"></div>');
var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
var noInterpolateTemplate = _.template('<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
result = noInterpolateTemplate();
equal(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
equal(result, '<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>');
var quoteTemplate = _.template("It's its, not it's");
equal(quoteTemplate({}), "It's its, not it's");
@@ -132,65 +147,68 @@ $(document).ready(function() {
var quoteInStatementAndBody = _.template("<%\
if(foo == 'bar'){ \
%>Statement quotes and 'quotes'.<% } %>");
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
equal(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
var template = _.template("<i><%- value %></i>");
var result = template({value: "<script>"});
var template = _.template('<i><%- value %></i>');
var result = template({value: '<script>'});
equal(result, '<i>&lt;script&gt;</i>');
var stooge = {
name: "Moe",
name: 'Moe',
template: _.template("I'm <%= this.name %>")
};
equal(stooge.template(), "I'm Moe");
if (!$.browser.msie) {
var fromHTML = _.template($('#template').html());
equal(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
}
template = _.template('\n \
<%\n \
// a comment\n \
if (data) { data += 12345; }; %>\n \
<li><%= data %></li>\n \
');
equal(template({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
_.templateSettings = {
evaluate : /\{\{([\s\S]+?)\}\}/g,
interpolate : /\{\{=([\s\S]+?)\}\}/g
};
var custom = _.template("<ul>{{ for (var key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
var custom = _.template('<ul>{{ for (var key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>');
result = custom({people : {moe : 'Moe', larry : 'Larry', curly : 'Curly'}});
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
var customQuote = _.template("It's its, not it's");
equal(customQuote({}), "It's its, not it's");
var quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
_.templateSettings = {
evaluate : /<\?([\s\S]+?)\?>/g,
interpolate : /<\?=([\s\S]+?)\?>/g
};
var customWithSpecialChars = _.template("<ul><? for (var key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
equal(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
var customWithSpecialChars = _.template('<ul><? for (var key in people) { ?><li><?= people[key] ?></li><? } ?></ul>');
result = customWithSpecialChars({people : {moe : 'Moe', larry : 'Larry', curly : 'Curly'}});
equal(result, '<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>', 'can run arbitrary javascript in templates');
var customWithSpecialCharsQuote = _.template("It's its, not it's");
equal(customWithSpecialCharsQuote({}), "It's its, not it's");
var quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
equal(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
equal(quoteInStatementAndBody({foo: 'bar'}), "Statement quotes and 'quotes'.");
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
var mustache = _.template("Hello {{planet}}!");
equal(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");
var mustache = _.template('Hello {{planet}}!');
equal(mustache({planet : 'World'}), 'Hello World!', 'can mimic mustache.js');
var templateWithNull = _.template("a null undefined {{planet}}");
equal(templateWithNull({planet : "world"}), "a null undefined world", "can handle missing escape and evaluate settings");
var templateWithNull = _.template('a null undefined {{planet}}');
equal(templateWithNull({planet : 'world'}), 'a null undefined world', 'can handle missing escape and evaluate settings');
});
test('_.template provides the generated function source, when a SyntaxError occurs', function() {
@@ -269,4 +287,4 @@ $(document).ready(function() {
strictEqual(template(), '<<\nx\n>>');
});
});
})();

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
// Underscore.js 1.5.2
// Underscore.js 1.6.0
// http://underscorejs.org
// (c) 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Underscore may be freely distributed under the MIT license.
(function() {
@@ -65,7 +65,7 @@
}
// Current version.
_.VERSION = '1.5.2';
_.VERSION = '1.6.0';
// Collection Functions
// --------------------
@@ -74,7 +74,7 @@
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.each = _.forEach = function(obj, iterator, context) {
if (obj == null) return;
if (obj == null) return obj;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
@@ -87,6 +87,7 @@
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
return obj;
};
// Return the results of applying the iterator to each element.
@@ -152,10 +153,10 @@
};
// Return the first value which passes a truth test. Aliased as `detect`.
_.find = _.detect = function(obj, iterator, context) {
_.find = _.detect = function(obj, predicate, context) {
var result;
any(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) {
if (predicate.call(context, value, index, list)) {
result = value;
return true;
}
@@ -166,33 +167,33 @@
// Return all the elements that pass a truth test.
// Delegates to **ECMAScript 5**'s native `filter` if available.
// Aliased as `select`.
_.filter = _.select = function(obj, iterator, context) {
_.filter = _.select = function(obj, predicate, context) {
var results = [];
if (obj == null) return results;
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(predicate, context);
each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) results.push(value);
if (predicate.call(context, value, index, list)) results.push(value);
});
return results;
};
// Return all the elements for which a truth test fails.
_.reject = function(obj, iterator, context) {
_.reject = function(obj, predicate, context) {
return _.filter(obj, function(value, index, list) {
return !iterator.call(context, value, index, list);
return !predicate.call(context, value, index, list);
}, context);
};
// Determine whether all of the elements match a truth test.
// Delegates to **ECMAScript 5**'s native `every` if available.
// Aliased as `all`.
_.every = _.all = function(obj, iterator, context) {
iterator || (iterator = _.identity);
_.every = _.all = function(obj, predicate, context) {
predicate || (predicate = _.identity);
var result = true;
if (obj == null) return result;
if (nativeEvery && obj.every === nativeEvery) return obj.every(iterator, context);
if (nativeEvery && obj.every === nativeEvery) return obj.every(predicate, context);
each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) return breaker;
if (!(result = result && predicate.call(context, value, index, list))) return breaker;
});
return !!result;
};
@@ -200,13 +201,13 @@
// Determine if at least one element in the object matches a truth test.
// Delegates to **ECMAScript 5**'s native `some` if available.
// Aliased as `any`.
var any = _.some = _.any = function(obj, iterator, context) {
iterator || (iterator = _.identity);
var any = _.some = _.any = function(obj, predicate, context) {
predicate || (predicate = _.identity);
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
if (nativeSome && obj.some === nativeSome) return obj.some(predicate, context);
each(obj, function(value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
if (result || (result = predicate.call(context, value, index, list))) return breaker;
});
return !!result;
};
@@ -232,25 +233,19 @@
// Convenience version of a common use case of `map`: fetching a property.
_.pluck = function(obj, key) {
return _.map(obj, function(value){ return value[key]; });
return _.map(obj, _.property(key));
};
// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key]) return false;
}
return true;
});
_.where = function(obj, attrs) {
return _.filter(obj, _.matches(attrs));
};
// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_.findWhere = function(obj, attrs) {
return _.where(obj, attrs, true);
return _.find(obj, _.matches(attrs));
};
// Return the maximum element or (element-based computation).
@@ -260,13 +255,15 @@
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.max.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity, value: -Infinity};
var result = -Infinity, lastComputed = -Infinity;
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed > result.computed && (result = {value : value, computed : computed});
if (computed > lastComputed) {
result = value;
lastComputed = computed;
}
});
return result.value;
return result;
};
// Return the minimum element (or element-based computation).
@@ -274,16 +271,18 @@
if (!iterator && _.isArray(obj) && obj[0] === +obj[0] && obj.length < 65535) {
return Math.min.apply(Math, obj);
}
if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity, value: Infinity};
var result = Infinity, lastComputed = Infinity;
each(obj, function(value, index, list) {
var computed = iterator ? iterator.call(context, value, index, list) : value;
computed < result.computed && (result = {value : value, computed : computed});
if (computed < lastComputed) {
result = value;
lastComputed = computed;
}
});
return result.value;
return result;
};
// Shuffle an array, using the modern version of the
// Shuffle an array, using the modern version of the
// [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/FisherYates_shuffle).
_.shuffle = function(obj) {
var rand;
@@ -297,11 +296,12 @@
return shuffled;
};
// Sample **n** random values from an array.
// If **n** is not specified, returns a single random element from the array.
// Sample **n** random values from a collection.
// If **n** is not specified, returns a single random element.
// The internal `guard` argument allows it to work with `map`.
_.sample = function(obj, n, guard) {
if (arguments.length < 2 || guard) {
if (n == null || guard) {
if (obj.length !== +obj.length) obj = _.values(obj);
return obj[_.random(obj.length - 1)];
}
return _.shuffle(obj).slice(0, Math.max(0, n));
@@ -309,12 +309,14 @@
// An internal function to generate lookup iterators.
var lookupIterator = function(value) {
return _.isFunction(value) ? value : function(obj){ return obj[value]; };
if (value == null) return _.identity;
if (_.isFunction(value)) return value;
return _.property(value);
};
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, value, context) {
var iterator = lookupIterator(value);
_.sortBy = function(obj, iterator, context) {
iterator = lookupIterator(iterator);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
@@ -334,9 +336,9 @@
// An internal function used for aggregate "group by" operations.
var group = function(behavior) {
return function(obj, value, context) {
return function(obj, iterator, context) {
var result = {};
var iterator = value == null ? _.identity : lookupIterator(value);
iterator = lookupIterator(iterator);
each(obj, function(value, index) {
var key = iterator.call(context, value, index, obj);
behavior(result, key, value);
@@ -348,7 +350,7 @@
// Groups the object's values by a criterion. Pass either a string attribute
// to group by, or a function that returns the criterion.
_.groupBy = group(function(result, key, value) {
(_.has(result, key) ? result[key] : (result[key] = [])).push(value);
_.has(result, key) ? result[key].push(value) : result[key] = [value];
});
// Indexes the object's values by a criterion, similar to `groupBy`, but for
@@ -367,7 +369,7 @@
// Use a comparator function to figure out the smallest index at which
// an object should be inserted so as to maintain order. Uses binary search.
_.sortedIndex = function(array, obj, iterator, context) {
iterator = iterator == null ? _.identity : lookupIterator(iterator);
iterator = lookupIterator(iterator);
var value = iterator.call(context, obj);
var low = 0, high = array.length;
while (low < high) {
@@ -399,7 +401,9 @@
// allows it to work with `_.map`.
_.first = _.head = _.take = function(array, n, guard) {
if (array == null) return void 0;
return (n == null) || guard ? array[0] : slice.call(array, 0, n);
if ((n == null) || guard) return array[0];
if (n < 0) return [];
return slice.call(array, 0, n);
};
// Returns everything but the last entry of the array. Especially useful on
@@ -414,11 +418,8 @@
// values in the array. The **guard** check allows it to work with `_.map`.
_.last = function(array, n, guard) {
if (array == null) return void 0;
if ((n == null) || guard) {
return array[array.length - 1];
} else {
return slice.call(array, Math.max(array.length - n, 0));
}
if ((n == null) || guard) return array[array.length - 1];
return slice.call(array, Math.max(array.length - n, 0));
};
// Returns everything but the first entry of the array. Aliased as `tail` and `drop`.
@@ -459,6 +460,16 @@
return _.difference(array, slice.call(arguments, 1));
};
// Split an array into two arrays: one whose elements all satisfy the given
// predicate, and one whose elements all do not satisfy the predicate.
_.partition = function(array, predicate) {
var pass = [], fail = [];
each(array, function(elem) {
(predicate(elem) ? pass : fail).push(elem);
});
return [pass, fail];
};
// Produce a duplicate-free version of the array. If the array has already
// been sorted, you have the option of using a faster algorithm.
// Aliased as `unique`.
@@ -492,7 +503,7 @@
var rest = slice.call(arguments, 1);
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
return _.indexOf(other, item) >= 0;
return _.contains(other, item);
});
});
};
@@ -507,7 +518,7 @@
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
var length = _.max(_.pluck(arguments, "length").concat(0));
var length = _.max(_.pluck(arguments, 'length').concat(0));
var results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = _.pluck(arguments, '' + i);
@@ -613,19 +624,27 @@
};
// Partially apply a function by creating a version that has had some of its
// arguments pre-filled, without changing its dynamic `this` context.
// arguments pre-filled, without changing its dynamic `this` context. _ acts
// as a placeholder, allowing any combination of arguments to be pre-filled.
_.partial = function(func) {
var args = slice.call(arguments, 1);
var boundArgs = slice.call(arguments, 1);
return function() {
return func.apply(this, args.concat(slice.call(arguments)));
var position = 0;
var args = boundArgs.slice();
for (var i = 0, length = args.length; i < length; i++) {
if (args[i] === _) args[i] = arguments[position++];
}
while (position < arguments.length) args.push(arguments[position++]);
return func.apply(this, args);
};
};
// Bind all of an object's methods to that object. Useful for ensuring that
// all callbacks defined on an object belong to it.
// Bind a number of an object's methods to that object. Remaining arguments
// are the method names to be bound. Useful for ensuring that all callbacks
// defined on an object belong to it.
_.bindAll = function(obj) {
var funcs = slice.call(arguments, 1);
if (funcs.length === 0) throw new Error("bindAll must be passed function names");
if (funcs.length === 0) throw new Error('bindAll must be passed function names');
each(funcs, function(f) { obj[f] = _.bind(obj[f], obj); });
return obj;
};
@@ -664,12 +683,13 @@
var previous = 0;
options || (options = {});
var later = function() {
previous = options.leading === false ? 0 : new Date;
previous = options.leading === false ? 0 : _.now();
timeout = null;
result = func.apply(context, args);
context = args = null;
};
return function() {
var now = new Date;
var now = _.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
@@ -679,6 +699,7 @@
timeout = null;
previous = now;
result = func.apply(context, args);
context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
@@ -692,24 +713,33 @@
// leading edge, instead of the trailing.
_.debounce = function(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function() {
var last = _.now() - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
};
return function() {
context = this;
args = arguments;
timestamp = new Date();
var later = function() {
var last = (new Date()) - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
}
};
timestamp = _.now();
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) result = func.apply(context, args);
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
};
@@ -731,11 +761,7 @@
// allowing you to adjust arguments, run code before and after, and
// conditionally execute the original function.
_.wrap = function(func, wrapper) {
return function() {
var args = [func];
push.apply(args, arguments);
return wrapper.apply(this, args);
};
return _.partial(wrapper, func);
};
// Returns a function that is the composition of a list of functions, each
@@ -765,8 +791,9 @@
// Retrieve the names of an object's properties.
// Delegates to **ECMAScript 5**'s native `Object.keys`
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
_.keys = function(obj) {
if (!_.isObject(obj)) return [];
if (nativeKeys) return nativeKeys(obj);
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
@@ -921,7 +948,8 @@
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
_.isFunction(bCtor) && (bCtor instanceof bCtor))
&& ('constructor' in a && 'constructor' in b)) {
return false;
}
// Add the first object to the stack of traversed objects.
@@ -1061,6 +1089,30 @@
return value;
};
_.constant = function(value) {
return function () {
return value;
};
};
_.property = function(key) {
return function(obj) {
return obj[key];
};
};
// Returns a predicate for checking whether an object has a given set of `key:value` pairs.
_.matches = function(attrs) {
return function(obj) {
if (obj === attrs) return true; //avoid comparing an object to itself.
for (var key in attrs) {
if (attrs[key] !== obj[key])
return false;
}
return true;
}
};
// Run a function **n** times.
_.times = function(n, iterator, context) {
var accum = Array(Math.max(0, n));
@@ -1077,6 +1129,9 @@
return min + Math.floor(Math.random() * (max - min + 1));
};
// A (possibly faster) way to get the current timestamp as an integer.
_.now = Date.now || function() { return new Date().getTime(); };
// List of HTML entities for escaping.
var entityMap = {
escape: {
@@ -1273,4 +1328,16 @@
});
// AMD registration happens at the end for compatibility with AMD loaders
// that may not enforce next-turn semantics on modules. Even though general
// practice for AMD registration is to be anonymous, underscore registers
// as a named module because, like jQuery, it is a base library that is
// popular enough to be bundled in a third party lib, but not be part of
// an AMD load request. Those cases could generate an error when an
// anonymous define() is called outside of a loader request.
if (typeof define === 'function' && define.amd) {
define('underscore', [], function() {
return _;
});
}
}).call(this);