aboutsummaryrefslogtreecommitdiff
path: root/lua/simple-denote/util.lua
blob: 7c014497c81d79c973f77475ff954244af7a6faf (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
local M = {}


function M.splitspace(str)
  local chunks = {}
  for substring in str:gmatch("%S+") do
    table.insert(chunks, #chunks, substring)
  end
  return chunks
end

function M.splitstring(str, delim)
  local items = {}
  local fulldelim = nil
  if delim == "." or delim == "-" then
    fulldelim = "%" .. delim
  else
    fulldelim = delim
  end
  str = str .. delim
  for w in str:gmatch("(.-)" .. fulldelim) do
    items[#items + 1] = w
  end
  return items
end

--- Trim whitespace on either end of string
function M.trim(str)
   local from = str:match"^%s*()"
   return from > #str and "" or str:match(".*%S", from)
end

--- Remove special chars, make lowercase, spaces to dashes
function M.plain_format(str)
  str = str:lower()
  str = str:gsub("[^%w%s]","")
  str = M.trim(str)
  str = str:gsub("%s+","-")
  return str
end

return M