Skip to content

Commit

Permalink
created a slug method to the strings module
Browse files Browse the repository at this point in the history
  • Loading branch information
tacheraSasi committed Dec 10, 2024
1 parent dfcf635 commit deec237
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 3 deletions.
Binary file removed binaries/vintLang_android_arm64_v0.1.0.tar.gz
Binary file not shown.
Binary file added binaries/vintLang_android_arm64_v0.1.3.tar.gz
Binary file not shown.
Binary file removed binaries/vintLang_linux_amd64_v0.1.0.tar.gz
Binary file not shown.
Binary file removed binaries/vintLang_linux_amd64_v0.1.2.tar.gz
Binary file not shown.
Binary file removed binaries/vintLang_mac_amd64_v0.1.0.tar.gz
Binary file not shown.
Binary file removed binaries/vint_android_arm64_v0.5.1.tar.gz
Binary file not shown.
32 changes: 32 additions & 0 deletions module/string.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package module

import (
"regexp"
"strings"

"github.com/ekilie/vint-lang/object"
"github.com/xrash/smetrics" // A library for string metrics, like Levenshtein
)
Expand All @@ -20,8 +22,38 @@ func init() {
StringFunctions["length"] = length
StringFunctions["indexOf"] = indexOf
StringFunctions["similarity"] = similarity
StringFunctions["slug"] = slug
}

// Creates a slug string from a normal string
func slug(args []object.Object, defs map[string]object.Object) object.Object {
// Ensure exactly one argument is passed
if len(args) != 1 {
return &object.Error{Message: "string.slug requires exactly one argument"}
}

// Inspect the argument and convert it to a string
input := args[0].Inspect()

// Convert the input to lowercase
input = strings.ToLower(input)

// Remove all non-alphanumeric characters except spaces and hyphens
re := regexp.MustCompile(`[^a-z0-9\s-]+`)
input = re.ReplaceAllString(input, "")

// Replace spaces and multiple hyphens with a single hyphen
re = regexp.MustCompile(`[\s-]+`)
input = re.ReplaceAllString(input, "-")

// Trim leading and trailing hyphens
input = strings.Trim(input, "-")

// Return the result as a Vint object string
return &object.String{Value: input}
}


// similarity computes a similarity score between two strings
func similarity(args []object.Object, defs map[string]object.Object) object.Object {
if len(args) != 2 {
Expand Down
Empty file added vint.upx
Empty file.
5 changes: 4 additions & 1 deletion vintLang/builtins.vint
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
print(eq(2,"")) //Prints False
print(eq(2,"")) //Prints False

a = print
a("yeee")
8 changes: 6 additions & 2 deletions vintLang/strings.vint
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ result = string.substring("Hello, World!", 0, 5)
print(result) // Output: "Hello"

// Example 12: Invalid indices (start >= end)
result = string.substring("Hello, World!", 7, 3)
print(result) // Output: Error: Invalid start or end index
// result = string.substring("Hello, World!", 7, 3)
// print(result) // Output: Error: Invalid start or end index

//Example 12: Generating a slug
result = string.slug("Creates a slug string from a normal string")
print(result) //Output: string: creates-a-slug-string-from-a-normal-string


/*
Expand Down

0 comments on commit deec237

Please sign in to comment.