moon add myfreess/texttest "camelCase conversion" {
inspect(@text.to_camel_case("hello world"), content="helloWorld")
inspect(@text.to_camel_case("deno_is_awesome"), content="denoIsAwesome")
inspect(@text.to_camel_case("HTMLElement"), content="htmlElement")
}test "PascalCase conversion" {
inspect(@text.to_pascal_case("hello world"), content="HelloWorld")
inspect(@text.to_pascal_case("my_variable_name"), content="MyVariableName")
}test "snake_case conversion" {
inspect(@text.to_snake_case("Hello World"), content="hello_world")
inspect(@text.to_snake_case("XMLHttpRequest"), content="xml_http_request")
}test "kebab-case conversion" {
inspect(@text.to_kebab_case("Hello World"), content="hello-world")
inspect(@text.to_kebab_case("backgroundColor"), content="background-color")
}test "Levenshtein distance calculation" {
inspect(@text.levenshtein_distance("kitten", "sitting"), content="3")
inspect(@text.levenshtein_distance("hello", "hallo"), content="1")
inspect(@text.levenshtein_distance("", "abc"), content="3")
inspect(@text.levenshtein_distance("same", "same"), content="0")
}test "create matching options" {
let default_opts = @text.default_closest_string_options()
let case_opts = @text.case_sensitive_closest_string_options()
let similarity_opts = @text.default_compare_similarity_options()
let case_similarity_opts = @text.case_sensitive_compare_similarity_options()
}test "find closest string" {
let candidates = ["length", "size", "help", "world"]
// Simple case - finds "help" as closest to "hep"
let result = @text.closest_string_simple("hep", candidates)
inspect(result, content="help")
// With options
let opts = @text.default_closest_string_options()
let result2 = @text.closest_string("test", candidates, Some(opts))
inspect(result2, content="help")
}test "simple closest string matching" {
let words = ["apple", "application", "apply", "apt"]
let result = @text.closest_string_simple("app", words)
inspect(result, content="apt")
}test "multiple closest strings" {
let candidates = ["help", "hello", "world", "test", "heap"]
let top3 = @text.closest_strings("hep", candidates, 3, None)
// Should return top 3 matches sorted by similarity
inspect(top3.length(), content="3")
inspect(top3[0], content="help")
}test "similarity comparison function" {
let compare_fn = @text.compare_similarity("target", None)
let distance1 = compare_fn("target", "test")
let distance2 = compare_fn("target", "target")
inspect(distance2, content="0") // exact match
}test "simple similarity comparison" {
let compare_fn = @text.compare_similarity_simple("hello")
let distance = compare_fn("hello", "hallo")
inspect(distance, content="-1")
}test "sort by similarity" {
let words = ["world", "help", "hello", "test"]
let sorted = @text.sort_by_similarity(words, "hep", None)
// "help" should be first as it's most similar to "hep"
inspect(sorted[0], content="help")
}test "word splitting" {
let words = @text.split_to_words("hello_world-test")
@json.inspect(words, content=["hello", "world", "test"])
let camel_words = @text.split_to_words("camelCaseExample")
@json.inspect(camel_words, content=["camel", "Case", "Example"])
}test "word capitalization" {
inspect(@text.capitalize_word("hello"), content="Hello")
inspect(@text.capitalize_word("WORLD"), content="World")
inspect(@text.capitalize_word("tEST"), content="Test")
}test "lowercase conversion" {
inspect(@text.to_lowercase_string("Hello World"), content="hello world")
inspect(@text.to_lowercase_string("TEST123"), content="test123")
}test "error handling" {
let empty_array : Array[String] = []
let result = try {
@text.closest_string_simple("test", empty_array)
} catch {
@text.StringMatchingError(msg) => "Error: " + msg
}
inspect(result, content="Error: When using closest_string(), the possible_words array must contain at least one word")
}test "CLI command suggestions" {
let commands = ["install", "uninstall", "list", "search", "update"]
let suggestion = @text.closest_string_simple("instal", commands)
inspect(suggestion, content="install")
}test "API method matching" {
let methods = ["getUserById", "getUserByName", "getAllUsers", "createUser"]
let closest = @text.closest_string_simple("getUser", methods)
inspect(closest, content="getUserById")
}test "variable name formatting" {
let api_name = "user profile data"
inspect(@text.to_camel_case(api_name), content="userProfileData")
inspect(@text.to_pascal_case(api_name), content="UserProfileData")
inspect(@text.to_snake_case(api_name), content="user_profile_data")
inspect(@text.to_kebab_case(api_name), content="user-profile-data")
}pub struct ClosestStringOptions {
case_sensitive : Bool
compare_fn : (String, String) -> Int
}pub struct CompareSimilarityOptions {
case_sensitive : Bool
compare_fn : (String, String) -> Int
}fn capitalize_word(word : String) -> Stringfn closest_string(given_word : String, possible_words : Array[String], options : ClosestStringOptions?) -> String raise StringMatchingErrorlet possible_words = ["length", "size", "blah", "help"]
let suggestion = closest_string("hep", possible_words, None)
// suggestion will be "help"fn closest_string_simple(given_word : String, possible_words : Array[String]) -> String raise StringMatchingErrorfn closest_strings(given_word : String, possible_words : Array[String], count : Int, options : ClosestStringOptions?) -> Array[String]fn compare_similarity(given_word : String, options : CompareSimilarityOptions?) -> ((String, String) -> Int)let words = ["hi", "hello", "help"]
let sorted_words = sort_by_similarity(words, "hep", None)
// sorted_words will be ["help", "hi", "hello"]fn compare_similarity_simple(given_word : String) -> ((String, String) -> Int)fn levenshtein_distance(str1 : String, str2 : String) -> Intlet _ = levenshtein_distance("aa", "bb") // returns 2
let _ = levenshtein_distance("hello", "hallo") // returns 1fn sort_by_similarity(words : Array[String], given_word : String, options : CompareSimilarityOptions?) -> Array[String]fn to_camel_case(input : String) -> Stringlet _ = to_camel_case("deno is awesome") // "denoIsAwesome"
let _ = to_camel_case("hello_world") // "helloWorld"
let _ = to_camel_case("HTMLElement") // "htmlElement"fn to_kebab_case(input : String) -> Stringlet _ = to_kebab_case("deno is awesome") // "deno-is-awesome"
let _ = to_kebab_case("helloWorld") // "hello-world"
let _ = to_kebab_case("HTMLElement") // "html-element"fn to_pascal_case(input : String) -> Stringlet _ = to_pascal_case("deno is awesome") // "DenoIsAwesome"
let _ = to_pascal_case("hello_world") // "HelloWorld"
let _ = to_pascal_case("htmlElement") // "HtmlElement"fn to_snake_case(input : String) -> Stringlet _ = to_snake_case("deno is awesome") // "deno_is_awesome"
let _ = to_snake_case("helloWorld") // "hello_world"
let _ = to_snake_case("HTMLElement") // "html_element"