------------------------------------------------------------------------------- -- Utilities shared by all modules. Categorize better if > 5 public functions ------------------------------------------------------------------------------- local Util = {} ------------------------------------------------------------------------------- -- Internal Functions ------------------------------------------------------------------------------- local fs = vim.loop local function create_directory(directory) local stat = fs.fs_stat(directory) if stat then if stat.type == 'directory' then return else error('Expected directory but found file at: ' .. directory) end else local parent = directory:match('(.+)/[^/]*$') if parent then create_directory(parent) end local success, error = fs.fs_mkdir(directory, 493) if not success then error('Could not directory at: ' .. directory .. '. ' .. error) end end end ------------------------------------------------------------------------------- -- Public Interface ------------------------------------------------------------------------------- -- File Utils function Util.ensure_directory_exists(path) local full_path = vim.fn.expand(path) create_directory(path) end -- Path Utils function Util.join(...) local separator = '/' local paths = {...} return table.concat(paths, separator):gsub(separator..'+', separator) end function Util.directory_name(file_path) local pattern = '(.+)/[^/]+$' return file_path:match(pattern) end -- Date Utils function Util.is_valid_date(date_string) local pattern = '(%d+)-(%d+)-(%d+)' local year, month, day = date_string:match(pattern) if year and month and day then return true end return false end function Util.is_before_today(date_string) local pattern = '(%d+)-(%d+)-(%d+)' local year, month, day = date_string:match(pattern) if year and month and day then local today = os.date('*t') local today_date = os.time({year = today.year, month = today.month, day = today.day}) local date = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) return date < today_date else return false end end return Util