15 Commits

Author SHA1 Message Date
rxi
a20a39c8ee Version 2.2.3 2015-11-28 10:40:50 +00:00
rxi
faa5d8252f Removed some unused iterator vars 2015-10-21 19:10:53 +01:00
rxi
bf32432dac Merge pull request #8 from technomancy/luacheck
Pacify Luacheck.
2015-10-21 19:05:56 +01:00
Phil Hagelberg
b861303333 Pacify Luacheck.
Mostly just renaming unused args.
2015-10-19 07:21:47 +07:00
rxi
27278fb887 Version 2.2.2 2015-09-28 21:45:14 +01:00
rxi
8627638db0 Added support for NaN, inf and -inf to lume.serialize; updated tests 2015-09-23 19:32:25 +01:00
rxi
1559803c70 Updated README for lume.serialize changes 2015-09-04 18:59:59 +01:00
rxi
258e523219 Version 2.2.1 2015-08-30 19:47:53 +01:00
rxi
16848d83a5 Fixed number printing in lume.trace for Lua5.3 2015-08-15 11:37:32 +01:00
rxi
ca338b8833 Removed .gitignore file 2015-08-15 11:32:37 +01:00
rxi
ca36473904 Renaming of internal serialize func map 2015-08-15 11:31:50 +01:00
rxi
717745fe79 Updated README example for lume.trace() 2015-08-14 20:56:41 +01:00
rxi
688de3368e Lots of tweaks to lume.serialize(); circular reference detection 2015-08-14 20:50:42 +01:00
rxi
7412706277 Moved some stuff around in "test" directory 2015-08-14 20:32:00 +01:00
rxi
78144dbdb8 Changed lume.trace() to output in a nicer format, updated test 2015-08-14 20:29:29 +01:00
5 changed files with 61 additions and 40 deletions

3
.gitignore vendored
View File

@@ -1,3 +0,0 @@
__*
*.tmp
*.swp

View File

