Skip to content

Commit

Permalink
feat: update new format rules camel_case, pascal_case, `snake_cas…
Browse files Browse the repository at this point in the history
…e`, `kebab_case`
  • Loading branch information
elhoangvu committed May 3, 2024
1 parent 91c4b88 commit 6830688
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,18 @@ This rule formats text according to specified rules. This rule now supports the
... "format_lowercase": "UPPERCASE TITLE",
... "format_uppercase": "lowercase content",
... "format_upper_head": "camelVariable",
... "format_camel_case": "Camel case",
... "format_pascal_case": "pascal case",
... "format_kabab_case": "Kebab case",
... "format_snake_case": "Snake case",
... }).generated()
uppercase title
LOWERCASE CONTENT
CamelVariable
camelCase
PascalCase
kebab-case
snake_case
```

### IndentRule
Expand Down
39 changes: 39 additions & 0 deletions src/dictrule/built_in_rules/format_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Optional,
)

import re
from enum import Enum
from ..rule import Rule
from ..dr_property import dr_property
Expand All @@ -27,10 +28,18 @@ class FormatRule(Rule):
... "format_lowercase": "UPPERCASE TITLE",
... "format_uppercase": "lowercase content",
... "format_upper_head": "camelVariable",
... "format_camel_case": "Camel case",
... "format_pascal_case": "pascal case",
... "format_kabab_case": "Kebab case",
... "format_snake_case": "Snake case",
... }).generated()
uppercase title
LOWERCASE CONTENT
CamelVariable
camelCase
PascalCase
kebab-case
snake_case
"""

@dr_property(prefix_matching=True)
Expand All @@ -43,6 +52,10 @@ class Type(Enum):
LOWERCASE = "lowercase"
UPPERCASE = "uppercase"
UPPER_HEAD = "upper_head"
CAMEL_CASE = "camel_case"
PASCAL_CASE = "pascal_case"
KEBAB_CASE = "kebab_case"
SNAKE_CASE = "snake_case"

@staticmethod
def from_str(string: str) -> Optional["FormatRule.Type"]:
Expand Down Expand Up @@ -84,6 +97,32 @@ def format(
if self == FormatRule.Type.UPPER_HEAD:
return (text[0].upper() + text[1:]) if len(text) > 0 else ""

if self == FormatRule.Type.CAMEL_CASE:
words = list(filter(None, re.split(r"[^a-zA-Z0-9]+", text)))
return (
words[0][0].lower()
+ (words[0][1:] if len(words[0]) > 0 else "")
+ "".join(
word[0].upper() + (word[1:] if len(word) > 0 else "")
for word in words[1:]
)
)

if self == FormatRule.Type.PASCAL_CASE:
words = list(filter(None, re.split(r"[^a-zA-Z0-9]+", text)))
return "".join(
word[0].upper() + (word[1:] if len(word) > 0 else "")
for word in words
)

if self == FormatRule.Type.KEBAB_CASE:
kebab_text = re.sub(r"[^a-zA-Z0-9]+", "-", text).strip("-").lower()
return kebab_text

if self == FormatRule.Type.SNAKE_CASE:
snake_text = re.sub(r"[^a-zA-Z0-9]+", "_", text).strip("_").lower()
return snake_text

return text

def parse(
Expand Down
113 changes: 112 additions & 1 deletion tests/built_in_rules/test_format_rule.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""FormatRule test"""

from dictrule.built_in_rules import FormatRule
import unittest
from dictrule.built_in_rules import FormatRule


class TestFormatRule(unittest.TestCase):
Expand Down Expand Up @@ -46,6 +46,117 @@ def test_format_upper_head(self):
)
self.assertEqual(parsed, "MyText 123")

def test_format_camel_case(self):
"""Test method"""

texts = [
"My Camel Case 123",
"My CamelCase 123",
"MyCamelCase 123",
"MyCamelCase123",
"myCamelCase123",
"My-Camel-Case 123",
"My-Camel-Case-123",
"---My---Camel---Case---123---",
"My_Camel_Case_123",
"$$My $Camel $Case $123",
"$$my $camel $case $123",
]

rule = FormatRule()

for text in texts:
parsed = rule.parse(
rule_dict={
"format_camel_case": text,
},
rule_callback=lambda x, y: y,
)
self.assertEqual(parsed, "myCamelCase123")

def test_format_pascal_case(self):
"""Test method"""

texts = [
"My Pascal Case 123",
"my pascal case 123",
"My PascalCase 123",
"MyPascalCase 123",
"MyPascalCase123",
"myPascalCase123",
"My-Pascal-Case 123",
"My-Pascal-Case-123",
"---My---Pascal---Case---123---",
"My_Pascal_Case_123",
"$$My $Pascal $Case $123",
"$$my $pascal $case $123",
]

rule = FormatRule()

for text in texts:
parsed = rule.parse(
rule_dict={
"format_pascal_case": text,
},
rule_callback=lambda x, y: y,
)
self.assertEqual(parsed, "MyPascalCase123")

def test_format_kebab_case(self):
"""Test method"""

texts = [
"My Kebab Case 123",
"my kebab case 123",
"My-Kebab-Case 123",
"My-Kebab-Case-123",
"---My---Kebab---Case---123---",
"My_Kebab_Case_123",
"$$My $Kebab $Case $123",
"$$my $kebab $case $123",
"my-kebab-case-123",
"__my-kebab-case-123__",
]

rule = FormatRule()

for text in texts:
parsed = rule.parse(
rule_dict={
"format_kebab_case": text,
},
rule_callback=lambda x, y: y,
)
self.assertEqual(parsed, "my-kebab-case-123")

def test_format_snake_case(self):
"""Test method"""

texts = [
"My Snake Case 123",
"my snake case 123",
"My-Snake-Case 123",
"My-Snake-Case-123",
"---My---Snake---Case---123---",
"My_Snake_Case_123",
"$$My $Snake $Case $123",
"$$my $snake $case $123",
"my-snake-case-123",
"__my-snake-case-123__",
]

rule = FormatRule()

for text in texts:
parsed = rule.parse(
rule_dict={
"format_snake_case": text,
},
rule_callback=lambda x, y: y,
)
self.assertEqual(parsed, "my_snake_case_123")

def test_format_empty(self):
"""Test method"""

Expand Down

0 comments on commit 6830688

Please sign in to comment.