Skip to content

Instantly share code, notes, and snippets.

@dz1984
Last active December 31, 2015 05:59
Show Gist options
  • Save dz1984/7944868 to your computer and use it in GitHub Desktop.
Save dz1984/7944868 to your computer and use it in GitHub Desktop.
學習寫LiveScript的起手式,可以從Version History看到慢慢增加練習的過程。
; See http://EditorConfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.ls]
; See https://github.com/gkz/LiveScript-style-guide
indent_size = 2

Learn to LiveScript Memo

Install

Install LiveScript to Global Env.

npm install -g LiveScript

UnitTest

Preinstall

npm install --save-dev chai
npm install --save-dev mocha
npm install --save-dev LiveScript

Mocha options

mocha.opts file.

--require LiveScript
--colors
--reporter spec
--compilers ls:LiveScript
should = (require \chai).should
expect = (require \chai).expect
# Comment here.
describe "Hello World", -> ``it``
.. "should be true", (done) ->
expect true .to.equal true
done!
.. "should be equal", (done) ->
expect "Hello World" .to.equal "Hello World"
done!
describe "Function", -> ``it``
.. "should be work.", (done) ->
expect ((x,y) -> x+y) 2,3 .to.equals 5
times = (x,y) -> x * y
expect times(2,2) .to.equals 4
done!
describe "Assignment", -> ``it``
.. "should be assign", (done) ->
x = 10
do ->
x = 5
expect x .to.equals 10
do ->
x := 2
expect x .to.equals 2
x = if 2+2 == 4 then 10 else 0
expect x .to.equals 10
done!
describe "Literals", ->
describe "Number", -> ``it``
.. "should be correct", (done) ->
expect 6~12 .to.equals 8
expect 2~1000 .to.equals 8
expect 16~ff .to.equals 255
done!
describe "Boolean", -> ``it``
.. "should all true", (done) ->
expect on .to.be.true
expect off .to.be.false
expect yes .to.be.true
expect no .to.be.false
done!
describe "String", -> ``it``
.. "should be correct", (done) ->
expect \word .to.equals "word"
expect "The answer is #{2+2}" .to.equals "The answer is 4"
expect 'The answer is #{2+2}' .to.equals 'The answer is #{2+2}'
name = \DonaldIsFreak
expect "Hello! #name" .to.equals "Hello! DonaldIsFreak"
done!
describe "Dynamic keys", -> ``it``
.. "should be operate with list", (done) ->
red = 0
yellow = 3
colors = %"#red #yellow"
expect colors .to.be.a 'array'
expect colors .to.have.length 3
expect colors[0] .to.equals 0
expect colors[1] .to.equals ' '
expect colors[2] .to.equals 3
done!
describe "Multiline string" -> ``it``
.. "should show multiline message", (done) ->
multiline = 'This is a long long \
long long \
long long message'
expect multiline .to.equals 'This is a long long long long long long message'
heredoc = '''
This is another long long
long long
long long message
'''
expect heredoc .to.equals 'This is another long long\nlong long\nlong long message'
done!
describe 'Lists', -> ``it``
.. 'should be array', (done) ->
arrs = [1 2 true]
expect arrs .to.be.a 'array'
expect arrs[0] .to.be.equals 1
expect arrs[1] .to.be.equals 2
expect arrs[2] .to.be.equals true
done!
.. 'created with an indented block.', (done) ->
my-list =
32+1
true
'block'
...
expect my-list .to.be.a 'array'
expect my-list.0 .to.be.equals 33
expect my-list.1 .to.be.equals true
expect my-list.2 .to.be.equals 'block'
done!
.. 'use an asterisk', (done) ->
tree =
* 1
* 2
3
4
* 5
6
* 7
8
* 9
10
11
expect tree .to.be.a 'array'
expect tree.0 .to.be.a 'array'
expect tree.0.1 .to.be.a 'array'
expect tree.1.2 .to.be.a 'array'
expect tree.1.2.2 .to.be.a 'array'
expect tree.length .to.be.equals 2
expect tree.0.length .to.be.equals 3
expect tree.1.length .to.be.equals 4
done!
.. 'obj-list', (done) ->
obj-list =
* name: 'test1'
age: 23
* name: 'test2'
age: 23
expect obj-list .to.be.a 'array'
expect obj-list.length .to.be.equals 2
done!
.. 'obj', (done) ->
obj =
* name: 'test'
age: 23
expect obj .to.be.a 'object'
expect obj .not.to.be.a 'array'
done!
.. 'obj-one-list', (done) ->
obj-one-list =
* name: 'test'
age: 23
...
expect obj-one-list .to.be.a 'array'
expect obj-one-list .not.to.be.a 'object'
done!
.. 'lists of words', (done) ->
lists-word = <[ list of words ]>
expect lists-word .to.be.a 'array'
expect lists-word.length .to.be.equals 3
done!
describe "Ranges", -> ``it``
.. "using to", (done) ->
nums = [1 to 5]
expect nums .to.be.a 'array'
expect nums.join '' .to.be.equals '12345'
expect nums.length .to.be.equals 5
nums = [4 to 1]
expect nums .to.be.a 'array'
expect nums.join '' .to.be.equals '4321'
expect nums.length .to.be.equals 4
nums = [to 5]
expect nums .to.be.a 'array'
expect nums.0 .to.be.equals 0
expect nums.join '' .to.be.equals '012345'
expect nums.length .to.be.equals 6
chars = [\A to \D]
expect chars .have.length 4
.that.is.an 'array'
.that.have.members <[ A B C D ]>
done!
.. 'using til', (done) ->
nums = [1 til 5]
expect nums .have.length 4
.that.is.an 'array'
.that.have.members [1 2 3 4]
done!
.. 'using to .. by', (done) ->
nums = [1 to 10 by 2]
expect nums .have.length 5
.that.is.an 'array'
.that.have.members [1 3 5 7 9]
done!
describe "Operators", ->
describe "Number", -> ``it``
.. 'remainder operator', (done) ->
expect 2**4 .to.be.equals 2^4
expect 2^2^2 .to.be.equals 16
done!
.. 'bitwise and shift operator', (done) ->
expect 14 .&. 9 .to.be.equals 8
expect 14 .|. 9 .to.be.equals 15
expect 14 .^. 9 .to.be.equals 7
expect ~9 .to.be.equals -10
expect 9 .<<. 2 .to.be.equals 36
expect -9 .>>. 2 .to.be.equals -3
expect -9 .>>>. 2 .to.be.equals 1073741821
done!
.. 'casting to a number', (done) ->
expect +'4' .to.be.equals 4
expect -'3' .to.be.equals -3
done!
describe "Comparison", -> ``it``
.. 'strict equality', (done) ->
expect 2+4==6 .to.be.equals true
expect \boom is 'boom' .to.be.equals true
done!
.. 'Fuzzy equality', (done) ->
expect 2 ~= '2' .to.be.true
expect \1 !~= 1 .to.be.false
done!
.. 'Chained comparison', (done) ->
expect 1 < 5 < 6 .to.be.equals 1<5 && 5<6
expect 2 < 5 == 10/2 > 2 .to.be.equals 2<5 && 5==(10/2) && (10/2)>2
expect 5 < 10 != 20/3 > 5 .to.be.equals 5<10 && 10!=(20/3) && (20/3)>5
done!
.. 'Min/Max of the two operand', (done) ->
expect 12>?8 .to.be.equals 12
expect 10-5 <? 20/3 .to.be.equals 5
done!
.. 'regex literal test operand', (done) ->
expect /^e(.*)/ is 'enter' .not.to.be.a.null
expect /^e(.*)/ == 'foo' .to.be.a.null
expect /foo/ != 'boo' .to.be.true
done!
describe 'Logic', -> ``it``
.. 'and, or, and xor close implicit call', (done) ->
even = (n) ->
!(n%2)
expect (even 0 and 5) .to.be.equals 5
expect (even 0 && 5) .to.be.true
done!
describe 'In/Of', -> ``it``
.. 'In/Of chek', (done) ->
lists = [7 to 10]
expect 2 in [1 to 5] .to.be.true
expect 3 in lists .to.be.false
expect \id of id:30,name: \DonaldIsFreak .to.be.true
done!
describe 'Pipling', -> ``it``
.. 'Piping', (done) ->
head = (arrs) ->
arrs.0
reverse = (arrs) ->
arrs.reverse!
expect ([1 to 5] |> reverse |> head) .to.be.equals 5
expect (head <| reverse <| [5 to 1]) .to.be.equals 1
expect (9 |> (* 2) |> (- 8)) .to.be.equals 10
done!
describe 'Function', -> ``it``
.. 'composing function', (done) ->
even = (x) -> !(x%2)
odd = (not) << even
expect odd 3 .to.be.true
add-three-times-three = (+ 3) >> (* 3)
times-three-add-three = (+ 3) << (* 3)
expect add-three-times-three 5 .to.be.equals 24
expect times-three-add-three 5 .to.be.equals 18
done!
describe 'List', -> ``it``
.. 'concatenate tow lists', (done) ->
temp = <[ one two three ]> ++ [\foo]
expect temp .is.an 'array'
.and.have.length 4
.and.have.members <[one two three foo]>
expect [\ha]*3 .have.length 3
.that.to.have.members ['ha']
.that.is.an 'array'
expect <[one two three]> * \, .to.be.equals <[ one two three ]>.join(',')
.that.is.a 'string'
expect typeof! [\b 5 {}] .have.length 3
.that.to.have.members <[String Number Object]>
.that.is.an 'array'
expect ~[4 5] .is.an 'array'
.and.have.length 2
.and.have.members [-5,-6]
player =
strength: 20
hp: 100
expect ++player<[strength hp]> .is.an 'array'
.and.have.length 2
.and.have.members [21 101]
done!
describe 'String', -> ``it``
.. 'String repetition',(done) ->
expect 'hi'*3 .is.a 'string'
.and.have.length 6
.and.have.equals <[hi hi hi]> * ''
done!
.. 'String subtraction/divison', (done) ->
expect 'say yeah' - /h/ .to.equals 'say yeah'.replace /h/ ''
expect 'say yeah' / \y .is.an 'array'
.and.have.length 3
.and.have.members [ \sa ' ' \eah ]
done!
describe "Object", -> ``it``
.. "should be object", (done) ->
I = name: 'DonaldIsFreak',like: 'programming'
expect I .to.be.a('object')
expect I .to.have.property 'name' 'DonaldIsFreak'
expect I .to.have.property 'like' 'programming'
# Another declare object way.
I =
name: 'DonaldIsFreak'
like: 'programming'
expect I .to.be.a 'object'
expect I .to.have.property 'name' 'DonaldIsFreak'
expect I .to.have.property 'like' 'programming'
# Dynamic keys
lucky_num = 47
obj =
lucky_num: "#lucky_num"
(I.name): 'my_name'
expect obj .to.be.a 'object'
expect obj .to.have.property 'lucky_num' '47'
expect obj .to.have.property 'DonaldIsFreak' 'my_name'
x = 1
y = 2
obj = {x, y,+debug,-live}
expect obj .to.be.a 'object'
expect obj .to.have.property 'x' 1
expect obj .to.have.property 'y' 2
expect obj .to.have.property 'debug' true
expect obj .to.have.property 'live' false
done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment