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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
-- Gemeinschaft 5 module: string functions
-- (c) AMOOMA GmbH 2012-2013
--
module(...,package.seeall)
function try(array, arguments)
if type(arguments) ~= 'string' or type(array) ~= 'table' then
return nil;
end
local result = array;
arguments:gsub('([^%.]+)', function(entry)
local success, result = pcall(function() result = (result[tonumber(entry) or entry]); end);
end);
return result;
end
function set(array, arguments, value)
local nop, arguments_count = arguments:gsub('%.', '');
local structure = array;
arguments:gsub('([^%.]+)', function(entry)
if arguments_count <= 0 then
structure[entry] = value;
elseif type(structure[entry]) == 'table' then
structure = structure[entry];
else
structure[entry] = {};
structure = structure[entry];
end
arguments_count = arguments_count - 1;
end);
end
-- to number
function to_n(value)
value = tostring(value):gsub('[^%d%.%+%-]', '');
return tonumber(value) or 0;
end
-- to integer
function to_i(value)
return math.floor(to_n(value));
end
-- to string
function to_s(value)
if value == nil then
return '';
end
return tostring(value);
end
-- to boolean
function to_b(value)
if type(value) == 'boolean' then
return value;
elseif tonumber(value) then
return (tonumber(value) > 0);
else
return (tostring(value) == 'yes' or tostring(value) == 'true');
end
end
-- to array
function to_a(line, separator)
line = line or '';
separator = separator or ';';
local result = {}
line:gsub('([^' .. separator .. ']+)', function(entry)
table.insert(result, entry);
end);
return result;
end
-- stripped to array
function strip_to_a(line, separator)
local result = {}
line:gsub('([^' .. separator .. ']+)', function(entry)
table.insert(result, (entry:gsub('^%s+', ''):gsub('%s+$', '')));
end);
return result;
end
-- downcase
function downcase(value)
if value == nil then
return '';
end
return tostring(value):lower();
end
-- remove special characters
function to_ascii(value)
return (to_s(value):gsub('[^A-Za-z0-9%-%_ %(%)]', ''));
end
-- to SQL
function to_sql(value)
if type(value) == 'boolean' then
return tostring(value):upper();
elseif type(value) == 'number' then
return tostring(value);
elseif type(value) == 'string' then
return '"' .. value:gsub('"', '\\"'):gsub("'", "\\'") .. '"';
else
return 'NULL';
end
end
-- to JSON
function to_json(value)
if type(value) == 'boolean' then
return tostring(value):lower();
elseif type(value) == 'number' then
return tostring(value);
elseif type(value) == 'string' then
return '"' .. value:gsub('"', '\\"'):gsub("'", "\\'") .. '"';
else
return 'null';
end
end
-- remove leading/trailing whitespace
function strip(value)
return (tostring(value):gsub('^%s+', ''):gsub('%s+$', ''));
end
-- split string
function partition(value, separator)
value = tostring(value);
separator = separator or ':'
return value:match('^(.-)' .. separator), value:match(separator .. '(.-)$');
end
-- check if value is empty string or nil
function blank(value)
return (value == nil or to_s(value) == '');
end
-- concatenate string to buffer
function append(buffer, value, separator, prefix, suffix)
separator = separator or '';
prefix = prefix or '';
suffix = suffix or '';
if buffer ~= '' then
buffer = buffer .. separator .. prefix .. value .. suffix;
else
buffer = prefix .. value .. suffix;
end
return buffer;
end
-- concatenate array values to string
function concat(array, separator, prefix, suffix)
local buffer = '';
for key, value in pairs(array) do
buffer = append(buffer, value, separator, prefix, suffix);
end
return buffer;
end
-- concatenate array keys to string
function concat_keys(array, separator, prefix, suffix)
local buffer = '';
for key, value in pairs(array) do
buffer = append(buffer, key, separator, prefix, suffix);
end
return buffer;
end
|