Skip to content

Commit e2c315a

Browse files
committed
fix: uppercase logic
fix: precommit
1 parent 043e02a commit e2c315a

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

Diff for: lua/text-transform/transformers.lua

+20-14
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,31 @@ function transformers.to_words(string)
2121
end
2222
word = ""
2323
else
24-
if not last_is_upper and char:match("%u") and word ~= "" then
25-
table.insert(words, word:lower())
26-
last_is_upper = true
27-
word = ""
28-
end
29-
if char:match("%l") then
30-
last_is_upper = false
24+
if
25+
(char:match("%d") and not last_is_digit)
26+
or (char:match("%u") and not last_is_upper and word ~= "")
27+
or (char:match("%l") and last_is_digit)
28+
then
29+
if word ~= "" then
30+
table.insert(words, word:lower())
31+
word = ""
32+
end
3133
end
32-
if not last_is_digit and char:match("%d") and word ~= "" then
33-
table.insert(words, word:lower())
34+
35+
-- Update flags based on current character type
36+
if char:match("%d") then
3437
last_is_digit = true
35-
word = ""
36-
end
37-
if char:match("%D") then
38+
last_is_upper = false
39+
elseif char:match("%u") then
40+
last_is_upper = true
41+
last_is_digit = false
42+
else -- Lowercase or any non-digit/non-uppercase
43+
last_is_upper = false
3844
last_is_digit = false
3945
end
46+
47+
-- Append current character to the current word
4048
word = word .. char
41-
-- word = word .. string:sub(i)
42-
-- break
4349
end
4450
D.log("transformers", "i %d char %s word %s words %s", i, char, word, utils.dump(words))
4551
end

Diff for: scripts/precommit.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2181
2+
# shellcheck disable=SC2181,SC2086
33

44
# Stylua check
55
diffs=$(stylua --check --output-format=json .)
@@ -10,8 +10,8 @@ if [[ "$?" -ne 0 ]]; then
1010
filelist="$(echo "$diffs" | jq -r '.file')"
1111

1212
# Format & add to git
13-
stylua "$filelist"
14-
git add "$filelist"
13+
stylua $filelist
14+
git add $filelist
1515
fi
1616

1717
# Run lints

Diff for: tests/test_to_words.lua

+26-26
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,47 @@ local T = MiniTest.new_set({
2323
},
2424
})
2525

26+
local function test_string(child, str)
27+
child.lua([[require('text-transform').setup()]])
28+
child.lua([[result = require('text-transform').to_words("]] .. str .. [[")]])
29+
end
30+
2631
T["to_words()"] = MiniTest.new_set()
2732

28-
T["to_words()"]["should split two words with spaces"] = function()
29-
child.lua([[require('text-transform').setup()]])
30-
child.lua([[result = require('text-transform').to_words("hello world")]])
33+
T["to_words()"]["should split normal spaced words"] = function()
34+
test_string(child, "hello world")
3135
eq_type_global(child, "result", "table")
32-
eq_global(child, "result[1]", "hello")
33-
eq_global(child, "result[2]", "world")
36+
eq_global(child, "result", { "hello", "world" })
3437
end
3538

36-
T["to_words()"]["should split two words with no spaces"] = function()
37-
child.lua([[require('text-transform').setup()]])
38-
child.lua([[result = require('text-transform').to_words("helloWorld")]])
39+
T["to_words()"]["should split camel case strings"] = function()
40+
test_string(child, "helloWorld")
3941
eq_type_global(child, "result", "table")
40-
eq_global(child, "result[1]", "hello")
41-
eq_global(child, "result[2]", "world")
42+
eq_global(child, "result", { "hello", "world" })
4243
end
4344

44-
T["to_words()"]["should split two words with dots"] = function()
45-
child.lua([[require('text-transform').setup()]])
46-
child.lua([[result = require('text-transform').to_words("hello.world")]])
45+
T["to_words()"]["should split dot case strings"] = function()
46+
test_string(child, "hello.world")
4747
eq_type_global(child, "result", "table")
48-
eq_global(child, "result[1]", "hello")
49-
eq_global(child, "result[2]", "world")
48+
eq_global(child, "result", { "hello", "world" })
5049
end
5150

52-
T["to_words()"]["should split two words with a number inside"] = function()
53-
child.lua([[require('text-transform').setup()]])
54-
child.lua([[result = require('text-transform').to_words("helloWorld123")]])
51+
T["to_words()"]["should split const case strings"] = function()
52+
test_string(child, "HELLO_WORLD")
5553
eq_type_global(child, "result", "table")
56-
eq_global(child, "result[1]", "hello")
57-
eq_global(child, "result[2]", "world")
58-
eq_global(child, "result[3]", "123")
54+
eq_global(child, "result", { "hello", "world" })
5955
end
6056

61-
T["to_words()"]["should split two words and ignore trailing/leading spaces"] = function()
62-
child.lua([[require('text-transform').setup()]])
63-
child.lua([[result = require('text-transform').to_words(" hello world ")]])
57+
T["to_words()"]["should treat numbers as words"] = function()
58+
test_string(child, "helloWorld123")
59+
eq_type_global(child, "result", "table")
60+
eq_global(child, "result", { "hello", "world", "123" })
61+
end
62+
63+
T["to_words()"]["should trim trailing/leading"] = function()
64+
test_string(child, " hello world ")
6465
eq_type_global(child, "result", "table")
65-
eq_global(child, "result[1]", "hello")
66-
eq_global(child, "result[2]", "world")
66+
eq_global(child, "result", { "hello", "world" })
6767
end
6868

6969
return T

0 commit comments

Comments
 (0)