Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLJS-728: fix (get coll k) when coll is vector-like and k is non-nume…

…ric.
  • Loading branch information...
commit 71f781c75bf6e9d19be8d0e529ed0275fa523942 1 parent 0f943b6
@favila favila authored swannodette committed
Showing with 92 additions and 55 deletions.
  1. +68 −55 src/cljs/cljs/core.cljs
  2. +24 −0 test/cljs/cljs/core_test.cljs
View
123 src/cljs/cljs/core.cljs
@@ -900,56 +900,63 @@ reduces them without incurring seq initialization"
also works for strings, arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
([coll n]
- (when-not (nil? coll)
- (cond
- (implements? IIndexed coll)
- (-nth ^not-native coll n)
-
- (array? coll)
- (when (< n (.-length coll))
- (aget coll n))
-
- (string? coll)
- (when (< n (.-length coll))
- (aget coll n))
-
- (native-satisfies? IIndexed coll)
- (-nth coll n)
-
- :else
- (if (satisfies? ISeq coll)
- (linear-traversal-nth coll n)
- (throw
- (js/Error.
- (str "nth not supported on this type "
- (type->str (type coll)))))))))
+ (cond
+ (not (number? n))
+ (throw (js/Error. "index argument to nth must be a number"))
+
+ (nil? coll)
+ coll
+
+ (implements? IIndexed coll)
+ (-nth ^not-native coll n)
+
+ (array? coll)
+ (when (< n (.-length coll))
+ (aget coll n))
+
+ (string? coll)
+ (when (< n (.-length coll))
+ (aget coll n))
+
+ (native-satisfies? IIndexed coll)
+ (-nth coll n)
+
+ (satisfies? ISeq coll)
+ (linear-traversal-nth coll n)
+
+ :else
+ (throw (js/Error. (str "nth not supported on this type "
+ (type->str (type coll)))))))
([coll n not-found]
- (if-not (nil? coll)
- (cond
- (implements? IIndexed coll)
- (-nth ^not-native coll n not-found)
-
- (array? coll)
- (if (< n (.-length coll))
- (aget coll n)
- not-found)
-
- (string? coll)
- (if (< n (.-length coll))
- (aget coll n)
- not-found)
-
- (native-satisfies? IIndexed coll)
- (-nth coll n)
-
- :else
- (if (satisfies? ISeq coll)
- (linear-traversal-nth coll n not-found)
- (throw
- (js/Error.
- (str "nth not supported on this type "
- (type->str (type coll)))))))
- not-found)))
+ (cond
+ (not (number? n))
+ (throw (js/Error. "index argument to nth must be a number."))
+
+ (nil? coll)
+ not-found
+
+ (implements? IIndexed coll)
+ (-nth ^not-native coll n not-found)
+
+ (array? coll)
+ (if (< n (.-length coll))
+ (aget coll n)
+ not-found)
+
+ (string? coll)
+ (if (< n (.-length coll))
+ (aget coll n)
+ not-found)
+
+ (native-satisfies? IIndexed coll)
+ (-nth coll n)
+
+ (satisfies? ISeq coll)
+ (linear-traversal-nth coll n not-found)
+
+ :else
+ (throw (js/Error. (str "nth not supported on this type "
+ (type->str (type coll))))))))
(defn get
"Returns the value mapped to key, not-found or nil if key not present."
@@ -3392,8 +3399,10 @@ reduces them without incurring seq initialization"
not-found))
ILookup
- (-lookup [coll k] (-nth coll k nil))
- (-lookup [coll k not-found] (-nth coll k not-found))
+ (-lookup [coll k] (-lookup coll k nil))
+ (-lookup [coll k not-found] (if (number? k)
+ (-nth coll k not-found)
+ not-found))
IMapEntry
(-key [coll]
@@ -3635,8 +3644,10 @@ reduces them without incurring seq initialization"
(-nth v (+ start n) not-found)))
ILookup
- (-lookup [coll k] (-nth coll k nil))
- (-lookup [coll k not-found] (-nth coll k not-found))
+ (-lookup [coll k] (-lookup coll k nil))
+ (-lookup [coll k not-found] (if (number? k)
+ (-nth coll k not-found)
+ not-found))
IAssociative
(-assoc [coll key val]
@@ -3856,9 +3867,11 @@ reduces them without incurring seq initialization"
not-found))
ILookup
- (-lookup [coll k] (-nth coll k nil))
+ (-lookup [coll k] (-lookup coll k nil))
- (-lookup [coll k not-found] (-nth coll k not-found))
+ (-lookup [coll k not-found] (if (number? k)
+ (-nth coll k not-found)
+ not-found))
IFn
(-invoke [coll k]
View
24 test/cljs/cljs/core_test.cljs
@@ -2127,5 +2127,29 @@
(assert (= (with-out-str (doseq [fn (cljs-739 [] [:a :b :c :d])] (fn)))
":a\n:b\n:c\n:d\n"))
+ ;; CLJS-728
+
+ (doseq [n [nil "-1" "" "0" "1" false true (js-obj)]]
+ (assert (nil? (get [1 2] n)))
+ (assert (= :fail (try (nth [1 2] n) (catch js/Error e :fail))))
+ (assert (= 4 (get [1 2] n 4)))
+ (assert (= :fail (try (nth [1 2] n 4) (catch js/Error e :fail))))
+
+ (assert (nil? (get (subvec [1 2] 1) n)))
+ (assert (= :fail (try (nth (subvec [1 2] 1) n) (catch js/Error e :fail))))
+ (assert (= 4 (get (subvec [1 2] 1) n 4)))
+ (assert (= :fail (try (nth (subvec [1 2] 1) n 4) (catch js/Error e :fail))))
+
+ (assert (nil? (get (transient [1 2]) n)))
+ (assert (= :fail (try (nth (transient [1 2]) n) (catch js/Error e :fail))))
+ (assert (= 4 (get (transient [1 2]) n 4)))
+ (assert (= :fail (try (nth (transient [1 2]) n 4) (catch js/Error e :fail))))
+
+ (assert (nil? (get (range 1 3) n)))
+ (assert (= :fail (try (nth (range 1 3) n) (catch js/Error e :fail))))
+ (assert (= 4 (get (range 1 3) n 4)))
+ (assert (= :fail (try (nth (range 1 3) n 4) (catch js/Error e :fail)))))
+
+
:ok
)
Please sign in to comment.
Something went wrong with that request. Please try again.