2010-12-29 00:48:54 -05:00
|
|
|
# Slicing and Splicing
|
|
|
|
# --------------------
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
# * Slicing
|
|
|
|
# * Splicing
|
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
# shared array
|
|
|
|
shared = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Slicing
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "basic slicing", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9] , shared[7..9]
|
|
|
|
arrayEq [2, 3] , shared[2...4]
|
|
|
|
arrayEq [2, 3, 4, 5], shared[2...6]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "slicing with variables as endpoints", ->
|
2010-12-16 20:21:29 -05:00
|
|
|
[a, b] = [1, 4]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [1, 2, 3, 4], shared[a..b]
|
|
|
|
arrayEq [1, 2, 3] , shared[a...b]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "slicing with expressions as endpoints", ->
|
2010-12-16 20:21:29 -05:00
|
|
|
[a, b] = [1, 3]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [2, 3, 4, 5, 6], shared[(a+1)..2*b]
|
|
|
|
arrayEq [2, 3, 4, 5] , shared[a+1...(2*b)]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "unbounded slicing", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9] , shared[7..]
|
|
|
|
arrayEq [8, 9] , shared[-2..]
|
|
|
|
arrayEq [9] , shared[-1...]
|
|
|
|
arrayEq [0, 1, 2] , shared[...3]
|
|
|
|
arrayEq [0, 1, 2, 3], shared[..-7]
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared , shared[..-1]
|
|
|
|
arrayEq shared[0..8], shared[...-1]
|
2010-12-16 20:21:29 -05:00
|
|
|
|
|
|
|
for a in [-shared.length..shared.length]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared[a..] , shared[a...]
|
2010-12-16 20:21:29 -05:00
|
|
|
for a in [-shared.length+1...shared.length]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared[..a][...-1] , shared[...a]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2011-12-20 19:21:26 -05:00
|
|
|
arrayEq [1, 2, 3], [1, 2, 3][..]
|
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
test "#930, #835, #831, #746 #624: inclusive slices to -1 should slice to end", ->
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq shared, shared[0..-1]
|
|
|
|
arrayEq shared, shared[..-1]
|
|
|
|
arrayEq shared.slice(1,shared.length), shared[1..-1]
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "string slicing", ->
|
|
|
|
str = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
ok str[1...1] is ""
|
|
|
|
ok str[1..1] is "b"
|
|
|
|
ok str[1...5] is "bcde"
|
|
|
|
ok str[0..4] is "abcde"
|
|
|
|
ok str[-5..] is "vwxyz"
|
|
|
|
|
2011-09-22 02:14:07 -04:00
|
|
|
test "#1722: operator precedence in unbounded slice compilation", ->
|
|
|
|
list = [0..9]
|
|
|
|
n = 2 # some truthy number in `list`
|
|
|
|
arrayEq [0..n], list[..n]
|
|
|
|
arrayEq [0..n], list[..n or 0]
|
|
|
|
arrayEq [0..n], list[..if n then n else 0]
|
|
|
|
|
2012-05-20 16:32:10 -04:00
|
|
|
test "#2349: inclusive slicing to numeric strings", ->
|
2012-05-20 16:22:25 -04:00
|
|
|
arrayEq [0, 1], [0..10][.."1"]
|
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Splicing
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
test "basic splicing", ->
|
|
|
|
ary = [0..9]
|
|
|
|
ary[5..9] = [0, 0, 0]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 2, 3, 4, 0, 0, 0], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
ary = [0..9]
|
|
|
|
ary[2...8] = []
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 8, 9], ary
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-16 05:14:57 -05:00
|
|
|
test "unbounded splicing", ->
|
|
|
|
ary = [0..9]
|
|
|
|
ary[3..] = [9, 8, 7]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 1, 2, 9, 8, 7]. ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
|
|
|
ary[...3] = [7, 8, 9]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [7, 8, 9, 9, 8, 7], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2011-12-20 19:21:26 -05:00
|
|
|
ary[..] = [1, 2, 3]
|
|
|
|
arrayEq [1, 2, 3], ary
|
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
test "splicing with variables as endpoints", ->
|
|
|
|
[a, b] = [1, 8]
|
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[a..b] = [2, 3]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 2, 3, 9], ary
|
2010-12-16 05:14:57 -05:00
|
|
|
|
2010-12-16 20:21:29 -05:00
|
|
|
ary = [0..9]
|
|
|
|
ary[a...b] = [5]
|
2010-12-18 14:45:32 -05:00
|
|
|
arrayEq [0, 5, 8, 9], ary
|
2010-12-16 20:21:29 -05:00
|
|
|
|
2010-12-20 23:41:58 -05:00
|
|
|
test "splicing with expressions as endpoints", ->
|
|
|
|
[a, b] = [1, 3]
|
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[ a+1 .. 2*b+1 ] = [4]
|
|
|
|
arrayEq [0, 1, 4, 8, 9], ary
|
2010-12-22 01:39:58 -05:00
|
|
|
|
|
|
|
ary = [0..9]
|
|
|
|
ary[a+1...2*b+1] = [4]
|
|
|
|
arrayEq [0, 1, 4, 7, 8, 9], ary
|
2010-12-23 17:02:46 -05:00
|
|
|
|
|
|
|
test "splicing to the end, against a one-time function", ->
|
|
|
|
ary = null
|
|
|
|
fn = ->
|
|
|
|
if ary
|
|
|
|
throw 'err'
|
|
|
|
else
|
|
|
|
ary = [1, 2, 3]
|
|
|
|
|
|
|
|
fn()[0..] = 1
|
|
|
|
|
|
|
|
arrayEq ary, [1]
|
2010-12-23 17:46:34 -05:00
|
|
|
|
|
|
|
test "the return value of a splice literal should be the RHS", ->
|
|
|
|
ary = [0, 0, 0]
|
|
|
|
eq (ary[0..1] = 2), 2
|
|
|
|
|
|
|
|
ary = [0, 0, 0]
|
|
|
|
eq (ary[0..] = 3), 3
|
|
|
|
|
|
|
|
arrayEq [ary[0..0] = 0], [0]
|
2011-09-21 18:56:20 -04:00
|
|
|
|
2011-09-22 02:14:07 -04:00
|
|
|
test "#1723: operator precedence in unbounded splice compilation", ->
|
|
|
|
n = 4 # some truthy number in `list`
|
|
|
|
|
2011-09-21 18:56:20 -04:00
|
|
|
list = [0..9]
|
2011-09-22 02:14:07 -04:00
|
|
|
list[..n] = n
|
|
|
|
arrayEq [n..9], list
|
|
|
|
|
|
|
|
list = [0..9]
|
|
|
|
list[..n or 0] = n
|
|
|
|
arrayEq [n..9], list
|
|
|
|
|
|
|
|
list = [0..9]
|
|
|
|
list[..if n then n else 0] = n
|
|
|
|
arrayEq [n..9], list
|
2013-04-22 23:42:37 -04:00
|
|
|
|
|
|
|
test "#2953: methods on endpoints in assignment from array splice literal", ->
|
|
|
|
list = [0..9]
|
|
|
|
|
|
|
|
Number.prototype.same = -> this
|
|
|
|
list[1.same()...9.same()] = 5
|
|
|
|
delete Number.prototype.same
|
|
|
|
|
|
|
|
arrayEq [0, 5, 9], list
|