longextname

function longextname( path ) --> ext

Description

Returns the longest file name extension used by the path. The extension is the suffix beginning at the first dot in in the final slash-separated element of path.

Parameters

path

Path with file name.

Return Values

ext

Longest file name extension.

Code

--ZFUNC-longextname-v1
local function longextname( path ) --> ext
   local i = #path
   local doti = nil
   local c = string.sub( path, i, i )
   while i > 0 and c ~= "/" do
      i = i - 1
      c = string.sub( path, i, i )
      if c == "." then doti = i end
   end

   if doti then return string.sub( path, doti ) end

   return ""
end

return longextname

Examples

local t = require( "tapered" )
local longextname = require( "longextname" )

t.is( ".html", longextname( "index.html" ) )
t.is( ".coffee.md", longextname( "index.coffee.md" ) )

t.is( "..", longextname( "index.." ) )
t.is( "", longextname( "index" ) )

t.is( ".index", longextname( ".index" ) )
t.is( ".index", longextname( "/.index" ) )

t.is( "", longextname( "abc.def/index" ) )

t.done()