string: add title_case

This commit is contained in:
Josh Perry 2023-02-12 22:00:04 +00:00
parent 952a8f6bbc
commit aafdab56b5
2 changed files with 17 additions and 0 deletions

View File

@ -6,6 +6,7 @@ package.path = package.path .. ";../?.lua"
local assert = require("batteries.assert")
local tablex = require("batteries.tablex")
local identifier = require("batteries.identifier")
local stringx = require("batteries.stringx")
-- tablex {{{
@ -196,3 +197,10 @@ local function test_ulid()
assert(not ulid:match("[ILOU%l]"))
end
end
-- stringx
local function test_title_case()
local str = "the quick brown fox jumps over the lazy dog"
assert(stringx.title_case(str) == "The Quick Brown Fox Jumps Over The Lazy Dog")
end

View File

@ -288,4 +288,13 @@ function stringx.split_and_trim(s, delim)
return s
end
--titlizes a string
--"quick brown fox" becomes "Quick Brown Fox"
function stringx.title_case(s)
s = s:gsub("%s%l", string.upper)
s = s:gsub("^%l", string.upper)
return s
end
return stringx