Skip to content

Commit 0306608

Browse files
committed
is_a
1 parent 329d657 commit 0306608

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

moonscript/util.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
local concat = table.concat
22
local unpack = unpack or table.unpack
3+
local type = type
34
local moon = {
45
is_object = function(value)
56
return type(value) == "table" and value.__class
67
end,
8+
is_a = function(thing, t)
9+
if not (type(thing) == "table") then
10+
return false
11+
end
12+
local cls = thing.__class
13+
while cls do
14+
if cls == t then
15+
return true
16+
end
17+
cls = cls.__parent
18+
end
19+
return false
20+
end,
721
type = function(value)
822
local base_type = type(value)
923
if base_type == "table" then

moonscript/util.moon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22
import concat from table
33

44
unpack = unpack or table.unpack
5+
type = type
56

67
moon =
78
is_object: (value) -> -- is a moonscript object
89
type(value) == "table" and value.__class
10+
11+
is_a: (thing, t) ->
12+
return false unless type(thing) == "table"
13+
cls = thing.__class
14+
while cls
15+
if cls == t
16+
return true
17+
cls = cls.__parent
18+
19+
false
20+
921
type: (value) -> -- the moonscript object class
1022
base_type = type value
1123
if base_type == "table"

0 commit comments

Comments
 (0)