Lodash - paths

About

These functions bring the most important path utilities from Node.jsopen in new window to 4D.

API

.basename()

.basename(path : Text { ; pathType : Integer { ; includeExtension : Boolean }}) : Text

A more convenient form of Path to objectopen in new window that returns the filename portion of a path.

If pathType is not passed, the path type is determined by calling .determinePathType(path). If passed, pathType should be either:

  • Path is system (the default)
  • Path is POSIX
  • A negative number to determine the path heuristically

If includeExtension is not passed, it defaults to true. If includeExtension is passed and is false, the filename extension (including the dot) is omitted.

Examples

$path:=_.basename("/foo/bar.html")  // => "bar.html"
$path:=_.basename("/foo/bar/")  // => "bar", no trailing separator
$path:=_.basename("/")  // => ""
$path:=_.basename("foo:bar"; Path is system)
// => "bar" on macOS, "foo:bar" on Windows
$path:=_.basename("c:\\foo\\bar"; Path is system)
// => "c:\\foo\\bar" on macOS, "c:\\foo" on Windows
$path:=_.basename("foo/bar/baz.html"; Path is POSIX; False) // => "baz"

.determinePathType()

.determinePathType(path : Text) : Integer

Determines if a path is a Posix or system path:

ConditionResult
Path contains "/"Path is POSIX
Path contains ":" (macOS)Path is system
Path contains "\" (Windows)Path is system
Any other pathPath is POSIX

.dirname()

.dirname(path : Text { ; pathType : Integer }) : Text

A more convenient form of Path to objectopen in new window that returns the parent directory portion of a path.

If pathType is not passed, the path type is determined by calling .determinePathType(path). If passed, pathType should be either:

Examples

$path:=_.dirname("/foo/bar.html")  // => "/foo/"
$path:=_.dirname("/")  // => ""
$path:=_.dirname("/foo")  // => "/"
$path:=_.dirname("/foo/bar/")  // => "/foo/", ignore trailing separators
$path:=_.dirname("foo:bar"; Path is system)
// => "foo" on macOS, "foo:bar" on Windows
$path:=_.dirname("c:\\foo\\bar"; Path is system)
// => "c:\\foo\\bar" on macOS, "c:\\foo" on Windows

.expandUser()

.expandUser(path : Text { ; pathType : Integer }) : Text

Expands ~ or ~<user> at the beginning of path into the relevant path of the current user or the given <user> on the current machine.

If pathType is not passed, the path type is determined by calling .determinePathType(path). If passed, pathType should be either:

Examples

Assuming the current username is "laurent":

$path:=_.expandUser("~/foo/bar")
// => "/Users/laurent/foo/bar"
$path:=_.expandUser("~:foo:bar")
// => "HD:Users:laurent:foo:bar"
$path:=_.expandUser("~\\foo\\bar")
// => "C:\\Users\\laurent\\foo\\bar"

.extname()

.extname(path : Text { ; pathType : Integer }) : Text

Returns the extension of path, from the last occurrence of the . (period) character to the end of the last portion of the path. If there is no . in the last portion of the path, or if there are no . characters other than the first character of the basename of path (see .basename(), an empty string is returned.

If pathType is not passed, the path type is determined by calling .determinePathType(path). If passed, pathType should be either Path is system (the default) or Path is POSIX.

Examples

_.extname("index.html")       // ".html"
_.extname("index.coffee.md")  // ".md"
_.extname("index.")           // "."
_.extname("index")            // ""
_.extname(".index")           // ""
_.extname(".index.md")        // ".md"

.isAbsolute()

.isAbsolute(path : Text { ; pathType : Integer }) : Boolean

Returns true if path is an absolute path.

If pathType is not passed, the path type is determined by calling .determinePathType(path). If passed, pathType should be either Path is system (the default) or Path is POSIX.


.joinPaths()

.joinPaths(pathSegment : Text { ; …pathSegmentN : Text }) : Text
.joinPaths(pathType : Integer; pathSegment : Text { ; …pathSegmentN : Text }) : Text

Joins together one or more path segments (relative or absolute).

Any path segment that is absolute replaces all previous segments. Otherwise a path separator for the current path type is inserted between segments if necessary. Note that trailing path separators are removed from all but the last segment, and that relative system path segments on macOS must begin with ":".

If using the first form, the path type for all of the segments will be inferred from the first segment using .determinePathType().

If using the second form, pathType should be either Path is system or Path is POSIX.

Examples

$path:=_.joinPaths("foo"; "bar"; "baz") // => "foo/bar/baz"
$path:=_.joinPaths("foo"; "bar/"; "baz") // => "foo/bar/baz"
$path:=_.joinPaths("foo"; "bar"; "/baz") // => "/baz"

.nativePath()

.nativePath(file : 4D.File) : Text

Similar to File.platformPathopen in new window, but returns a POSIX path (instead of HFS) on macOS.

Last Updated:
Contributors: Aparajita