ends_with
ends_with checks whether a string ends with a given suffix.
Overview
:ends_with returns true if the string ends with the given suffix, false otherwise.
:ends_with("index.html", ".html") /// true :ends_with("index.html", ".md") /// false
:ends_with("index.html", ".html") /// true :ends_with("index.html", ".md") /// false
Basic Usage
path | "content/index.md" if :ends_with(path, ".md") :say("markdown file") xx
path | "content/index.md" if :ends_with(path, ".md") :say("markdown file") xx
Multiple Suffixes
Pass an array to check against several suffixes at once. Returns true if the string ends with any of them.
filename | "photo.jpg" if :ends_with(filename, [".jpg", ".jpeg", ".png", ".webp"]) :say("image file") xx
filename | "photo.jpg" if :ends_with(filename, [".jpg", ".jpeg", ".png", ".webp"]) :say("image file") xx
This replaces chained or conditions:
/// before if :ends_with(filename, ".jpg") or :ends_with(filename, ".jpeg") or :ends_with(filename, ".png") /// after if :ends_with(filename, [".jpg", ".jpeg", ".png"])
/// before if :ends_with(filename, ".jpg") or :ends_with(filename, ".jpeg") or :ends_with(filename, ".png") /// after if :ends_with(filename, [".jpg", ".jpeg", ".png"])
In a Judge Dispatcher
:ends_with pairs naturally with judge when branching by file type or extension:
judge :ends_with(path, ".md"): process_markdown(path) :ends_with(path, [".jpg", ".png"]): process_image(path) :ends_with(path, ".yall"): process_config(path) else: process_generic(path) xx
judge :ends_with(path, ".md"): process_markdown(path) :ends_with(path, [".jpg", ".png"]): process_image(path) :ends_with(path, ".yall"): process_config(path) else: process_generic(path) xx
Signature
:ends_with(text, suffix) :ends_with(text, [suffix1, suffix2, ...])
:ends_with(text, suffix) :ends_with(text, [suffix1, suffix2, ...])
| Argument | Type | Description |
|---|---|---|
text |
string | The string to check. |
suffix |
string or array | Suffix or list of suffixes to match. |
Returns bool.