Make sure $A works with primitive values.

This commit is contained in:
Juriy Zaytsev 2009-04-17 22:08:37 -04:00 committed by Andrew Dupont
parent cebf7d673b
commit 31fe0a0328
3 changed files with 13 additions and 1 deletions

View File

@ -1,3 +1,5 @@
* Make sure $A works with primitive values. (mr_justin, kangax)
* Do not browser sniff when forking `unmark` function in selector suite. Instead use a proper test - PROPERTIES_ATTRIBUTES_MAP. (kangax)
* Do not use short-hand element methods notation (@element.getStyle() -> Element.getStyle(@element)) for performance reasons. Do not use `$A` and `Array.prototype.shift` when `Array.prototype.slice` can be used instead. (kangax)

View File

@ -10,7 +10,7 @@ function $A(iterable) {
if (!iterable) return [];
// Safari <2.0.4 crashes when accessing property of a node list with property accessor.
// It nevertheless works fine with `in` operator, which is why we use it here
if ('toArray' in iterable) return iterable.toArray();
if ('toArray' in Object(iterable)) return iterable.toArray();
var length = iterable.length || 0, results = new Array(length);
while (length--) results[length] = iterable[length];
return results;

View File

@ -33,6 +33,16 @@ new Test.Unit.Runner({
this.assertEqual(3, $A(element.childNodes).length);
},
testToArrayOnPrimitive: function() {
this.assertEnumEqual(['a', 'b', 'c'], $A('abc'));
this.assertEnumEqual([], $A(''));
this.assertEnumEqual([], $A(null));
this.assertEnumEqual([], $A(undefined));
this.assertEnumEqual([], $A());
this.assertEnumEqual([], $A(5));
this.assertEnumEqual([], $A(true));
},
testClear: function(){
this.assertEnumEqual([], [].clear());
this.assertEnumEqual([], [1].clear());