Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def run_palindrome(args):
def run_reverse(args):
print(textutils.reverse_words(args.text))

def run_slugify(args):
print(textutils.slugify(args.text))
COMMAND_TABLE = {
"wc": run_wc,
"convert": run_convert,
Expand All @@ -64,6 +66,7 @@ def run_reverse(args):
"json": run_json,
"palindrome": run_palindrome,
"reverse": run_reverse,
"slugify": run_slugify,
}

def build_parser() -> argparse.ArgumentParser:
Expand Down Expand Up @@ -103,9 +106,11 @@ def build_parser() -> argparse.ArgumentParser:

rw = sub.add_parser("reverse", help="reverse the words in a sentence")
rw.add_argument("text")
return parser

sg = sub.add_parser("slugify", help="convert text into a URL-friendly slug")
sg.add_argument("text")

return parser
def main(argv=None) -> int:
"""Run a fixit command and return its exit status.

Expand Down
14 changes: 14 additions & 0 deletions fixit/textutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ def reverse_words(text: str) -> str:
reverse_words("hello world") returns "world hello".
"""
return " ".join(reversed(text.split()))


def slugify(text: str) -> str:
"""Convert text into a URL-friendly slug.

Example:
slugify("Hello World!") returns "hello-world".
"""
import re

text = text.lower()
text = re.sub(r"[^\w\s-]", "", text)
text = re.sub(r"[\s_-]+", "-", text)
return text.strip("-")
9 changes: 8 additions & 1 deletion tests/test_textutils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from fixit.textutils import is_palindrome, reverse_words
from fixit.textutils import is_palindrome, reverse_words, slugify


def test_simple_palindrome():
Expand All @@ -18,3 +18,10 @@ def test_palindrome_ignores_punctuation():

def test_reverse_words_extra_spaces():
assert reverse_words("hello world") == "world hello"

def test_slugify_basic():
assert slugify("Hello World!") == "hello-world"


def test_slugify_punctuation_and_spaces():
assert slugify("Open Source, Project!") == "open-source-project"
Loading