Skip to content

Commit b1ad22a

Browse files
committed
CLJS-2538: nth on fractional indices near array and string bounds
1 parent 9e94a79 commit b1ad22a

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

src/main/cljs/cljs/core.cljs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,13 +1862,13 @@ reduces them without incurring seq initialization"
18621862
(-nth coll n)
18631863

18641864
(array? coll)
1865-
(if (and (>= n 0) (< n (.-length coll)))
1866-
(aget coll n)
1865+
(if (and (< -1 n (.-length coll)))
1866+
(aget coll (int n))
18671867
(throw (js/Error. "Index out of bounds")))
18681868

18691869
(string? coll)
1870-
(if (and (>= n 0) (< n (.-length coll)))
1871-
(.charAt coll n)
1870+
(if (and (< -1 n (.-length coll)))
1871+
(.charAt coll (int n))
18721872
(throw (js/Error. "Index out of bounds")))
18731873

18741874
(or (implements? ISeq coll)
@@ -1895,13 +1895,13 @@ reduces them without incurring seq initialization"
18951895
(-nth coll n not-found)
18961896

18971897
(array? coll)
1898-
(if (and (>= n 0) (< n (.-length coll)))
1899-
(aget coll n)
1898+
(if (and (< -1 n (.-length coll)))
1899+
(aget coll (int n))
19001900
not-found)
19011901

19021902
(string? coll)
1903-
(if (and (>= n 0) (< n (.-length coll)))
1904-
(.charAt coll n)
1903+
(if (and (< -1 n (.-length coll)))
1904+
(.charAt coll (int n))
19051905
not-found)
19061906

19071907
(or (implements? ISeq coll)

src/test/cljs/cljs/core_test.cljs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,38 @@
15451545
(is (= \a (get "ab" -0.5)))
15461546
(is (= \a (get "ab" -0.5 :not-found))))
15471547

1548+
(deftest test-cljs-2538
1549+
(testing "fractional indices in nth on arrays"
1550+
(is (thrown-with-msg? js/Error #"Index out of bounds" (nth (to-array [1 2]) -1)))
1551+
(is (= :not-found (nth (to-array [1 2]) -1 :not-found)))
1552+
(is (== 1 (nth (to-array [1 2]) -0.5)))
1553+
(is (== 1 (nth (to-array [1 2]) -0.5 :not-found)))
1554+
(is (== 1 (nth (to-array [1 2]) 0)))
1555+
(is (== 1 (nth (to-array [1 2]) 0 :not-found)))
1556+
(is (== 1 (nth (to-array [1 2]) 0.5)))
1557+
(is (== 1 (nth (to-array [1 2]) 0.5 :not-found)))
1558+
(is (== 2 (nth (to-array [1 2]) 1)))
1559+
(is (== 2 (nth (to-array [1 2]) 1 :not-found)))
1560+
(is (== 2 (nth (to-array [1 2]) 1.5)))
1561+
(is (== 2 (nth (to-array [1 2]) 1.5 :not-found)))
1562+
(is (thrown-with-msg? js/Error #"Index out of bounds" (nth (to-array [1 2]) 2)))
1563+
(is (= :not-found (nth (to-array [1 2]) 2 :not-found))))
1564+
(testing "fractional indices in nth on strings"
1565+
(is (thrown-with-msg? js/Error #"Index out of bounds" (nth "ab" -1)))
1566+
(is (= :not-found (nth "ab" -1 :not-found)))
1567+
(is (== \a (nth "ab" -0.5)))
1568+
(is (== \a (nth "ab" -0.5 :not-found)))
1569+
(is (== \a (nth "ab" 0)))
1570+
(is (== \a (nth "ab" 0 :not-found)))
1571+
(is (== \a (nth "ab" 0.5)))
1572+
(is (== \a (nth "ab" 0.5 :not-found)))
1573+
(is (== \b (nth "ab" 1)))
1574+
(is (== \b (nth "ab" 1 :not-found)))
1575+
(is (== \b (nth "ab" 1.5)))
1576+
(is (== \b (nth "ab" 1.5 :not-found)))
1577+
(is (thrown-with-msg? js/Error #"Index out of bounds" (nth "ab" 2)))
1578+
(is (= :not-found (nth "ab" 2 :not-found)))))
1579+
15481580
(deftest test-cljs-2549
15491581
(let [tap (fn [_])]
15501582
(add-tap tap)

0 commit comments

Comments
 (0)