endswithany

function endswithany( str, tab ) --> res

Description

Checks if str ends with one of the values in tab.

Parameters

str

The string that should be checked.

tab

An array table with strings.

Return Values

res

Is true if the string ends with one of the values in tab, otherwise false.

Code

--ZFUNC-endswithany-v1
local function endswithany( str, tab ) --> res
   for _, v in ipairs( tab ) do
      local sub = string.sub( str, -string.len( v ) )
      if sub == v then
         return true
      end
   end

   return false
end

return endswithany

Examples

local t = require( "taptest" )
local endswithany = require( "endswithany" )

filename = "note.adoc"
t( endswithany( filename, { ".adoc", ".lua" } ), true )
t( endswithany( filename, { ".txt", ".lua" } ), false )

t()