Skip to content

Commit

Permalink
clean up isvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
acxz committed Jun 16, 2024
1 parent 0097083 commit 9ea7507
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 51 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ camel_case_acro = PatternStringCase(
tokencasefirst,
strcasefirst,
acronymintoken,
splitonnumber
splitonnumber,
);

# Convert a string from our newly defined string case, camel_case_acro, to a
Expand All @@ -81,13 +81,8 @@ StringCases.convert("stringCasesFTW!", camel_case_acro, StringCases.PASCAL_CASE)
# Now let's define a delimiter string case with hyphens, -, while preserving the
# original casing of the string via StringCases.anycase
# this is useful for keeping the acronym around
camel_train_any_case = DelimiterStringCase(
"camel-Train-ANY-Case",
anycase,
uppercase,
lowercase,
"-"
);
camel_train_any_case =
DelimiterStringCase("camel-Train-ANY-Case", anycase, uppercase, lowercase, "-");

StringCases.convert("stringCasesFTW!", camel_case_acro, camel_train_any_case)
# Output: "string-Cases-FTW!"
Expand All @@ -102,13 +97,8 @@ wordpat = r"
"x;

# You can use it like so:
my_pattern_case = PatternStringCase(
"myCamelCaseACRO123",
lowercase,
uppercase,
lowercase,
wordpat
);
my_pattern_case =
PatternStringCase("myCamelCaseACRO123", lowercase, uppercase, lowercase, wordpat);

StringCases.convert("askBest30MWPrice", my_pattern_case, StringCases.SNAKE_CASE)
# Output: "ask_best_30mw_price"
Expand Down Expand Up @@ -171,6 +161,18 @@ StringCases.split("askBest30MWPrice", camel_case_acro_num)
# "30MW"
# "Price"

# Validating a string to a StringCase is done with StringCases.isvalid
StringCases.isvalid("String.Cases-_FTW!", StringCases.KEBAB_CASE)
# Output: false

# After converting the string to kebab case
StringCases.convert("String.Cases-_FTW!", StringCases.TRAIN_CASE, StringCases.KEBAB_CASE)
# Output: string.cases-_ftw!"

# We now have a valid kebab case string
StringCases.isvalid("string.cases-_ftw!", StringCases.KEBAB_CASE)
# Output: true

```

For more examples see the
Expand Down
39 changes: 3 additions & 36 deletions src/stringcases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,40 +281,7 @@ function convert(
return join(tokens, output_sc)
end

# TODO: add isvalid, validated_tokens, and correct_tokens as output to an
# encompassing validate function,
# this helps redundant splits if we run validate inside convert
function validate(s::AbstractString, sc::AbstractStringCase)
# Split string based string case
tokens = split(s, sc)

# Validate split tokens with respect to the original string
is_valid_str = s == join(tokens, sc)

# Check case for all but first token
correct_tokens = Vector{SubString{typeof(s)}}()
for token in tokens[(begin + 1):end]
correct_token_wip = sc.tokencase(token)
correct_token = _casefirst(correct_token_wip, sc.strcasefirst)

if token != correct_token
is_valid_str = false
end

push!(correct_tokens, correct_token)
end

# Check case for first token
correct_first_token_wip = sc.tokencase(first(tokens))
correct_first_token = _casefirst(correct_first_token_wip, sc.strcasefirst)

if first(tokens) != correct_first_token
is_valid_str = false
end

pushfirst!(correct_tokens, correct_first_token)

correct_str = join(correct_tokens, sc)

return is_valid_str
function isvalid(s::AbstractString, sc::AbstractStringCase)
# Validate converted string to the original string
return s == convert(s, sc, sc)
end

0 comments on commit 9ea7507

Please sign in to comment.