fix: word break/transform

This commit is contained in:
2024-04-30 12:25:25 +03:00
parent bbe802e0b2
commit a53e0d6d64
3 changed files with 60 additions and 52 deletions

View File

@@ -1,24 +1,29 @@
local D = require("text-transform.util.debug")
local utils = require("text-transform.util")
local fn = {}
local transformers = {}
fn.WORD_BOUNDRY = "[%_%-%s%.]"
transformers.WORD_BOUNDRY = "[%_%-%s%.]"
--- Splits a string into words.
--- @param string string
--- @return table
function fn.to_words(string)
function transformers.to_words(string)
local words = {}
local word = ""
local last_is_upper = false
for i = 1, #string do
local char = string:sub(i, i)
if char:match(fn.WORD_BOUNDRY) then
if char:match(transformers.WORD_BOUNDRY) then
if word ~= "" then
table.insert(words, word:lower())
end
word = ""
else
if not last_is_upper and char:match("%u") and word ~= "" then
table.insert(words, word:lower())
word = ""
end
word = word .. char
-- word = word .. string:sub(i)
-- break
@@ -36,19 +41,22 @@ end
--- The callback is called with the word, the index, and the table of words.
--- The separator is added between each word.
---
--- @param words table of strings
--- @param words string|table string or table of strings
--- @param with_word_cb function (word: string, index: number, words: table) -> string
--- @param separator string|nil (optional)
--- @return string
function fn.transform_words(words, with_word_cb, separator)
function transformers.transform_words(words, with_word_cb, separator)
if type(words) ~= "table" then
words = transformers.to_words(words)
end
local out = ""
for i, word in ipairs(words) do
local new_word = with_word_cb(word, i, word)
out = out .. new_word
if separator and i > 1 then
out = out .. separator
new_word = separator .. new_word
end
D.log("transformers", "word %s new_word %s out %s", word, new_word, out)
out = out .. new_word
D.log("transformers", "word %s (%d) new_word %s out %s", word, i, new_word, out)
end
return out
end
@@ -56,8 +64,8 @@ end
--- Transforms a string into camelCase.
--- @param string string
--- @return string
function fn.to_camel_case(string)
return fn.transform_words(fn.to_words(string), function(word, i)
function transformers.to_camel_case(string)
return transformers.transform_words(string, function(word, i)
if i == 1 then
return word:lower()
end
@@ -68,8 +76,8 @@ end
--- Transfroms a string into snake_case.
--- @param string any
--- @return string
function fn.to_snake_case(string)
return fn.transform_words(fn.to_words(string), function(word, i)
function transformers.to_snake_case(string)
return transformers.transform_words(string, function(word, i)
if i == 1 then
return word:lower()
end
@@ -80,16 +88,16 @@ end
--- Transforms a string into PascalCase.
--- @param string string
--- @return string
function fn.to_pascal_case(string)
local cc = fn.to_camel_case(string)
function transformers.to_pascal_case(string)
local cc = transformers.to_camel_case(string)
return cc:sub(1, 1):upper() .. cc:sub(2)
end
--- Transforms a string into Title Case.
--- @param string string
--- @return string
function fn.to_title_case(string)
return fn.transform_words(fn.to_words(string), function(word)
function transformers.to_title_case(string)
return transformers.transform_words(string, function(word)
return word:sub(1, 1):upper() .. word:sub(2):lower()
end, " ")
end
@@ -97,8 +105,8 @@ end
--- Transforms a string into kebab-case.
--- @param string string
--- @return string
function fn.to_kebab_case(string)
return fn.transform_words(fn.to_words(string), function(word)
function transformers.to_kebab_case(string)
return transformers.transform_words(string, function(word)
return word:lower()
end, "-")
end
@@ -106,8 +114,8 @@ end
--- Transforms a string into dot.case.
--- @param string string
--- @return string
function fn.to_dot_case(string)
return fn.transform_words(fn.to_words(string), function(word)
function transformers.to_dot_case(string)
return transformers.transform_words(string, function(word)
return word:lower()
end, ".")
end
@@ -115,10 +123,10 @@ end
--- Transforms a string into CONSTANT_CASE.
--- @param string string
--- @return string
function fn.to_const_case(string)
return fn.transform_words(fn.to_words(string), function(word)
function transformers.to_const_case(string)
return transformers.transform_words(string, function(word)
return word:upper()
end, "_")
end
return fn
return transformers