Skip to content

Commit 7a5be95

Browse files
committed
docs(base): fix and improve docs for self:with_suffix
Release-As: 2.1.0
1 parent 25d2123 commit 7a5be95

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lua/pathlib/base.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ function Path:with_basename(name)
298298
end
299299

300300
---Return the group name of the file GID. Same as `str(self) minus self:modify(":r")`.
301+
---
302+
--->>> Path("folder/foo.txt"):suffix()
303+
---"foo"
304+
--->>> Path("folder/no-extension"):suffix()
305+
---""
306+
--->>> Path("folder/.bashrc"):suffix()
307+
---".bashrc"
308+
--->>> Path("folder/archive.tar.gz"):suffix()
309+
---"archive.tar"
310+
---
301311
---@return string # extension of path including the dot (`.`): `.py`, `.lua` etc
302312
function Path:suffix()
303313
local s, counter = self:basename():gsub("^.*(%.[^.]+)$", "%1", 1)
@@ -306,25 +316,27 @@ end
306316

307317
---Return new object with new suffix.
308318
---
309-
--->>> Path("./folder/foo.txt"):with_suffix("png")
319+
--->>> Path("./folder/foo.txt"):with_suffix(".png")
310320
---Path("./folder/foo.png")
311321
---
322+
---@see PathlibPath.add_suffix as well
312323
---@param suffix string # New suffix
313324
function Path:with_suffix(suffix)
314325
local name = self:stem() .. suffix
315326
return self:with_basename(name)
316327
end
317328

318329
---Append given `suffix` to path, if suffix is not as same as given.
330+
---@see PathlibPath.with_suffix as well
319331
---
320-
--->>> Path("./folder/foo.tar"):add_suffix("gz")
332+
--->>> Path("./folder/foo.tar"):add_suffix(".gz")
321333
---Path("./folder/foo.tar.gz")
322334
---
323-
--->>> Path("./folder/foo.txt.bak"):add_suffix("bak")
335+
--->>> Path("./folder/foo.txt.bak"):add_suffix(".bak")
324336
---Path("./folder/foo.txt.bak") -- is already ".bak", so no change applied
325337
---
326338
---@param suffix string # Append this suffix to path, if suffix is not already equal.
327-
---@param force boolean|nil # If true, always append given suffix. Result will become `foo.txt.bak.bak` in above expample.
339+
---@param force boolean|nil # If true, always append given suffix. Result will be `foo.txt.bak.bak` in above expample.
328340
function Path:add_suffix(suffix, force)
329341
local basename = self:basename()
330342
if force or basename:sub(-suffix:len()) ~= suffix then

0 commit comments

Comments
 (0)