Skip to content

Commit

Permalink
[utils] Moved split to utils #19
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 27, 2016
1 parent bae3bb6 commit 035502c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
15 changes: 15 additions & 0 deletions openwisp-config/files/lib/openwisp/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ function starts_with_dot(str)
return false
end

function split(input, sep)
if input == '' or input == nil then
return {}
end
if sep == nil then
sep = '%s'
end
local t={}; i=1
for str in string.gmatch(input, '([^' .. sep .. ']+)') do
t[i] = str
i = i + 1
end
return t
end

function add_values_to_set(set, values)
for i, el in pairs(values) do
set[el] = true
Expand Down
15 changes: 0 additions & 15 deletions openwisp-config/files/sbin/openwisp-store-unmanaged.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ local standard_path = standard_prefix .. 'config/'
local unmanaged_path = unmanaged_prefix .. 'unmanaged/'
local uci_tmp_path = '/tmp/openwisp/.uci'

function split(input, sep)
if input == '' or input == nil then
return {}
end
if sep == nil then
sep = '%s'
end
local t={}; i=1
for str in string.gmatch(input, '([^' .. sep .. ']+)') do
t[i] = str
i = i + 1
end
return t
end

function empty_file(path)
local file = io.open(path, 'w')
file:write('')
Expand Down
7 changes: 7 additions & 0 deletions openwisp-config/tests/test_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,11 @@ function TestUtils.test_set_to_file()
luaunit.assertEquals(read.line3, nil)
end

function TestUtils.test_split()
t = {'a', 'b', 'c'}
luaunit.assertEquals(split('a b c'), t)
luaunit.assertEquals(split('a,b,c', ','), t)
luaunit.assertEquals(split('a/b/c', '/'), t)
end

os.exit(luaunit.LuaUnit.run())

0 comments on commit 035502c

Please sign in to comment.