2010-07-25 01:23:37 -04:00
|
|
|
num = 10
|
2009-12-24 04:33:59 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = switch num
|
2009-12-28 23:08:02 -05:00
|
|
|
when 5 then false
|
|
|
|
when 'a'
|
|
|
|
true
|
|
|
|
true
|
|
|
|
false
|
|
|
|
when 10 then true
|
2010-01-09 13:25:44 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Mid-switch comment with whitespace
|
|
|
|
# and multi line
|
2009-12-28 23:08:02 -05:00
|
|
|
when 11 then false
|
|
|
|
else false
|
2010-01-09 13:25:44 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result
|
2010-01-13 19:56:35 -05:00
|
|
|
|
2010-02-12 16:23:52 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
func = (num) ->
|
2010-01-13 19:56:35 -05:00
|
|
|
switch num
|
|
|
|
when 2, 4, 6
|
|
|
|
true
|
|
|
|
when 1, 3, 5
|
|
|
|
false
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok func(2)
|
|
|
|
ok func(6)
|
|
|
|
ok !func(3)
|
2010-10-20 23:45:50 -04:00
|
|
|
eq func(8), undefined
|
2010-02-17 21:23:59 -05:00
|
|
|
|
2010-02-22 19:22:09 -05:00
|
|
|
|
|
|
|
# Ensure that trailing switch elses don't get rewritten.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = false
|
2010-02-22 19:22:09 -05:00
|
|
|
switch "word"
|
|
|
|
when "one thing"
|
2010-06-12 19:05:13 -04:00
|
|
|
doSomething()
|
2010-02-22 19:22:09 -05:00
|
|
|
else
|
2010-07-25 01:23:37 -04:00
|
|
|
result = true unless false
|
2010-02-22 19:22:09 -05:00
|
|
|
|
|
|
|
ok result
|
2010-02-22 20:17:54 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = false
|
2010-02-22 20:17:54 -05:00
|
|
|
switch "word"
|
|
|
|
when "one thing"
|
2010-06-12 19:05:13 -04:00
|
|
|
doSomething()
|
2010-02-22 20:17:54 -05:00
|
|
|
when "other thing"
|
2010-06-12 19:05:13 -04:00
|
|
|
doSomething()
|
2010-02-22 20:17:54 -05:00
|
|
|
else
|
2010-07-25 01:23:37 -04:00
|
|
|
result = true unless false
|
2010-02-22 20:17:54 -05:00
|
|
|
|
|
|
|
ok result
|
2010-05-10 22:57:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Should be able to handle switches sans-condition.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = switch
|
2010-11-14 15:07:43 -05:00
|
|
|
when null then 0
|
|
|
|
when !1 then 1
|
|
|
|
when '' not of {''} then 2
|
|
|
|
when [] not instanceof Array then 3
|
|
|
|
when true is false then 4
|
|
|
|
when 'x' < 'y' > 'z' then 5
|
|
|
|
when 'a' in ['b', 'c'] then 6
|
|
|
|
when 'd' in (['e', 'f']) then 7
|
|
|
|
else ok
|
|
|
|
|
|
|
|
eq result, ok
|
2010-05-31 15:13:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Should be able to use "@properties" within the switch clause.
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {
|
2010-05-31 15:13:48 -04:00
|
|
|
num: 101
|
|
|
|
func: ->
|
|
|
|
switch @num
|
|
|
|
when 101 then '101!'
|
|
|
|
else 'other'
|
|
|
|
}
|
|
|
|
|
|
|
|
ok obj.func() is '101!'
|
2010-09-28 14:24:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Should be able to use "@properties" within the switch cases.
|
|
|
|
obj = {
|
|
|
|
num: 101
|
|
|
|
func: (yesOrNo) ->
|
|
|
|
result = switch yesOrNo
|
|
|
|
when yes then @num
|
|
|
|
else 'other'
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
ok obj.func(yes) is 101
|