Skip to content

Commit 69ed1f5

Browse files
committed
initial commit
1 parent a83c12c commit 69ed1f5

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ luac.out
3939
*.x86_64
4040
*.hex
4141

42+
.DS_Store

rockspecs/switch-1.0-0.rockspec

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package = 'switch'
2+
version = '1.0-0'
3+
source = {
4+
url = 'https://github.com/ryanplusplus/switch.lua/archive/v1.0-0.tar.gz',
5+
dir = 'switch.lua-1.0-0/src'
6+
}
7+
description = {
8+
summary = 'Lua implementation of a switch statement.',
9+
homepage = 'https://github.com/ryanplusplus/switch.lua/',
10+
license = 'MIT <http://opensource.org/licenses/MIT>'
11+
}
12+
dependencies = {
13+
'lua >= 5.1'
14+
}
15+
build = {
16+
type = 'builtin',
17+
modules = {
18+
['switch'] = 'switch.lua'
19+
}
20+
}

rockspecs/switch-git-0.rockspec

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package = 'switch'
2+
version = 'git-0'
3+
source = {
4+
url = 'git://github.com/ryanplusplus/switch.lua.git',
5+
dir = 'src'
6+
}
7+
description = {
8+
summary = 'Lua implementation of a switch statement.',
9+
homepage = 'https://github.com/ryanplusplus/switch.lua/',
10+
license = 'MIT <http://opensource.org/licenses/MIT>'
11+
}
12+
dependencies = {
13+
'lua >= 5.1'
14+
}
15+
build = {
16+
type = 'builtin',
17+
modules = {
18+
['switch'] = 'switch.lua'
19+
}
20+
}

spec/switch_spec.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
describe('switch', function()
2+
local switch = require 'switch'
3+
4+
it('should call the corresponding function with the switched value', function()
5+
local f = spy.new(load'')
6+
7+
switch('hello', {
8+
goodbye = error,
9+
hello = f
10+
})
11+
12+
assert.spy(f).was_called_with('hello')
13+
end)
14+
15+
it('should not call any functions if no value matches', function()
16+
switch('hello', {
17+
goodbye = error
18+
})
19+
end)
20+
21+
it('should allow a default case to be provided', function()
22+
local f = spy.new(load'')
23+
24+
switch('lua', {
25+
goodbye = error,
26+
hello = error,
27+
[switch.default] = f
28+
})
29+
30+
assert.spy(f).was_called_with('lua')
31+
end)
32+
end)

src/switch.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local default_case = {}
2+
3+
local function switch(value, cases)
4+
local default = cases[default_case] or load''
5+
return setmetatable(cases, {
6+
__index = function()
7+
return default
8+
end
9+
})[value](value)
10+
end
11+
12+
return setmetatable({
13+
default = default_case
14+
}, {
15+
__call = function(_, ...) return switch(...) end
16+
})

0 commit comments

Comments
 (0)