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( "tapered" )
local isabsolute = require( "isabsolute" )

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

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

t.done()

See also