react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / test_underscore / collections.js
80684 views$(document).ready(function() {12module("Collections");34test("collections: each", function() {5_.each([1, 2, 3], function(num, i) {6equals(num, i + 1, 'each iterators provide value and iteration count');7});89var answers = [];10_.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});11equals(answers.join(', '), '5, 10, 15', 'context object property accessed');1213answers = [];14_.forEach([1, 2, 3], function(num){ answers.push(num); });15equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');1617answers = [];18var obj = {one : 1, two : 2, three : 3};19obj.constructor.prototype.four = 4;20_.each(obj, function(value, key){ answers.push(key); });21equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');22delete obj.constructor.prototype.four;2324answer = null;25_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });26ok(answer, 'can reference the original collection from inside the iterator');2728answers = 0;29_.each(null, function(){ ++answers; });30equals(answers, 0, 'handles a null properly');31});3233test('collections: map', function() {34var doubled = _.map([1, 2, 3], function(num){ return num * 2; });35equals(doubled.join(', '), '2, 4, 6', 'doubled numbers');3637var tripled = _.map([1, 2, 3], function(num){ return num * this.multiplier; }, {multiplier : 3});38equals(tripled.join(', '), '3, 6, 9', 'tripled numbers with context');3940var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });41equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');4243var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });44ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');4546var ids = _.map(document.images, function(n){ return n.id; });47ok(ids[0] == 'chart_image', 'can use collection methods on HTMLCollections');4849var ifnull = _.map(null, function(){});50ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly');51});5253test('collections: reduce', function() {54var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; }, 0);55equals(sum, 6, 'can sum up an array');5657var context = {multiplier : 3};58sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num * this.multiplier; }, 0, context);59equals(sum, 18, 'can reduce with a context object');6061sum = _.inject([1, 2, 3], function(sum, num){ return sum + num; }, 0);62equals(sum, 6, 'aliased as "inject"');6364sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);65equals(sum, 6, 'OO-style reduce');6667var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });68equals(sum, 6, 'default initial value');6970var ifnull;71try {72_.reduce(null, function(){});73} catch (ex) {74ifnull = ex;75}76ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');7778ok(_.reduce(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');7980// Sparse arrays:81var sparseArray = [];82sparseArray[100] = 10;83sparseArray[200] = 20;8485equals(_.reduce(sparseArray, function(a, b){ return a + b }), 30, 'initially-sparse arrays with no memo');86});8788test('collections: reduceRight', function() {89var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');90equals(list, 'bazbarfoo', 'can perform right folds');9192var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');93equals(list, 'bazbarfoo', 'aliased as "foldr"');9495var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });96equals(list, 'bazbarfoo', 'default initial value');9798var ifnull;99try {100_.reduceRight(null, function(){});101} catch (ex) {102ifnull = ex;103}104ok(ifnull instanceof TypeError, 'handles a null (without inital value) properly');105106ok(_.reduceRight(null, function(){}, 138) === 138, 'handles a null (with initial value) properly');107});108109test('collections: detect', function() {110var result = _.detect([1, 2, 3], function(num){ return num * 2 == 4; });111equals(result, 2, 'found the first "2" and broke the loop');112});113114test('collections: select', function() {115var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });116equals(evens.join(', '), '2, 4, 6', 'selected each even number');117118evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });119equals(evens.join(', '), '2, 4, 6', 'aliased as "filter"');120});121122test('collections: reject', function() {123var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });124equals(odds.join(', '), '1, 3, 5', 'rejected each even number');125});126127test('collections: all', function() {128ok(_.all([], _.identity), 'the empty set');129ok(_.all([true, true, true], _.identity), 'all true values');130ok(!_.all([true, false, true], _.identity), 'one false value');131ok(_.all([0, 10, 28], function(num){ return num % 2 == 0; }), 'even numbers');132ok(!_.all([0, 11, 28], function(num){ return num % 2 == 0; }), 'an odd number');133ok(_.every([true, true, true], _.identity), 'aliased as "every"');134});135136test('collections: any', function() {137var nativeSome = Array.prototype.some;138Array.prototype.some = null;139ok(!_.any([]), 'the empty set');140ok(!_.any([false, false, false]), 'all false values');141ok(_.any([false, false, true]), 'one true value');142ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers');143ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number');144ok(_.some([false, false, true]), 'aliased as "some"');145Array.prototype.some = nativeSome;146});147148test('collections: include', function() {149ok(_.include([1,2,3], 2), 'two is in the array');150ok(!_.include([1,3,9], 2), 'two is not in the array');151ok(_.contains({moe:1, larry:3, curly:9}, 3) === true, '_.include on objects checks their values');152ok(_([1,2,3]).include(2), 'OO-style include');153});154155test('collections: invoke', function() {156var list = [[5, 1, 7], [3, 2, 1]];157var result = _.invoke(list, 'sort');158equals(result[0].join(', '), '1, 5, 7', 'first array sorted');159equals(result[1].join(', '), '1, 2, 3', 'second array sorted');160});161162test('collections: invoke w/ function reference', function() {163var list = [[5, 1, 7], [3, 2, 1]];164var result = _.invoke(list, Array.prototype.sort);165equals(result[0].join(', '), '1, 5, 7', 'first array sorted');166equals(result[1].join(', '), '1, 2, 3', 'second array sorted');167});168169test('collections: pluck', function() {170var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}];171equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'pulls names out of objects');172});173174test('collections: max', function() {175equals(3, _.max([1, 2, 3]), 'can perform a regular Math.max');176177var neg = _.max([1, 2, 3], function(num){ return -num; });178equals(neg, 1, 'can perform a computation-based max');179180equals(-Infinity, _.max({}), 'Maximum value of an empty object');181equals(-Infinity, _.max([]), 'Maximum value of an empty array');182});183184test('collections: min', function() {185equals(1, _.min([1, 2, 3]), 'can perform a regular Math.min');186187var neg = _.min([1, 2, 3], function(num){ return -num; });188equals(neg, 3, 'can perform a computation-based min');189190equals(Infinity, _.min({}), 'Minimum value of an empty object');191equals(Infinity, _.min([]), 'Minimum value of an empty array');192});193194test('collections: sortBy', function() {195var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];196people = _.sortBy(people, function(person){ return person.age; });197equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');198});199200test('collections: groupBy', function() {201var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });202ok('0' in parity && '1' in parity, 'created a group for each value');203equals(parity[0].join(', '), '2, 4, 6', 'put each even number in the right group');204205var list = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];206var grouped = _.groupBy(list, 'length');207equals(grouped['3'].join(' '), 'one two six ten');208equals(grouped['4'].join(' '), 'four five nine');209equals(grouped['5'].join(' '), 'three seven eight');210});211212test('collections: sortedIndex', function() {213var numbers = [10, 20, 30, 40, 50], num = 35;214var index = _.sortedIndex(numbers, num);215equals(index, 3, '35 should be inserted at index 3');216});217218test('collections: shuffle', function() {219var numbers = _.range(10);220var shuffled = _.shuffle(numbers).sort();221notStrictEqual(numbers, shuffled, 'original object is unmodified');222equals(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');223});224225test('collections: toArray', function() {226ok(!_.isArray(arguments), 'arguments object is not an array');227ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');228var a = [1,2,3];229ok(_.toArray(a) !== a, 'array is cloned');230equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');231232var numbers = _.toArray({one : 1, two : 2, three : 3});233equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');234});235236test('collections: size', function() {237equals(_.size({one : 1, two : 2, three : 3}), 3, 'can compute the size of an object');238});239240});241242243