Path.ext
func Path.ext(p: string): stringPath.ext returns the suffix of p from the last dot after its last separator, or "" when there is none.
Details
- The letters are kept as written, so
.TXTand.txtare different extensions. - The extension starts at the last dot after the last separator, so a name that begins with a dot, as
.env, is all extension, and a path that ends in a separator has none. Path.extworks on the text alone and never looks at the file system.
Basic examples
Get the extension of a file name, with its dot:
Path.ext("a/b.tar.gz") // ".gz"
Path.ext("main.ant") // ".ant"
Path.ext("a/b") // ""Scope
An empty path, and a name with no dot, give "":
Path.ext("") // ""
Path.ext("Makefile") // ""A dot in a directory name is not an extension:
Path.ext("v1.2/readme") // ""
Path.ext("v1.2/readme.txt") // ".txt"An absolute path works the same as a relative one:
Path.ext("/etc/nginx/nginx.conf") // ".conf"Letters outside ASCII are kept as they are:
Path.ext("notes/résumé.pdf") // ".pdf"
Path.ext("data.été") // ".été"Call it in a pipe:
"src/main.ant" | Path.ext() // ".ant"Applications
Keep the Ant sources of a list of files:
let files = ["main.ant", "README.md", "lib/util.ant", "Makefile"]
files.filter({ Path.ext(x) == ".ant" }) // ["main.ant", "lib/util.ant"]Match an extension whatever its case, by lowering it first:
func isImage(name: string): bool {
let ext = Path.ext(name).toLower()
ext == ".png" || ext == ".jpg" || ext == ".gif"
}
isImage("Photo.JPG") // true
isImage("photo.txt") // falseStrip the extension to get the name without it:
func stem(p: string): string {
Path.base(p).trimSuffix(Path.ext(p))
}
stem("docs/guide.md") // "guide"
stem("dist/app.tar.gz") // "app.tar"
stem("bin/ant") // "ant"Swap the extension of a path, keeping its directory:
func withExt(p: string, ext: string): string {
p.trimSuffix(Path.ext(p)) + ext
}
withExt("src/main.ant", ".js") // "src/main.js"
withExt("README", ".md") // "README.md"Group files by extension:
let files = ["a.ant", "b.md", "c.ant", "Makefile"]
files.groupBy(Path.ext) // Map{ ".ant": ["a.ant", "c.ant"], ".md": ["b.md"], "": ["Makefile"] }Properties and relations
The extension is "" or starts with a dot, and the last element ends with it:
for p in ["a/b.tar.gz", "a/b", "main.ant", "/etc/hosts"] {
let ext = Path.ext(p)
assert(ext.isEmpty() || ext.startsWith("."))
assert(Path.base(p).endsWith(ext))
}For a path that does not end in a separator, the extension is the extension of its last element:
let p = "photos/2024.06/beach.jpeg"
assert(Path.ext(p) == Path.ext(Path.base(p)))Possible issues
A double extension such as .tar.gz gives only its last part. Test the whole suffix with String.endsWith instead:
Path.ext("dist/app.tar.gz") // ".gz"
"dist/app.tar.gz".endsWith(".tar.gz") // trueA path that ends in a separator has no extension, even though its last element has one:
Path.ext("photos/album.d/") // ""
Path.ext(Path.base("photos/album.d/")) // ".d"A path is a plain string, and a call through . looks in String, so it does not find Path.ext. Use the namespace form or a pipe:
let name = "notes.md"
let ext = name.ext() // error: string has no field or function extSee also
Path.base, Path.dir, Path.join, Path.match, String.trimSuffix, String.endsWith, Path
