Skip to content

Commit

Permalink
init: added basic source file and doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis committed May 8, 2019
1 parent c50b9a9 commit d38086b
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ luac.out
*.x86_64
*.hex

# dev
go
t/servroot
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# lua-typeof
Check the data type for Lua variable

## Install

```shell
luarocks install typeof
```
119 changes: 119 additions & 0 deletions lib/typeof.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
local INFINITE_POS = math.huge
local INFINITE_NEG = -INFINITE_POS
local type = type
local floor = math.floor
local rawequal = rawequal

local function typeof(cmp, arg)
return cmp == type(arg)
end

local function typeof_nil(...)
return typeof('nil', ...)
end

local function typeof_bool(...)
return typeof('boolean', ...)
end

local function typeof_str(...)
return typeof('string', ...)
end

local function typeof_num(...)
return typeof('number', ...)
end

local function typeof_fun(...)
return typeof('function', ...)
end

local function typeof_table(...)
return typeof('table', ...)
end

local function typeof_thread(...)
return typeof('thread', ...)
end

local function typeof_userdata(...)
return typeof('userdata', ...)
end

local function typeof_finite(arg)
return type(arg) == 'number' and (arg < INFINITE_POS and arg > INFINITE_NEG)
end

local function typeof_unsigned(arg)
return type(arg) == 'number' and (arg < INFINITE_POS and arg >= 0)
end

local function typeof_int(arg)
return typeof_finite(arg) and rawequal(floor(arg), arg)
end

local function typeof_int8(arg)
return typeof_int(arg) and arg >= -128 and arg <= 127
end

local function typeof_int16(arg)
return typeof_int(arg) and arg >= -32768 and arg <= 32767
end

local function typeof_int32(arg)
return typeof_int(arg) and arg >= -2147483648 and arg <= 2147483647
end

local function typeof_uint(arg)
return typeof_unsigned(arg) and rawequal(floor(arg), arg)
end

local function typeof_uint8(arg)
return typeof_uint(arg) and arg <= 255
end

local function typeof_uint16(arg)
return typeof_uint(arg) and arg <= 65535
end

local function typeof_uint32(arg)
return typeof_uint(arg) and arg <= 4294967295
end

local function typeof_nan(arg)
return arg ~= arg
end

local function typeof_non(arg)
return arg == nil or arg == false or arg == 0 or arg == '' or arg ~= arg
end


local _M = {
version = 0.1,
['nil'] = typeof_nil,
['boolean'] = typeof_bool,
['string'] = typeof_str,
['number'] = typeof_num,
['function'] = typeof_fun,
['table'] = typeof_table,
['thread'] = typeof_thread,
['userdata'] = typeof_userdata,
['finite'] = typeof_finite,
['unsigned'] = typeof_unsigned,
['int'] = typeof_int,
['int8'] = typeof_int8,
['int16'] = typeof_int16,
['int32'] = typeof_int32,
['uint'] = typeof_uint,
['uint8'] = typeof_uint8,
['uint16'] = typeof_uint16,
['uint32'] = typeof_uint32,
['nan'] = typeof_nan,
['non'] = typeof_non,
-- alias
['Nil'] = typeof_nil,
['Function'] = typeof_fun
}

return _M
21 changes: 21 additions & 0 deletions lua-typeof-0.1-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package = "lua-typeof"
version = "0.1-0"
source = {
url = "git://github.com/iresty/lua-typeof",
tag = "v0.1"
}
description = {
summary = "Nonblocking Lua etcd driver library for OpenResty",
homepage = "https://github.com/iresty/lua-typeof",
license = "Apache License 2.0",
maintainer = "Yuansheng Wang <[email protected]>"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
["typeof"] = "lib/typeof.lua",
}
}
Loading

0 comments on commit d38086b

Please sign in to comment.