Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 0b52dfb

Browse files
committed
Merge pull request jashkenas#233 from chaoflow/master
make toArray return a clone in case of an array
2 parents 96dbdaf + 9ab3ac4 commit 0b52dfb

3 files changed

Lines changed: 23 additions & 19 deletions

File tree

test/collections.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $(document).ready(function() {
4141
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
4242
equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
4343

44-
var ids = _.map(document.body.childNodes, function(n){ return n.id; });
44+
var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });
4545
ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
4646

4747
var ids = _.map(document.images, function(n){ return n.id; });
@@ -197,6 +197,9 @@ $(document).ready(function() {
197197
test('collections: toArray', function() {
198198
ok(!_.isArray(arguments), 'arguments object is not an array');
199199
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
200+
var a = [1,2,3];
201+
ok(_.toArray(a) !== a, 'array is cloned');
202+
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
200203

201204
var numbers = _.toArray({one : 1, two : 2, three : 3});
202205
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');

test/test.html

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@
1616
<script type="text/javascript" src="speed.js"></script>
1717
</head>
1818
<body>
19-
<h1 id="qunit-header">Underscore Test Suite</h1>
20-
<h2 id="qunit-banner"></h2>
21-
<h2 id="qunit-userAgent"></h2>
22-
<ol id="qunit-tests"></ol>
23-
<br />
24-
<h1 class="qunit-header">Underscore Speed Suite</h1>
25-
<p>
26-
A representative sample of the functions are benchmarked here, to provide
27-
a sense of how fast they might run in different browsers.
28-
Each iteration runs on an array of 1000 elements.<br /><br />
29-
For example, the 'intersect' test measures the number of times you can
30-
find the intersection of two thousand-element arrays in one second.
31-
</p>
32-
<br />
19+
<div class="underscore-test">
20+
<h1 id="qunit-header">Underscore Test Suite</h1>
21+
<h2 id="qunit-banner"></h2>
22+
<h2 id="qunit-userAgent"></h2>
23+
<ol id="qunit-tests"></ol>
24+
<br />
25+
<h1 class="qunit-header">Underscore Speed Suite</h1>
26+
<p>
27+
A representative sample of the functions are benchmarked here, to provide
28+
a sense of how fast they might run in different browsers.
29+
Each iteration runs on an array of 1000 elements.<br /><br />
30+
For example, the 'intersect' test measures the number of times you can
31+
find the intersection of two thousand-element arrays in one second.
32+
</p>
33+
<br />
3334

34-
<script type="text/html" id="template">
35+
<script type="text/html" id="template">
3536
<%
3637
if (data) { data += 12345; }; %>
3738
<li><%= data %></li>
38-
</script>
39-
39+
</script>
40+
</div>
4041
</body>
4142
</html>

underscore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@
278278
_.toArray = function(iterable) {
279279
if (!iterable) return [];
280280
if (iterable.toArray) return iterable.toArray();
281-
if (_.isArray(iterable)) return iterable;
281+
if (_.isArray(iterable)) return slice.call(iterable);
282282
if (_.isArguments(iterable)) return slice.call(iterable);
283283
return _.values(iterable);
284284
};

0 commit comments

Comments
 (0)