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) ### lume.serialize(x)
Serializes the argument `x` into a string which can be loaded again using Serializes the argument `x` into a string which can be loaded again using
`lume.deserialize()`. Only booleans, numbers, tables and strings can be `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. serialized as unique tables.
```lua ```lua
lume.serialize({a = "test", b = {1, 2, 3}, false}) 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. by a space.
```lua ```lua
-- Assuming the file is called "example.lua" and the next line is 12: -- 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) ### lume.dostring(str)

View File

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

View File

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