shortfilename

function shortfilename( path ) --> name

Description

Returns the file name without the longest extension used by path. The name is the prefix befor the first dot in the final slash-separated element of path.

Parameters

path

Path with file name.

Return Values

name

File name without the longest extension.

Code

--ZFUNC-shortfilename-v1
local function shortfilename( path ) --> name
   --ZFUNC-basename-v1
   local function basename( path, ext )
      --ZFUNC-rmsuffix-v1
      local function rmsuffix( str, suffix )
         local suffixlen = string.len( suffix )
         local endsub = string.sub( str, -suffixlen )
         if endsub == suffix then
            local n = string.len( str ) - suffixlen
            return string.sub( str, 1, n )
         else
            return str
         end
      end

      local i = #path
      local c = string.sub( path, i, i )
      while i > 0 and c ~= "/" do
         i = i - 1
         c = string.sub( path, i, i )
      end

      local name = path
      if i ~= 0 then name = string.sub( path, i+1 ) end

      if ext then
         return rmsuffix( name, ext )
      else
         return name
      end
   end

   --ZFUNC-longextname-v1
   local function longextname( path )
      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 basename( path, longextname( path ) )
end

return shortfilename

Examples

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

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

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

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

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

t.done()