Skip to content

Commit c776692

Browse files
authored
Merge pull request #1357 from harehare/feat/add-slugify-function
✨ feat: add slugify function
2 parents a004113 + 7a19a19 commit c776692

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

crates/mq-lang/builtin.mq

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,11 @@ def bsearch(arr, target):
618618
end
619619
end
620620

621+
# Converts a string into a URL-friendly slug by lowercasing, replacing non-alphanumeric characters with hyphens, and trimming hyphens from the ends.
622+
def slugify(s, separator = "-"):
623+
s
624+
| downcase()
625+
| gsub("[^a-z0-9]+", separator)
626+
| gsub("^-+|-+$", "")
627+
end
628+

crates/mq-lang/builtin_tests.mq

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,17 @@ def test_bsearch():
600600
| assert_eq(result4, 1)
601601
end
602602

603+
def test_slugify():
604+
let result1 = slugify("Hello World!")
605+
| assert_eq(result1, "hello-world")
606+
607+
| let result2 = slugify("This is a test.")
608+
| assert_eq(result2, "this-is-a-test")
609+
610+
| let result3 = slugify("Special characters: @#$%^&*()")
611+
| assert_eq(result3, "special-characters")
612+
end
613+
603614
| run_tests([
604615
# Type checking
605616
test_case("is_array", test_is_array),
@@ -616,6 +627,7 @@ end
616627
test_case("rtrimstr", test_rtrimstr),
617628
test_case("lpad", test_lpad),
618629
test_case("rpad", test_rpad),
630+
test_case("slugify", test_slugify),
619631

620632
# Collection operations
621633
test_case("is_empty", test_is_empty),

0 commit comments

Comments
 (0)