robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local F ={} 2 3-- Failure returns no values, so every result becomes optional. 4--- @nodoc 5--- @alias vim.F.OptionalReturns<T> { [K in keyof T]: T[K]|nil } 6 7--- Returns the first argument which is not nil. 8--- 9--- If all arguments are nil, returns nil. 10--- 11--- Examples: 12--- 13--- ```lua 14--- local a = nil 15--- local b = nil 16--- local c = 42 17--- local d = true 18--- assert(vim.F.if_nil(a, b, c, d) == 42) 19--- ``` 20--- 21--- @deprecated 22--- @generic T 23--- @param ... T 24--- @return T 25function F . if_nil ( ... ) 26vim . deprecate ( 'vim.F.if_nil' , 'vim.nonnil' , '0.15' ) 27return vim . nonnil ( ... ) 28end 29 30-- Use in combination with pcall 31--- @deprecated 32--- @generic T... 33--- @param status boolean 34--- @param ... T... 35--- @return vim.F.OptionalReturns<T>... 36function F . ok_or_nil ( status , ... ) 37vim . deprecate ( 'vim.F.ok_or_nil' , 'actual error handling' , '0.15' ) 38if not status then 39return 40end 41return ... 42end 43 44-- Nil pcall. 45--- @deprecated 46--- @generic T 47--- @param fn fun(...):T 48--- @param ... T? 49--- @return T 50function F . npcall ( fn , ... ) 51vim . deprecate ( 'vim.F.npcall' , 'vim.npcall' , '0.15' ) 52return vim . npcall ( fn , ... ) 53end 54 55--- Wrap a function to return nil if it fails, otherwise the value 56--- @deprecated 57--- @generic A..., R... 58--- @param fn fun(...: A...): R... 59--- @return fun(...: A...): vim.F.OptionalReturns<R>... 60function F . nil_wrap ( fn ) 61vim . deprecate ( 'vim.F.nil_wrap' , 'vim.npcall' , '0.15' ) 62return function ( ... ) 63return vim . npcall ( fn , ... ) 64end 65end 66 67-- TODO: deprecate `F.pack_len` and `F.unpack_len` 68 69--- like {...} except preserve the length explicitly 70--- @param ... any 71--- @return { [integer]: any, n: integer } 72function F . pack_len ( ... ) 73return { n =select ( '#' , ... ), ... } 74end 75 76--- like unpack() but use the length set by F.pack_len if present 77--- @param t { [integer]: any, n?: integer } 78--- @return any... 79function F . unpack_len ( t ) 80return unpack ( t , 1 , t . n or # t ) 81end 82 83return F