Skip to content

Commit 1f12642

Browse files
committed
Slice and Splice tests are back on master.
1 parent 9111c2e commit 1f12642

File tree

7 files changed

+232
-119
lines changed

7 files changed

+232
-119
lines changed

lib/grammar.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/nodes.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/parser.js

Lines changed: 116 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/grammar.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ grammar =
245245
o ':: Identifier', -> new Accessor $2, 'proto'
246246
o '::', -> new Accessor new Literal 'prototype'
247247
o 'Index'
248+
o 'Slice', -> new Slice $1
248249
]
249250

250251
# Indexing into an object or array using bracket notation.

src/nodes.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ exports.Assign = class Assign extends Base
871871
compileNode: (o) ->
872872
if isValue = @variable instanceof Value
873873
return @compilePatternMatch o if @variable.isArray() or @variable.isObject()
874+
return @compileSplice o if @variable.isSplice()
874875
return @compileConditional o if @context in ['||=', '&&=', '?=']
875876
name = @variable.compile o, LEVEL_LIST
876877
if @value instanceof Code and match = @METHOD_DEF.exec name

test/test_comprehensions.coffee

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,34 @@ ok odds.join(' ') is "one! three!"
1616

1717
# Basic range comprehensions.
1818
nums = (i * 3 for i in [1..3])
19+
1920
negs = (x for x in [-20..-5*2])
20-
eq nums.concat(negs.slice 0, 3).join(' '), '3 6 9 -20 -19 -18'
21+
negs = negs[0..2]
22+
23+
result = nums.concat(negs).join(', ')
24+
25+
ok result is '3, 6, 9, -20, -19, -18'
2126

2227

2328
# With range comprehensions, you can loop in steps.
24-
eq "#{ x for x in [0..9] by 3 }", '0,3,6,9'
25-
eq "#{ x for x in [9..0] by -3 }", '9,6,3,0'
26-
eq "#{ x for x in [3*3..0*0] by 0-3 }", '9,6,3,0'
29+
results = (x for x in [0...15] by 5)
30+
ok results.join(' ') is '0 5 10'
31+
32+
results = (x for x in [0..100] by 10)
33+
ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
34+
35+
36+
# And can loop downwards, with a negative step.
37+
results = (x for x in [5..1])
38+
39+
ok results.join(' ') is '5 4 3 2 1'
40+
ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
41+
42+
results = (x for x in [10..1])
43+
ok results.join(' ') is [10..1].join(' ')
44+
45+
results = (x for x in [10...0] by -2)
46+
ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
2747

2848

2949
# Range comprehension gymnastics.
@@ -110,6 +130,11 @@ results = for i in [1..3]
110130
ok results.join(', ') is '3 , 3 6 , 3 6 9'
111131

112132

133+
# Naked ranges are expanded into arrays.
134+
array = [0..10]
135+
ok(num % 2 is 0 for num in array by 2)
136+
137+
113138
# Nested comprehensions.
114139
multiLiner =
115140
for x in [3..5]
@@ -153,6 +178,11 @@ ok own.join(' ') is 'Whiskers'
153178
ok all.sort().join(' ') is 'Whiskers cream tabby'
154179

155180

181+
# Optimized range comprehensions.
182+
exxes = ('x' for [0...10])
183+
ok exxes.join(' ') is 'x x x x x x x x x x'
184+
185+
156186
# Comprehensions safely redeclare parameters if they're not present in closest
157187
# scope.
158188
rule = (x) -> x
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# # Slice.
2+
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3+
4+
a = array[7..9]
5+
b = array[2...4]
6+
7+
result = a.concat(b).join(' ')
8+
9+
ok result is "7 8 9 2 3"
10+
11+
a = [0, 1, 2, 3, 4, 5, 6, 7]
12+
eq a[2...6].join(' '), '2 3 4 5'
13+
14+
15+
# Ranges.
16+
countdown = [10..1].join(' ')
17+
ok countdown is "10 9 8 7 6 5 4 3 2 1"
18+
19+
a = 1
20+
b = 5
21+
nums = [a...b]
22+
ok nums.join(' ') is '1 2 3 4'
23+
24+
b = -5
25+
nums = [a..b]
26+
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
27+
28+
29+
# Expression-based range.
30+
array = [(1+5)..1+9]
31+
ok array.join(' ') is "6 7 8 9 10"
32+
33+
array = [5..1]
34+
ok array.join(' ') is '5 4 3 2 1'
35+
36+
array = [30...0]
37+
ok (len = array.length) is 30
38+
ok array[len - 1] is 1
39+
40+
41+
42+
# String slicing (at least on Node).
43+
hello = "Hello World"
44+
45+
ok hello[1...1] is ""
46+
ok hello[1..1] is "e"
47+
ok hello[1...5] is "ello"
48+
ok hello[0..4] is "Hello"
49+
50+
51+
# Splice literals.
52+
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
53+
54+
array[5..10] = [0, 0, 0]
55+
56+
ok array.join(' ') is '0 1 2 3 4 0 0 0'
57+
58+
59+
# Slices and splices that omit their beginning or end.
60+
array = [0..10]
61+
62+
ok array[7..].join(' ') is '7 8 9 10'
63+
ok array[-2..].join(' ') is '9 10'
64+
65+
ok array[...3].join(' ') is '0 1 2'
66+
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
67+
68+
array[3..] = [9, 8, 7]
69+
70+
ok array.join(' ') is '0 1 2 9 8 7'
71+
72+
array[...3] = [7, 8, 9]
73+
74+
ok array.join(' ') is '7 8 9 9 8 7'

0 commit comments

Comments
 (0)