Merge pull request #63 from josh-perry/title_case

string: add title_case
This commit is contained in:
Max Cahill 2023-02-14 14:14:08 +11:00 committed by GitHub
commit 2762d4ada2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 assert = require("batteries.assert")
local tablex = require("batteries.tablex") local tablex = require("batteries.tablex")
local identifier = require("batteries.identifier") local identifier = require("batteries.identifier")
local stringx = require("batteries.stringx")
-- tablex {{{ -- tablex {{{
@ -196,3 +197,10 @@ local function test_ulid()
assert(not ulid:match("[ILOU%l]")) assert(not ulid:match("[ILOU%l]"))
end end
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 return s
end 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 return stringx