From d4e5bc7f8ef14db7f8f8bdb7bef3fa4255d59336 Mon Sep 17 00:00:00 2001 From: RiskoZoSlovenska Date: Fri, 23 Jun 2023 23:09:14 -0400 Subject: [PATCH] Add `string.truthy` --- libs/string.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libs/string.lua b/libs/string.lua index fea2c95..e13171b 100644 --- a/libs/string.lua +++ b/libs/string.lua @@ -36,6 +36,14 @@ function ext_string.endswith(str, pattern, plain) end end +---Returns false if `str` is falsy or an empty string; otherwise, returns `str` +---unchanged. +---@param str string +---@return boolean|string +function ext_string.truthy(str) + return (str and str ~= "") and str or false +end + local pattern_special = [[^$()%.[]*+-?]] local pattern_match = '[' .. pattern_special:gsub('.', '%%%1') .. ']'