@@ -329,7 +329,7 @@ f(10, 5) -- Returns 25
### lume.serialize(x)
Serializes the argument `x` into a string which can be loaded again using
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
serialized. Circular references are not handled; all nested tables are
serialized. Circular references will result in an error; all nested tables are
serialized as unique tables.
```lua
lume.serialize({a = "test", b = {1, 2, 3}, false})
@@ -383,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
by a space.
```lua
-- Assuming the file is called "example.lua" and the next line is 12:
lume.trace("hello", 1234) -- Prints "[example.lua:12] hello 1234"
lume.trace("hello", 1234) -- Prints "example.lua:12: hello 1234"
```
### lume.dostring(str)

View File

@@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details.
--
local lume = { _version = "2.2.0" }
local lume = { _version = "2.2.3" }
local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack
@@ -15,11 +15,9 @@ local tostring, tonumber = tostring, tonumber
local math_floor = math.floor
local math_ceil = math.ceil
local math_random = math.random
local math_cos = math.cos
local math_atan2 = math.atan2 or math.atan
local math_sqrt = math.sqrt
local math_abs = math.abs
local math_pi = math.pi
local noop = function()
end
@@ -131,7 +129,7 @@ end
function lume.weightedchoice(t)
local sum = 0
for k, v in pairs(t) do
for _, v in pairs(t) do
assert(v >= 0, "weight value less than zero")
sum = sum + v
end
@@ -172,7 +170,7 @@ end
function lume.clear(t)
local iter = getiter(t)
for k, v in iter(t) do
for k in iter(t) do
t[k] = nil
end
return t
@@ -250,7 +248,7 @@ end
function lume.all(t, fn)
fn = iteratee(fn)
local iter = getiter(t)
for k, v in iter(t) do
for _, v in iter(t) do
if not fn(v) then return false end
end
return true
@@ -260,7 +258,7 @@ end
function lume.any(t, fn)
fn = iteratee(fn)
local iter = getiter(t)
for k, v in iter(t) do
for _, v in iter(t) do
if fn(v) then return true end
end
return false
@@ -271,7 +269,7 @@ function lume.reduce(t, fn, first)
local acc = first
local started = first and true or false
local iter = getiter(t)
for k, v in iter(t) do
for _, v in iter(t) do
if started then
acc = fn(acc, v)
else
@@ -286,7 +284,7 @@ end
function lume.set(t)
local rtn = {}
for k, v in pairs(lume.invert(t)) do
for k in pairs(lume.invert(t)) do
rtn[#rtn + 1] = k
end
return rtn
@@ -302,7 +300,7 @@ function lume.filter(t, fn, retainkeys)
if fn(v) then rtn[k] = v end
end
else
for k, v in iter(t) do
for _, v in iter(t) do
if fn(v) then rtn[#rtn + 1] = v end
end
end
@@ -319,7 +317,7 @@ function lume.reject(t, fn, retainkeys)
if not fn(v) then rtn[k] = v end
end
else
for k, v in iter(t) do
for _, v in iter(t) do
if not fn(v) then rtn[#rtn + 1] = v end
end
end
@@ -346,7 +344,7 @@ function lume.concat(...)
local t = select(i, ...)
if t ~= nil then
local iter = getiter(t)
for k, v in iter(t) do
for _, v in iter(t) do
rtn[#rtn + 1] = v
end
end
@@ -379,14 +377,14 @@ function lume.count(t, fn)
local iter = getiter(t)
if fn then
fn = iteratee(fn)
for k, v in iter(t) do
for _, v in iter(t) do
if fn(v) then count = count + 1 end
end
else
if isarray(t) then
return #t
end
for k in iter(t) do count = count + 1 end
for _ in iter(t) do count = count + 1 end
end
return count
end
@@ -435,7 +433,7 @@ end
function lume.keys(t)
local rtn = {}
local iter = getiter(t)
for k, v in iter(t) do rtn[#rtn + 1] = k end
for k in iter(t) do rtn[#rtn + 1] = k end
return rtn
end
@@ -458,12 +456,12 @@ end
function lume.once(fn, ...)
local fn = lume.fn(fn, ...)
local f = lume.fn(fn, ...)
local done = false
return function(...)
if done then return end
done = true
return fn(...)
return f(...)
end
end
@@ -536,19 +534,41 @@ function lume.lambda(str)
end
function lume.serialize(x)
local f = { string = function(v) return string.format("%q", v) end,
number = tostring, boolean = tostring }
f.table = function(t)
local serialize
local serialize_map = {
[ "boolean" ] = tostring,
[ "nil" ] = tostring,
[ "string" ] = function(v) return string.format("%q", v) end,
[ "number" ] = function(v)
if v ~= v then return "0/0" -- nan
elseif v == 1 / 0 then return "1/0" -- inf
elseif v == -1 / 0 then return "-1/0" end -- -inf
return tostring(v)
end,
[ "table" ] = function(t, stk)
stk = stk or {}
if stk[t] then error("circular reference") end
local rtn = {}
stk[t] = true
for k, v in pairs(t) do
rtn[#rtn + 1] = "[" .. f[type(k)](k) .. "]=" .. f[type(v)](v) .. ","
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
end
return "{" .. table.concat(rtn) .. "}"
stk[t] = nil
return "{" .. table.concat(rtn, ",") .. "}"
end
local err = function(t,k) error("unsupported serialize type: " .. k) end
setmetatable(f, { __index = err })
return f[type(x)](x)
}
setmetatable(serialize_map, {
__index = function(_, k) error("unsupported serialize type: " .. k) end
})
serialize = function(x, stk)
return serialize_map[type(x)](x, stk)
end
function lume.serialize(x)
return serialize(x)
end
@@ -579,19 +599,19 @@ function lume.wordwrap(str, limit)
limit = limit or 72
local check
if type(limit) == "number" then
check = function(str) return #str >= limit end
check = function(s) return #s >= limit end
else
check = limit
end
local rtn = {}
local line = ""
for word, spaces in str:gmatch("(%S+)(%s*)") do
local str = line .. word
if check(str) then
local s = line .. word
if check(s) then
table.insert(rtn, line .. "\n")
line = word
else
line = str
line = s
end
for c in spaces:gmatch(".") do
if c == "\n" then
@@ -618,7 +638,7 @@ end
function lume.trace(...)
local info = debug.getinfo(2, "Sl")
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
local t = { info.short_src .. ":" .. info.currentline .. ":" }
for i = 1, select("#", ...) do
local x = select(i, ...)
if type(x) == "number" then
@@ -659,7 +679,7 @@ function lume.hotswap(modname)
end
local err = nil
local function onerror(e)
for k, v in pairs(_G) do _G[k] = oldglobal[k] end
for k in pairs(_G) do _G[k] = oldglobal[k] end
err = lume.trim(e)
end
local ok, oldmod = pcall(require, modname)
@@ -738,7 +758,7 @@ function lume.chain(value)
end
setmetatable(lume, {
__call = function(t, ...)
__call = function(_, ...)
return lume.chain(...)
end
})

View File

@@ -1,4 +1,4 @@
local tester = require "tester"
local tester = require "util.tester"
package.path = "../?.lua;" .. package.path
@@ -484,6 +484,10 @@ tests["lume.serialize, lume.deserialize"] = function()
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
local s = lume.serialize(t)
testeq( lume.deserialize(s), t )
testeq( lume.deserialize(lume.serialize(math.huge)), math.huge )
testeq( lume.deserialize(lume.serialize(-math.huge)), -math.huge )
local x = lume.deserialize(lume.serialize(0 / 0)) -- nan
testeq( x ~= x, true )
end
-- lume.split
@@ -535,7 +539,7 @@ tests["lume.trace"] = function()
local oldprint = print
local file, line, msg
print = function(x)
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
file, line, msg = x:match("(.-):(.-): (.*)")
end
lume.trace("Hi world", 123.456, 1, nil)
print = oldprint