PageRenderTime 40ms CodeModel.GetById 22ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/node_modules/underscore.string/test/test_underscore/arrays.js

https://bitbucket.org/jorritposthuma/inventory-counter-mobile
JavaScript | 166 lines | 139 code | 27 blank | 0 comment | 4 complexity | 86cad9f0ce59bfeca09765383d2bebcb MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1$(document).ready(function() {
  2
  3  module("Arrays");
  4
  5  test("arrays: first", function() {
  6    equals(_.first([1,2,3]), 1, 'can pull out the first element of an array');
  7    equals(_([1, 2, 3]).first(), 1, 'can perform OO-style "first()"');
  8    equals(_.first([1,2,3], 0).join(', '), "", 'can pass an index to first');
  9    equals(_.first([1,2,3], 2).join(', '), '1, 2', 'can pass an index to first');
 10    equals(_.first([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to first');
 11    var result = (function(){ return _.first(arguments); })(4, 3, 2, 1);
 12    equals(result, 4, 'works on an arguments object.');
 13    result = _.map([[1,2,3],[1,2,3]], _.first);
 14    equals(result.join(','), '1,1', 'works well with _.map');
 15  });
 16
 17  test("arrays: rest", function() {
 18    var numbers = [1, 2, 3, 4];
 19    equals(_.rest(numbers).join(", "), "2, 3, 4", 'working rest()');
 20    equals(_.rest(numbers, 0).join(", "), "1, 2, 3, 4", 'working rest(0)');
 21    equals(_.rest(numbers, 2).join(', '), '3, 4', 'rest can take an index');
 22    var result = (function(){ return _(arguments).tail(); })(1, 2, 3, 4);
 23    equals(result.join(', '), '2, 3, 4', 'aliased as tail and works on arguments object');
 24    result = _.map([[1,2,3],[1,2,3]], _.rest);
 25    equals(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
 26  });
 27
 28  test("arrays: initial", function() {
 29    equals(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
 30    equals(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
 31    var result = (function(){ return _(arguments).initial(); })(1, 2, 3, 4);
 32    equals(result.join(", "), "1, 2, 3", 'initial works on arguments object');
 33    result = _.map([[1,2,3],[1,2,3]], _.initial);
 34    equals(_.flatten(result).join(','), '1,2,1,2', 'initial works with _.map');
 35  });
 36
 37  test("arrays: last", function() {
 38    equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');
 39    equals(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
 40    equals(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
 41    equals(_.last([1,2,3], 5).join(', '), '1, 2, 3', 'can pass an index to last');
 42    var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
 43    equals(result, 4, 'works on an arguments object');
 44    result = _.map([[1,2,3],[1,2,3]], _.last);
 45    equals(result.join(','), '3,3', 'works well with _.map');
 46  });
 47
 48  test("arrays: compact", function() {
 49    equals(_.compact([0, 1, false, 2, false, 3]).length, 3, 'can trim out all falsy values');
 50    var result = (function(){ return _(arguments).compact().length; })(0, 1, false, 2, false, 3);
 51    equals(result, 3, 'works on an arguments object');
 52  });
 53
 54  test("arrays: flatten", function() {
 55    if (window.JSON) {
 56      var list = [1, [2], [3, [[[4]]]]];
 57      equals(JSON.stringify(_.flatten(list)), '[1,2,3,4]', 'can flatten nested arrays');
 58      equals(JSON.stringify(_.flatten(list, true)), '[1,2,3,[[[4]]]]', 'can shallowly flatten nested arrays');
 59      var result = (function(){ return _.flatten(arguments); })(1, [2], [3, [[[4]]]]);
 60      equals(JSON.stringify(result), '[1,2,3,4]', 'works on an arguments object');
 61    }
 62  });
 63
 64  test("arrays: without", function() {
 65    var list = [1, 2, 1, 0, 3, 1, 4];
 66    equals(_.without(list, 0, 1).join(', '), '2, 3, 4', 'can remove all instances of an object');
 67    var result = (function(){ return _.without(arguments, 0, 1); })(1, 2, 1, 0, 3, 1, 4);
 68    equals(result.join(', '), '2, 3, 4', 'works on an arguments object');
 69
 70    var list = [{one : 1}, {two : 2}];
 71    ok(_.without(list, {one : 1}).length == 2, 'uses real object identity for comparisons.');
 72    ok(_.without(list, list[0]).length == 1, 'ditto.');
 73  });
 74
 75  test("arrays: uniq", function() {
 76    var list = [1, 2, 1, 3, 1, 4];
 77    equals(_.uniq(list).join(', '), '1, 2, 3, 4', 'can find the unique values of an unsorted array');
 78
 79    var list = [1, 1, 1, 2, 2, 3];
 80    equals(_.uniq(list, true).join(', '), '1, 2, 3', 'can find the unique values of a sorted array faster');
 81
 82    var list = [{name:'moe'}, {name:'curly'}, {name:'larry'}, {name:'curly'}];
 83    var iterator = function(value) { return value.name; };
 84    equals(_.map(_.uniq(list, false, iterator), iterator).join(', '), 'moe, curly, larry', 'can find the unique values of an array using a custom iterator');
 85
 86    var iterator = function(value) { return value +1; };
 87    var list = [1, 2, 2, 3, 4, 4];
 88    equals(_.uniq(list, true, iterator).join(', '), '1, 2, 3, 4', 'iterator works with sorted array');
 89
 90    var result = (function(){ return _.uniq(arguments); })(1, 2, 1, 3, 1, 4);
 91    equals(result.join(', '), '1, 2, 3, 4', 'works on an arguments object');
 92  });
 93
 94  test("arrays: intersection", function() {
 95    var stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'];
 96    equals(_.intersection(stooges, leaders).join(''), 'moe', 'can take the set intersection of two arrays');
 97    equals(_(stooges).intersection(leaders).join(''), 'moe', 'can perform an OO-style intersection');
 98    var result = (function(){ return _.intersection(arguments, leaders); })('moe', 'curly', 'larry');
 99    equals(result.join(''), 'moe', 'works on an arguments object');
100  });
101
102  test("arrays: union", function() {
103    var result = _.union([1, 2, 3], [2, 30, 1], [1, 40]);
104    equals(result.join(' '), '1 2 3 30 40', 'takes the union of a list of arrays');
105
106    var result = _.union([1, 2, 3], [2, 30, 1], [1, 40, [1]]);
107    equals(result.join(' '), '1 2 3 30 40 1', 'takes the union of a list of nested arrays');
108  });
109
110  test("arrays: difference", function() {
111    var result = _.difference([1, 2, 3], [2, 30, 40]);
112    equals(result.join(' '), '1 3', 'takes the difference of two arrays');
113
114    var result = _.difference([1, 2, 3, 4], [2, 30, 40], [1, 11, 111]);
115    equals(result.join(' '), '3 4', 'takes the difference of three arrays');
116  });
117
118  test('arrays: zip', function() {
119    var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
120    var stooges = _.zip(names, ages, leaders);
121    equals(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
122  });
123
124  test("arrays: indexOf", function() {
125    var numbers = [1, 2, 3];
126    numbers.indexOf = null;
127    equals(_.indexOf(numbers, 2), 1, 'can compute indexOf, even without the native function');
128    var result = (function(){ return _.indexOf(arguments, 2); })(1, 2, 3);
129    equals(result, 1, 'works on an arguments object');
130    equals(_.indexOf(null, 2), -1, 'handles nulls properly');
131
132    var numbers = [10, 20, 30, 40, 50], num = 35;
133    var index = _.indexOf(numbers, num, true);
134    equals(index, -1, '35 is not in the list');
135
136    numbers = [10, 20, 30, 40, 50]; num = 40;
137    index = _.indexOf(numbers, num, true);
138    equals(index, 3, '40 is in the list');
139
140    numbers = [1, 40, 40, 40, 40, 40, 40, 40, 50, 60, 70]; num = 40;
141    index = _.indexOf(numbers, num, true);
142    equals(index, 1, '40 is in the list');
143  });
144
145  test("arrays: lastIndexOf", function() {
146    var numbers = [1, 0, 1, 0, 0, 1, 0, 0, 0];
147    numbers.lastIndexOf = null;
148    equals(_.lastIndexOf(numbers, 1), 5, 'can compute lastIndexOf, even without the native function');
149    equals(_.lastIndexOf(numbers, 0), 8, 'lastIndexOf the other element');
150    var result = (function(){ return _.lastIndexOf(arguments, 1); })(1, 0, 1, 0, 0, 1, 0, 0, 0);
151    equals(result, 5, 'works on an arguments object');
152    equals(_.indexOf(null, 2), -1, 'handles nulls properly');
153  });
154
155  test("arrays: range", function() {
156    equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
157    equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
158    equals(_.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');
159    equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
160    equals(_.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');
161    equals(_.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');
162    equals(_.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');
163    equals(_.range(0, -10, -1).join(' '), '0 -1 -2 -3 -4 -5 -6 -7 -8 -9', 'final example in the Python docs');
164  });
165
166});