mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 17:37:50 +00:00
Merge pull request #233 from chaoflow/master
make toArray return a clone in case of an array
This commit is contained in:
@@ -41,7 +41,7 @@ $(document).ready(function() {
|
|||||||
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
|
var doubled = _([1, 2, 3]).map(function(num){ return num * 2; });
|
||||||
equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
|
equals(doubled.join(', '), '2, 4, 6', 'OO-style doubled numbers');
|
||||||
|
|
||||||
var ids = _.map(document.body.childNodes, function(n){ return n.id; });
|
var ids = _.map($('div.underscore-test').children(), function(n){ return n.id; });
|
||||||
ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
|
ok(_.include(ids, 'qunit-header'), 'can use collection methods on NodeLists');
|
||||||
|
|
||||||
var ids = _.map(document.images, function(n){ return n.id; });
|
var ids = _.map(document.images, function(n){ return n.id; });
|
||||||
@@ -197,6 +197,9 @@ $(document).ready(function() {
|
|||||||
test('collections: toArray', function() {
|
test('collections: toArray', function() {
|
||||||
ok(!_.isArray(arguments), 'arguments object is not an array');
|
ok(!_.isArray(arguments), 'arguments object is not an array');
|
||||||
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
||||||
|
var a = [1,2,3];
|
||||||
|
ok(_.toArray(a) !== a, 'array is cloned');
|
||||||
|
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');
|
||||||
|
|
||||||
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
||||||
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
<script type="text/javascript" src="speed.js"></script>
|
<script type="text/javascript" src="speed.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<div class="underscore-test">
|
||||||
<h1 id="qunit-header">Underscore Test Suite</h1>
|
<h1 id="qunit-header">Underscore Test Suite</h1>
|
||||||
<h2 id="qunit-banner"></h2>
|
<h2 id="qunit-banner"></h2>
|
||||||
<h2 id="qunit-userAgent"></h2>
|
<h2 id="qunit-userAgent"></h2>
|
||||||
@@ -36,6 +37,6 @@
|
|||||||
if (data) { data += 12345; }; %>
|
if (data) { data += 12345; }; %>
|
||||||
<li><%= data %></li>
|
<li><%= data %></li>
|
||||||
</script>
|
</script>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -278,7 +278,7 @@
|
|||||||
_.toArray = function(iterable) {
|
_.toArray = function(iterable) {
|
||||||
if (!iterable) return [];
|
if (!iterable) return [];
|
||||||
if (iterable.toArray) return iterable.toArray();
|
if (iterable.toArray) return iterable.toArray();
|
||||||
if (_.isArray(iterable)) return iterable;
|
if (_.isArray(iterable)) return slice.call(iterable);
|
||||||
if (_.isArguments(iterable)) return slice.call(iterable);
|
if (_.isArguments(iterable)) return slice.call(iterable);
|
||||||
return _.values(iterable);
|
return _.values(iterable);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user