2010-07-25 01:23:37 -04:00
|
|
|
a = [((x) -> x), ((x) -> x * x)]
|
2009-12-30 18:09:43 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok a.length is 2
|
2010-01-01 09:49:18 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
regex = /match/i
|
|
|
|
words = "I think there is a match in here."
|
2010-01-01 09:49:18 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok !!words.match(regex)
|
2010-01-01 10:17:33 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
neg = (3 -4)
|
2010-01-01 10:17:33 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok neg is -1
|
2010-01-06 21:21:42 -05:00
|
|
|
|
|
|
|
|
2010-05-31 15:13:48 -04:00
|
|
|
# Decimal number literals.
|
2010-07-25 01:23:37 -04:00
|
|
|
value = .25 + .75
|
2010-05-31 15:13:48 -04:00
|
|
|
ok value is 1
|
2010-07-25 01:23:37 -04:00
|
|
|
value = 0.0 + -.25 - -.75 + 0.0
|
2010-05-31 15:13:48 -04:00
|
|
|
ok value is 0.5
|
|
|
|
|
|
|
|
# Decimals don't interfere with ranges.
|
|
|
|
ok [0..10].join(' ') is '0 1 2 3 4 5 6 7 8 9 10'
|
|
|
|
ok [0...10].join(' ') is '0 1 2 3 4 5 6 7 8 9'
|
|
|
|
|
|
|
|
|
2010-05-31 22:56:51 -04:00
|
|
|
# Can call methods directly on numbers.
|
2010-07-11 20:39:54 -04:00
|
|
|
4.valueOf() is 4
|
2010-05-31 22:56:51 -04:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
func = ->
|
2010-01-06 21:21:42 -05:00
|
|
|
return if true
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok func() is null
|
2010-01-06 23:15:13 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
str = "\\"
|
|
|
|
reg = /\\/
|
2010-01-06 23:15:13 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok reg(str) and str is '\\'
|
2010-01-09 11:58:50 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
trailingComma = [1, 2, 3,]
|
2010-06-12 19:05:13 -04:00
|
|
|
ok (trailingComma[0] is 1) and (trailingComma[2] is 3) and (trailingComma.length is 3)
|
2010-03-26 11:44:25 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
trailingComma = [
|
2010-03-26 14:11:34 -04:00
|
|
|
1, 2, 3,
|
|
|
|
4, 5, 6
|
|
|
|
7, 8, 9,
|
|
|
|
]
|
2010-07-25 01:23:37 -04:00
|
|
|
(sum = (sum or 0) + n) for n in trailingComma
|
2010-03-26 14:11:34 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
trailingComma = {k1: "v1", k2: 4, k3: (-> true),}
|
2010-06-12 19:05:13 -04:00
|
|
|
ok trailingComma.k3() and (trailingComma.k2 is 4) and (trailingComma.k1 is "v1")
|
2010-01-09 11:58:50 -05:00
|
|
|
|
2010-05-12 18:28:40 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
money$ = 'dollars'
|
2010-01-10 15:52:23 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok money$ is 'dollars'
|
2010-02-02 20:36:46 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
multiline = "one
|
|
|
|
two
|
|
|
|
three"
|
2010-03-08 22:25:06 -05:00
|
|
|
|
|
|
|
ok multiline is 'one two three'
|
|
|
|
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok {a: (num) -> num is 10 }.a 10
|
2010-02-15 19:13:08 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
moe = {
|
2010-04-03 10:39:32 -04:00
|
|
|
name: 'Moe'
|
2010-02-02 20:36:46 -05:00
|
|
|
greet: (salutation) ->
|
|
|
|
salutation + " " + @name
|
|
|
|
hello: ->
|
2010-02-20 20:09:52 -05:00
|
|
|
@['greet'] "Hello"
|
2010-02-08 11:48:43 -05:00
|
|
|
10: 'number'
|
2010-02-02 20:36:46 -05:00
|
|
|
}
|
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
ok moe.hello() is "Hello Moe"
|
|
|
|
ok moe[10] is 'number'
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
moe.hello = ->
|
2010-04-03 10:39:32 -04:00
|
|
|
this['greet'] "Hello"
|
|
|
|
|
|
|
|
ok moe.hello() is 'Hello Moe'
|
2010-02-16 08:43:58 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {
|
2010-05-31 15:13:48 -04:00
|
|
|
is: -> yes,
|
|
|
|
'not': -> no,
|
2010-02-16 08:43:58 -05:00
|
|
|
}
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok obj.is()
|
|
|
|
ok not obj.not()
|
2010-06-09 00:37:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Funky indentation within non-comma-seperated arrays.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = [['a']
|
2010-06-09 00:37:23 -04:00
|
|
|
{b: 'c'}]
|
|
|
|
|
|
|
|
ok result[0][0] is 'a'
|
|
|
|
ok result[1]['b'] is 'c'
|
2010-06-15 00:54:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Object literals should be able to include keywords.
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {class: 'höt'}
|
|
|
|
obj.function = 'dog'
|
2010-06-15 00:54:02 -04:00
|
|
|
|
2010-06-21 20:47:58 -04:00
|
|
|
ok obj.class + obj.function is 'hötdog'
|
2010-07-04 20:55:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
# But keyword assignment should be smart enough not to stringify variables.
|
2010-07-25 01:23:37 -04:00
|
|
|
func = ->
|
2010-07-04 20:55:21 -04:00
|
|
|
this == 'this'
|
|
|
|
|
|
|
|
ok func() is false
|
2010-07-25 02:52:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
# New fancy implicit objects:
|
|
|
|
config =
|
|
|
|
development:
|
|
|
|
server: 'localhost'
|
|
|
|
timeout: 10
|
|
|
|
|
|
|
|
production:
|
|
|
|
server: 'dreamboat'
|
|
|
|
timeout: 1000
|
|
|
|
|
|
|
|
ok config.development.server is 'localhost'
|
|
|
|
ok config.production.server is 'dreamboat'
|
|
|
|
ok config.development.timeout is 10
|
2010-07-25 20:46:08 -04:00
|
|
|
ok config.production.timeout is 1000
|
|
|
|
|
|
|
|
obj =
|
|
|
|
a: 1
|
|
|
|
b: 2
|
|
|
|
|
|
|
|
ok obj.a is 1
|
|
|
|
ok obj.b is 2
|
2010-07-30 22:50:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Implicit objects nesting.
|
|
|
|
obj =
|
|
|
|
options:
|
|
|
|
value: yes
|
|
|
|
|
|
|
|
fn: ->
|
|
|
|
{}
|
|
|
|
null
|
|
|
|
|
|
|
|
ok obj.options.value is yes
|
2010-07-31 07:44:49 -04:00
|
|
|
ok obj.fn() is null
|
|
|
|
|
|
|
|
|
|
|
|
# Implicit objects with wacky indentation:
|
|
|
|
obj =
|
|
|
|
reverse: (obj) ->
|
|
|
|
Array.prototype.reverse.call obj
|
|
|
|
abc: ->
|
|
|
|
@reverse(
|
|
|
|
@reverse @reverse ['a', 'b', 'c'].reverse()
|
|
|
|
)
|
|
|
|
one: [1, 2,
|
|
|
|
a: 'b'
|
|
|
|
3, 4]
|
|
|
|
red:
|
|
|
|
orange:
|
|
|
|
yellow:
|
|
|
|
green: 'blue'
|
|
|
|
indigo: 'violet'
|
|
|
|
misdent: [[],
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
[]]
|
|
|
|
|
|
|
|
ok obj.abc().join(' ') is 'a b c'
|
|
|
|
ok obj.one.length is 5
|
|
|
|
ok obj.one[4] is 4
|
|
|
|
ok obj.one[2].a is 'b'
|
|
|
|
ok (key for key of obj.red).length is 2
|
|
|
|
ok obj.red.orange.yellow.green is 'blue'
|
|
|
|
ok obj.red.indigo is 'violet'
|
|
|
|
ok obj.misdent.toString() is ',,,'
|