react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / test_underscore / arrays.js
80684 views$(document).ready(function() {12module("Arrays");34test("arrays: first", function() {5equals(_.first([1,2,3]), 1, 'can pull out the first element of an array');6equals(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');7equals(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');8equals(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');9var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);10equals(result, 4, 'works on an arguments object.');11result = _.map([[1,2,3],[1,2,3]], _.first);12equals(result.join(','), '1,1', 'works well with _.map');13});1415test("arrays: rest", function() {16var numbers = [1, 2, 3, 4];17equals(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');18equals(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');19equals(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');20var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);21equals(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');22result = _.map([[1,2,3],[1,2,3]], _.rest);23equals(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');24});2526test("arrays: initial", function() {27equals(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');28equals(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');29var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);30equals(result.join(", "), "1, 2, 3", 'initial works on arguments object');31result = _.map([[1,2,3],[1,2,3]], _.initial);32equals(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');33});3435test("arrays: last", function() {36equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');37equals(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');38equals(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');39var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);40equals(result, 4, 'works on an arguments object');41result = _.map([[1,2,3],[1,2,3]], _.last);42equals(result.join(','), '3,3', 'works well with _.map');43});4445test("arrays: compact", function() {46equals(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');47var result = (function(){ return _(arguments).compact().length; })(0, 1, false, 2, false, 3);48equals(result, 3, 'works on an arguments object');49});5051test("arrays: flatten", function() {52if (window.JSON) {53var list = [1, [2], [3, [[[4]]]]];54equals(JSON.stringify(_.flatten(list)), '[1,2,3,4]', 'can flatten nested arrays');55equals(JSON.stringify(_.flatten(list, true)), '[1,2,3,[[[4]]]]', 'can shallowly flatten nested arrays');56var result = (function(){ return _.flatten(arguments); })(1, [2], [3, [[[4]]]]);57equals(JSON.stringify(result), '[1,2,3,4]', 'works on an arguments object');58}59});6061test("arrays: without", function() {62var list = [1, 2, 1, 0, 3, 1, 4];63equals(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');64var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);65equals(result.join(', '), '2, 3, 4', 'works on an arguments object');6667var list = [{one : 1}, {two : 2}];68ok(_.without(list, {one : 1}).length == 2, 'uses real object identity for comparisons.');69ok(_.without(list, list[0]).length == 1, 'ditto.');70});7172test("arrays: uniq", function() {73var list = [1, 2, 1, 3, 1, 4];74equals(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');7576var list = [1, 1, 1, 2, 2, 3];77equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');7879var list = [{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}];80var iterator = function(value) { return value.name; };81equals(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');8283var iterator = function(value) { return value +1; };84var list = [1, 2, 2, 3, 4, 4];85equals(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');8687var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);88equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');89});9091test("arrays: intersection", function() {92var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];93equals(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');94equals(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');95var result = (function(){ return _.intersection(arguments, leaders); })('moe', 'curly', 'larry');96equals(result.join(''), 'moe', 'works on an arguments object');97});9899test("arrays: union", function() {100var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);101equals(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');102103var result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);104equals(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');105});106107test("arrays: difference", function() {108var result = _.difference([1, 2, 3], [2, 30, 40]);109equals(result.join(' '), '1 3', 'takes the difference of two arrays');110});111112test('arrays: zip', function() {113var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];114var stooges = _.zip(names, ages, leaders);115equals(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');116});117118test("arrays: indexOf", function() {119var numbers = [1, 2, 3];120numbers.indexOf = null;121equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');122var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);123equals(result, 1, 'works on an arguments object');124equals(_.indexOf(null, 2), -1, 'handles nulls properly');125126var numbers = [10, 20, 30, 40, 50], num = 35;127var index = _.indexOf(numbers, num, true);128equals(index, -1, '35 is not in the list');129130numbers = [10, 20, 30, 40, 50]; num = 40;131index = _.indexOf(numbers, num, true);132equals(index, 3, '40 is in the list');133134numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;135index = _.indexOf(numbers, num, true);136equals(index, 1, '40 is in the list');137});138139test("arrays: lastIndexOf", function() {140var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];141numbers.lastIndexOf = null;142equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');143equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');144var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);145equals(result, 5, 'works on an arguments object');146equals(_.indexOf(null, 2), -1, 'handles nulls properly');147});148149test("arrays: range", function() {150equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');151equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');152equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');153equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');154equals(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');155equals(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');156equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');157equals(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');158});159160});161162163