fix: uppercase logic

fix: precommit
This commit is contained in:
2024-04-30 13:38:17 +03:00
parent c0a354ac79
commit a669a998f4
3 changed files with 49 additions and 43 deletions

View File

@@ -21,25 +21,31 @@ function transformers.to_words(string)
end
word = ""
else
if not last_is_upper and char:match("%u") and word ~= "" then
table.insert(words, word:lower())
last_is_upper = true
word = ""
if
(char:match("%d") and not last_is_digit)
or (char:match("%u") and not last_is_upper and word ~= "")
or (char:match("%l") and last_is_digit)
then
if word ~= "" then
table.insert(words, word:lower())
word = ""
end
end
if char:match("%l") then
last_is_upper = false
end
if not last_is_digit and char:match("%d") and word ~= "" then
table.insert(words, word:lower())
-- Update flags based on current character type
if char:match("%d") then
last_is_digit = true
word = ""
end
if char:match("%D") then
last_is_upper = false
elseif char:match("%u") then
last_is_upper = true
last_is_digit = false
else -- Lowercase or any non-digit/non-uppercase
last_is_upper = false
last_is_digit = false
end
-- Append current character to the current word
word = word .. char
-- word = word .. string:sub(i)
-- break
end
D.log("transformers", "i %d char %s word %s words %s", i, char, word, utils.dump(words))
end

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# shellcheck disable=SC2181
# shellcheck disable=SC2181,SC2086
# Stylua check
diffs=$(stylua --check --output-format=json .)
@@ -10,8 +10,8 @@ if [[ "$?" -ne 0 ]]; then
filelist="$(echo "$diffs" | jq -r '.file')"
# Format & add to git
stylua "$filelist"
git add "$filelist"
stylua $filelist
git add $filelist
fi
# Run lints

View File

@@ -23,47 +23,47 @@ local T = MiniTest.new_set({
},
})
local function test_string(child, str)
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words("]] .. str .. [[")]])
end
T["to_words()"] = MiniTest.new_set()
T["to_words()"]["should split two words with spaces"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words("hello world")]])
T["to_words()"]["should split normal spaced words"] = function()
test_string(child, "hello world")
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
eq_global(child, "result", { "hello", "world" })
end
T["to_words()"]["should split two words with no spaces"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words("helloWorld")]])
T["to_words()"]["should split camel case strings"] = function()
test_string(child, "helloWorld")
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
eq_global(child, "result", { "hello", "world" })
end
T["to_words()"]["should split two words with dots"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words("hello.world")]])
T["to_words()"]["should split dot case strings"] = function()
test_string(child, "hello.world")
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
eq_global(child, "result", { "hello", "world" })
end
T["to_words()"]["should split two words with a number inside"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words("helloWorld123")]])
T["to_words()"]["should split const case strings"] = function()
test_string(child, "HELLO_WORLD")
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
eq_global(child, "result[3]", "123")
eq_global(child, "result", { "hello", "world" })
end
T["to_words()"]["should split two words and ignore trailing/leading spaces"] = function()
child.lua([[require('text-transform').setup()]])
child.lua([[result = require('text-transform').to_words(" hello world ")]])
T["to_words()"]["should treat numbers as words"] = function()
test_string(child, "helloWorld123")
eq_type_global(child, "result", "table")
eq_global(child, "result[1]", "hello")
eq_global(child, "result[2]", "world")
eq_global(child, "result", { "hello", "world", "123" })
end
T["to_words()"]["should trim trailing/leading"] = function()
test_string(child, " hello world ")
eq_type_global(child, "result", "table")
eq_global(child, "result", { "hello", "world" })
end
return T