Ticket #5961: 5961.patch
| File 5961.patch, 6.9 kB (added by peller, 10 months ago) |
|---|
-
Users/peller/Sites/workspace/trunk4/dojo/tests/_base/array.js
6 6 var foo = [128, 256, 512]; 7 7 var bar = ["aaa", "bbb", "ccc"]; 8 8 9 t.assertTrue(dojo.indexOf([45, 56, 85], 56) == 1); 10 t.assertTrue(dojo.indexOf([Number, String, Date], String) == 1); 11 t.assertTrue(dojo.indexOf(foo, foo[1]) == 1); 12 t.assertTrue(dojo.indexOf(foo, foo[2]) == 2); 13 t.assertTrue(dojo.indexOf(bar, bar[1]) == 1); 14 t.assertTrue(dojo.indexOf(bar, bar[2]) == 2); 15 9 t.assertEqual(1, dojo.indexOf([45, 56, 85], 56)); 10 t.assertEqual(1, dojo.indexOf([Number, String, Date], String)); 11 t.assertEqual(1, dojo.indexOf(foo, foo[1])); 12 t.assertEqual(2, dojo.indexOf(foo, foo[2])); 13 t.assertEqual(1, dojo.indexOf(bar, bar[1])); 14 t.assertEqual(2, dojo.indexOf(bar, bar[2])); 15 t.assertEqual(-1, dojo.indexOf({a:1}, "a")); 16 16 17 foo.push(bar); 17 t.assert True(dojo.indexOf(foo, bar) == 3);18 t.assertEqual(3, dojo.indexOf(foo, bar)); 18 19 }, 19 20 20 21 function testIndexOfFromIndex(t){ 21 22 var foo = [128, 256, 512]; 22 23 var bar = ["aaa", "bbb", "ccc"]; 23 24 24 // FIXME: what happens w/ negative indexes?25 25 t.assertEqual(-1, dojo.indexOf([45, 56, 85], 56, 2)); 26 26 t.assertEqual(1, dojo.indexOf([45, 56, 85], 56, 1)); 27 t.assertEqual(1, dojo.indexOf([45, 56, 85], 56, -1)); 28 // Make sure going out of bounds doesn't throw us in an infinite loop 29 t.assertEqual(-1, dojo.indexOf([45, 56, 85], 56, 3)); 27 30 }, 28 31 29 32 function testLastIndexOf(t){ 30 33 var foo = [128, 256, 512]; 31 34 var bar = ["aaa", "bbb", "aaa", "ccc"]; 32 35 33 t.assert True(dojo.indexOf([45, 56, 85], 56) == 1);34 t.assert True(dojo.indexOf([Number, String, Date], String) == 1);35 t.assert True(dojo.lastIndexOf(foo, foo[1]) == 1);36 t.assert True(dojo.lastIndexOf(foo, foo[2]) == 2);37 t.assert True(dojo.lastIndexOf(bar, bar[1]) == 1);38 t.assert True(dojo.lastIndexOf(bar, bar[2]) == 2);39 t.assert True(dojo.lastIndexOf(bar, bar[0]) == 2);36 t.assertEqual(1, dojo.indexOf([45, 56, 85], 56)); 37 t.assertEqual(1, dojo.indexOf([Number, String, Date], String)); 38 t.assertEqual(1, dojo.lastIndexOf(foo, foo[1])); 39 t.assertEqual(2, dojo.lastIndexOf(foo, foo[2])); 40 t.assertEqual(1, dojo.lastIndexOf(bar, bar[1])); 41 t.assertEqual(2, dojo.lastIndexOf(bar, bar[2])); 42 t.assertEqual(2, dojo.lastIndexOf(bar, bar[0])); 40 43 }, 41 44 42 45 function testLastIndexOfFromIndex(t){ 43 // FIXME: what happens w/ negative indexes?44 46 t.assertEqual(1, dojo.lastIndexOf([45, 56, 85], 56, 1)); 45 47 t.assertEqual(-1, dojo.lastIndexOf([45, 56, 85], 85, 1)); 48 t.assertEqual(-1, dojo.lastIndexOf([45, 56, 85], 85, -1)); 49 t.assertEqual(0, dojo.lastIndexOf([45, 56, 45], 45, 0)); 46 50 }, 47 51 48 52 function testForEach(t){ -
Users/peller/Sites/workspace/trunk4/dojo/_base/array.js
23 23 // For details on this method, see: 24 24 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf> 25 25 26 var step = 1, end = array.length , i = (fromIndex||0);26 var step = 1, end = array.length || 0, i = 0; 27 27 if(findLast){ 28 step = -1, i = (fromIndex||array.length - 1), end = -1; 28 i = end - 1; 29 step = end = -1; 29 30 } 30 for(; i!=end; i+=step){ 31 if(array[i] == value){ return i; } 31 if(fromIndex !== undefined){ i = fromIndex; } 32 if((findLast && i > end) || i < end){ 33 for(; i != end; i += step){ 34 if(array[i] == value){ return i; } 35 } 32 36 } 33 37 return -1; // Number 34 38 }, … … 39 43 // If the value is not found, -1 is returned. 40 44 // description: 41 45 // For details on this method, see: 42 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf46 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf> 43 47 return dojo.indexOf(array, value, fromIndex, true); // Number 44 48 }, 45 49 … … 53 57 // This function corresponds to the JavaScript 1.6 Array.forEach() method. 54 58 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 55 59 // For more details, see: 56 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach60 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach> 57 61 58 62 // match the behavior of the built-in forEach WRT empty arrs 59 63 if(!arr || !arr.length){ return; } … … 89 93 // This function corresponds to the JavaScript 1.6 Array.every() method. 90 94 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 91 95 // For more details, see: 92 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every96 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every> 93 97 // example: 94 98 // | dojo.every([1, 2, 3, 4], function(item){ return item>1; }); 95 99 // returns false … … 111 115 // This function corresponds to the JavaScript 1.6 Array.some() method. 112 116 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 113 117 // For more details, see: 114 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some118 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some> 115 119 // example: 116 120 // | dojo.some([1, 2, 3, 4], function(item){ return item>1; }); 117 121 // returns true … … 132 136 // This function corresponds to the JavaScript 1.6 Array.map() method. 133 137 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 134 138 // For more details, see: 135 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map139 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map> 136 140 // example: 137 141 // | dojo.map([1, 2, 3, 4], function(item){ return item+1 }); 138 142 // returns [2, 3, 4, 5] … … 156 160 // This function corresponds to the JavaScript 1.6 Array.filter() method. 157 161 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 158 162 // For more details, see: 159 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter163 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter> 160 164 // example: 161 165 // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; }); 162 166 // returns [2, 3, 4]