-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: added basic source file and doc.
- Loading branch information
Showing
5 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,3 +39,6 @@ luac.out | |
*.x86_64 | ||
*.hex | ||
|
||
# dev | ||
go | ||
t/servroot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
Oops, something went wrong.