fix: into_words dot split

test: add into_words tests
This commit is contained in:
Chen Asraf
2023-05-18 00:54:30 +03:00
parent 63c0b700f7
commit f79799cd91
2 changed files with 35 additions and 8 deletions

View File

@@ -51,31 +51,31 @@ function TextTransform.into_words(str)
local words = {}
local word = ""
local previous_is_upper = false
local previous_is_split_token = false
for i = 1, #str do
local char = str:sub(i, i)
-- split on uppercase letters
if char:match("%u") and not previous_is_upper then
-- split on uppercase letters or numbers
if char:match("%u") or char:match("%d") and not previous_is_split_token then
if word ~= "" then
table.insert(words, word)
end
previous_is_upper = true
previous_is_split_token = true
word = char
-- split on underscores, hyphens, and spaces
elseif char:match("[%_%-%s]") then
elseif char:match("[%_%-%s%.]") then
if word ~= "" then
table.insert(words, word)
previous_is_upper = false
previous_is_split_token = false
end
word = ""
else
word = word .. char
previous_is_upper = char:match("%u")
previous_is_split_token = char:match("%u") or char:match("%d")
end
end
if word ~= "" then
table.insert(words, word)
previous_is_upper = false
previous_is_split_token = false
end
return words
end

View File

@@ -83,6 +83,33 @@ local function make_transform_test(fn_name, input, expected)
end
end
T["into_words()"] = MiniTest.new_set()
T["into_words()"]["should split two words with spaces"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').into_words("helloWorld")]])
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "World")
end
T["into_words()"]["should split two words with dots"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').into_words("hello.world")]])
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
end
T["into_words()"]["should split two words with a number inside"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').into_words("helloWorld123")]])
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "World")
eq_global(child, "result[3]", "123")
end
local map = {
["camel_case"] = {
{ "hello_world", "helloWorld" },