isabsolute

function isabsolute( path ) --> res

Description

Returns true if the path is absolute, otherwise returns false if the path is relative.

Parameters

path

Path that should be checked.

Return Values

res

true if, and only if, the path is absolute, otherwise false.

Code

--ZFUNC-isabsolute-v1
local function isabsolute( path ) --> res
   --ZFUNC-rootprefix-v1
   local function rootprefix( path )
      local remote = path:match[[^//%w+/]] or path:match[[^\\%w+\]]
      if remote then return remote end

      local unix = path:sub( 1, 1 )
      if unix == "/" then return unix end

      local win = path:match[=[^[a-zA-Z]:[\/]]=]
      if win then return win end

      return ""
   end

   return rootprefix( path ) ~= ""
end

return isabsolute

Examples

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

--nodejs test-path.js examples
t( isabsolute( "//server/file" ), true )
t( isabsolute[[\\server\file]], true )
t( isabsolute( "C:/Users/" ), true )
t( isabsolute[[C:\Users\]], true )
t( isabsolute( "/home/foo" ), true )
t( isabsolute( "/home/foo/..." ), true )

t( isabsolute( "C:cwd/another" ), false )
t( isabsolute[[C:cwd\another]], false )
t( isabsolute( "directory/directory" ), false )
t( isabsolute[[directory\directory]], false )
t( isabsolute( "bar/" ), false )
t( isabsolute( "./baz" ), false )

t()

See also