Changeset 13155
- Timestamp:
- 03/20/08 05:23:00 (10 months ago)
- Location:
- dojo/trunk
- Files:
-
- 2 modified
-
tests/_base/array.js (modified) (3 diffs)
-
_base/array.js (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dojo/trunk/tests/_base/array.js
r12668 r13155 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 … … 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 … … 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 -
dojo/trunk/_base/array.js
r12827 r13155 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 … … 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 }, … … 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 … … 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; }); … … 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; }); … … 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 }); … … 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; });