summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/common/str.lua
blob: ca6dcd94353a902469cebbb40efb59793e396c10 (plain)
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
-- Gemeinschaft 5 module: string functions
-- (c) AMOOMA GmbH 2012
-- 

module(...,package.seeall)

function try(array, arguments)
  local argument = arguments:match('^(.-)%.') or arguments;
  local remaining_arguments = arguments:match('%.(.-)$');
  argument = tonumber(argument) or argument;
  
  if argument and type(array) == 'table' then
    if remaining_arguments then
      if type(array[argument]) == 'table' then
        return try(array[argument], remaining_arguments);
      else
        return nil;
      end
    else
      return array[argument];
    end
  end

  return nil;
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