From 31fe0a032809d9aacc5fa22369647644d5f43f01 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 17 Apr 2009 22:08:37 -0400 Subject: [PATCH] Make sure $A works with primitive values. --- CHANGELOG | 2 ++ src/lang/array.js | 2 +- test/unit/array_test.js | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c4485b2..6b8f650 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/src/lang/array.js b/src/lang/array.js index 8d6e1d4..afe5e76 100644 --- a/src/lang/array.js +++ b/src/lang/array.js @@ -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; diff --git a/test/unit/array_test.js b/test/unit/array_test.js index d090504..7fa07b0 100644 --- a/test/unit/array_test.js +++ b/test/unit/array_test.js @@ -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());