aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/F.lua
blob: 09467eb8c6045fc88584129c8bb26f3bf12a5f1a (plain) (blame)
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
local F = {}

--- Returns {a} if it is not nil, otherwise returns {b}.
---
---@param a
---@param b
function F.if_nil(a, b)
  if a == nil then return b end
  return a
end

-- Use in combination with pcall
function F.ok_or_nil(status, ...)
  if not status then return end
  return ...
end

-- Nil pcall.
function F.npcall(fn, ...)
  return F.ok_or_nil(pcall(fn, ...))
end

--- Wrap a function to return nil if it fails, otherwise the value
function F.nil_wrap(fn)
  return function(...)
    return F.npcall(fn, ...)
  end
end


return F