From 8ce9aece1ac35cad7644eae7f87bb7c82c1c9f41 Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Sun, 2 Mar 2025 12:12:15 +0700 Subject: [PATCH] update --- src/util.jl | 165 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 143 insertions(+), 22 deletions(-) diff --git a/src/util.jl b/src/util.jl index 7d977e6..344bcc8 100644 --- a/src/util.jl +++ b/src/util.jl @@ -5,7 +5,8 @@ export timedifference, showstracktrace, findHighestIndexKey, uuid4snakecase, rep dataframeToCSV, dfToVectorDict, disintegrate_vectorDict, getDataFrameValue, dfRowtoString, dfToString, dataframe_to_json_list, dictToString, dictToString_noKey, dictToString_numbering, extract_triple_backtick_text, - countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter + countGivenWords, remove_french_accents, detect_keyword, extractTextBetweenCharacter, + convertCamelSnakeKebabCase using JSON3, DataStructures, Distributions, Random, Dates, UUIDs, MQTTClient, DataFrames @@ -741,10 +742,10 @@ end Extracts text enclosed within triple backticks (```) from the given string. # Arguments: -- `text::String`: The input string containing potential triple backtick blocks. +- `text::String` The input string containing potential triple backtick blocks. # Returns: -- `Vector{String}`: A vector of strings, each representing a block of text enclosed within triple backticks found in the input string. +- `Vector{String}` A vector of strings, each representing a block of text enclosed within triple backticks found in the input string. # Examples: ```jldoctest @@ -772,11 +773,11 @@ end Detects if a keyword exists in the text in different case variations (lowercase, uppercase first letter, or all uppercase). # Arguments: -- `keyword::String`: The keyword to search for -- `text::String`: The text to search in +- `keyword::String` The keyword to search for +- `text::String` The text to search in # Returns: -- `Union{Nothing, String}`: Returns the matched keyword variation if found, otherwise returns nothing +- `Union{Nothing, String}` Returns the matched keyword variation if found, otherwise returns nothing # Examples: ```jldoctest @@ -815,10 +816,10 @@ Count the occurrences of each word in the given list within the provided text. # Arguments - `text::String`: The input text to search through. -- `words::Vector{String}`: A vector of words whose occurrences need to be counted. +- `words::Vector{String}` A vector of words whose occurrences need to be counted. # Returns -- `Vector{Int64}`: Their respective counts in the `text`. +- `Vector{Int64}` Their respective counts in the `text`. # Examples ```jldoctest @@ -857,10 +858,10 @@ end Remove French accents from the given text. # Arguments -- `text::String`: The input string containing French accents. +- `text::String` The input string containing French accents. # Returns -- `String`: The input string with all French accents removed. +- `String` The input string with all French accents removed. # Examples ```jldoctest @@ -908,23 +909,22 @@ end """ - extractTextBetweenCharacters(text::String, start_char::Char, end_char::Char)::String - Extracts and returns the text that is enclosed between two specified characters within a given string. -# Arguments: -- `text::String`: The input string from which to extract the text. -- `startchar::Char`: The starting character that marks the beginning of the desired text. -- `endchar::Char`: The ending character that marks the end of the desired text. +# Arguments +- `text::String` The input string from which to extract the text. +- `startchar::Char` The starting character that marks the beginning of the desired text. +- `endchar::Char` The ending character that marks the end of the desired text. -# Returns: -- `String`: The substring enclosed between `start_char` and `end_char`. +# Returns +- `String` The substring enclosed between `start_char` and `end_char`. -# Examples: +# Examples ```jldoctest - julia> text = "Hello [World]!" - julia> extracted_text = extractTextBetweenCharacter(text, '[', ']') - println(extracted_text) # Output: "World" +julia> text = "Hello [World]!" +julia> extracted_text = extractTextBetweenCharacter(text, '[', ']') +println(extracted_text) # Output: "World" +``` """ function extractTextBetweenCharacter(text::String, startchar::Char, endchar::Char) result = [] @@ -947,8 +947,129 @@ function extractTextBetweenCharacter(text::String, startchar::Char, endchar::Cha end +""" +Determines if the given string follows camel case naming convention. + +# Arguments: +- `text::String` The input string to check for camel case. + +# Returns: +- `Bool` True if the string is in camel case, False otherwise. + +# Examples: +```jldoctest +julia> isCamelCase("helloWorld") +true +julia> isCamelCase("HelloWorld") +false +julia> isCamelCase("hello_world") +false +``` +""" +function isCamelCase(text::String) + # Regular expression to match camel case + pattern = r"^[a-z]+(?:[A-Z][a-z]*)*$" + return occursin(pattern, text) +end + +""" + Splits a string at the boundaries of camel case words. + +# Arguments +- `text::String` The input string to be split. + +# Returns +- `Vector{String}` A vector containing the individual words from the camel case string. + +# Examples +```jldoctest +julia> text = "splitCamelCaseFunction" +julia> words = splitCamelCase(text) +println(words) # Output: ["split", "Camel", "Case", "Function"] +``` +""" +function splitCamelCase(text::String) + # Regular expression to match the boundaries of camel case words + # split_pattern = r"(?<=.)(?=[A-Z])" + split_pattern = r"(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])" + result = split(text, split_pattern) + return result +end +""" + Converts a string between Camel Case, Snake Case, and Kebab Case. + +# Arguments +- `text::String` The input string to be converted. +- `tocase::Symbol` The target case format. Can be one of :camel, :snake, or :kebab. + +# Returns +- `String` The converted string in the specified case format. + +# Examples +```jldoctest +julia> text = "splitCamelCaseFunction" +julia> converted_text = convertCamelSnakeKebabCase(text, :snake) +println(converted_text) # Output: "split_camel_case_function" +julia> converted_text = convertCamelSnakeKebabCase(text, :kebab) +println(converted_text) # Output: "split-camel-case-function" +julia> converted_text = convertCamelSnakeKebabCase(text, :camel) +println(converted_text) # Output: "splitCamelCaseFunction" +``` +""" +function convertCamelSnakeKebabCase(text::T, tocase::Symbol)::String where {T<:AbstractString} + # Check the current case format of the input text + iscamel = isCamelCase(text) + issnake = occursin('_', text) # Check for snake case by looking for underscores + iskebab = occursin('-', text) # Check for kebab case by looking for hyphens + + if iscamel + # Handle camel case input + wordlist = splitCamelCase(text) + if tocase == :camel + return text # Already in camel case, return as is + elseif tocase == :snake + wordlist = lowercase.(wordlist) # Convert all words to lowercase + return join(wordlist, '_') # Join with underscores for snake case + elseif tocase == :kebab + wordlist = lowercase.(wordlist) # Convert all words to lowercase + return join(wordlist, '-') # Join with hyphens for kebab case + end + + elseif issnake + # Handle snake case input + wordlist = split(text, '_') # Split at underscores + if tocase == :camel + wordlist = lowercase.(wordlist) + wordlist = uppercasefirst.(wordlist) # Capitalize first letter of each word + wordlist[1] = lowercase(wordlist[1]) # First word should start with lowercase + return join(wordlist) # Join without separator for camel case + elseif tocase == :snake + return text # Already in snake case, return as is + elseif tocase == :kebab + wordlist = lowercase.(wordlist) # Convert all words to lowercase + return join(wordlist, '-') # Join with hyphens for kebab case + end + + elseif iskebab + # Handle kebab case input + wordlist = split(text, '-') # Split at hyphens + if tocase == :camel + wordlist = lowercase.(wordlist) + wordlist = uppercasefirst.(wordlist) # Capitalize first letter of each word + wordlist[1] = lowercase(wordlist[1]) # First word should start with lowercase + return join(wordlist) # Join without separator for camel case + elseif tocase == :snake + wordlist = lowercase.(wordlist) # Convert all words to lowercase + return join(wordlist, '_') # Join with underscores for snake case + elseif tocase == :kebab + return text # Already in kebab case, return as is + end + else + error("Undefined condition") # Input text format not recognized + end +end