-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.lua
59 lines (57 loc) · 1.3 KB
/
Utils.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local function toJSON(e)
local t = type (e);
if t == "boolean" then
if e then
return "true"
else
return "false"
end
elseif t == "string" then
return '"' .. e .. '"'; --[[escaping e]]
elseif t == "number" then
return '' .. e;
elseif t == "table" then
local justNum = true;
local expected = 1;
table.foreach(e, function(k,v)
if k==expected then
expected = expected+1;
else
justNum = false;
end
end)
if justNum then
local text = '[';
local sep = '';
table.foreach(e, function(k,v)
text = text .. sep .. toJSON(v);
sep = ',';
end)
return text .. ']';
else
local text = '{';
local sep = '';
table.foreach(e, function(k,v)
text = text .. sep .. '"'.. k .. '":' .. toJSON(v);--[[escaping k]]
sep = ',';
end)
return text .. '}';
end
end
end
local function slice(arr,from)
local part = {};
table.foreach(arr,function (k,v)
if from <= k then
table.insert(part,v);
end
end)
return part;
end
local function map(foo,arr)
local mapped = {};
table.foreach(arr, function(k,v)
table.insert(mapped,foo(v));
end)
return mapped;
end