Module: LuckyCase
- Defined in:
- lib/lucky_case.rb,
lib/lucky_case/version.rb
Overview
LuckyCase
Convert and detect various letter cases in strings
Constant Summary collapse
- CASES =
{ snake_case: /^[[:lower:]]{1}[[:lower:]_0-9]+$/, upper_snake_case: /^[[:upper:]]{1}[[:upper:]_0-9]+$/, pascal_case: /^[[:upper:]]{1}[[:upper:][:lower:]0-9]+$/, camel_case: /^[[:lower:]]{1}[[:upper:][:lower:]0-9]+$/, dash_case: /^([[:lower:]]){1}[[:lower:]\-0-9]*[[:lower:]0-9]+$/, upper_dash_case: /^([[:upper:]]){1}[[:upper:]\-0-9]*[[:upper:]0-9]+$/, train_case: /^([[:upper:]][[:lower:]0-9]*\-|[0-9]+\-)*([[:upper:]][[:lower:]0-9]*)$/, word_case: /^[[:lower:]]{1}[[:lower:] 0-9]+$/, upper_word_case: /^[[:upper:]]{1}[[:upper:] 0-9]+$/, capital_word_case: /^([[:upper:]][[:lower:]0-9]*\ |[0-9]+\ )*([[:upper:]][[:lower:]0-9]*)$/, sentence_case: /^[[:upper:]]{1}[[:lower:] 0-9]+$/, mixed_case: /^[[:upper:][:lower:]][[:upper:][:lower:]_\-0-9 ]*$/, }
- FORMATS =
{ capital: /^[[:upper:]]{1}.*$/, upper_case: /^[^[:lower:]]+$/, lower_case: /^[^[:upper:]]+$/, }
- VERSION =
'1.1.0'.freeze
Class Method Summary collapse
-
.camel_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into camel case.
-
.camel_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is camel case.
-
.capital(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to capital.
-
.capital?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a capital letter.
-
.capital_word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into capital word case.
-
.capital_word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is capital word case.
-
.capitalize(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to capital.
-
.capitalized?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a capital letter.
-
.case(string, allow_prefixed_underscores: true) ⇒ Symbol?
Get type of case of string (one key of LuckyCase.CASES).
-
.cases(string, allow_prefixed_underscores: true) ⇒ Array<Symbol>?
Get types of cases of string (keys of LuckyCase.CASES).
-
.constantize(string) ⇒ Constant
Convert the string from any case into pascal case and casts it into a constant.
-
.convert_case(string, case_type, preserve_prefixed_underscores: true) ⇒ String
Convert a string into the given case type.
-
.cut_underscores_at_start(string) ⇒ String
Return string without underscores at the start.
-
.dash_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into dash case.
-
.dash_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is dash case.
-
.decapitalize(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to lower case.
-
.decapitalized?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a lower letter.
-
.deconstantize(constant, target: :path, case_type: nil) ⇒ String
Deconverts the constant back into specified target type.
-
.lower_case(string) ⇒ String
Convert all characters inside the string into lower case.
-
.lower_case?(string) ⇒ Boolean
Check if all characters inside the string are lower case.
-
.mixed_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into mixed case.
-
.mixed_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is a valid mixed case (without special characters!).
-
.not_capital?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a lower letter.
-
.pascal_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into pascal case.
-
.pascal_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper pascal case.
-
.sentence_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into sentence case.
-
.sentence_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is sentence case.
-
.snake_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into snake case.
-
.snake_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is snake case.
-
.split_case_string(string) ⇒ Array<String>
Split the string into parts It is splitted by all (different) case separators.
-
.swap_case(string, preserve_prefixed_underscores: false) ⇒ String
Swaps character cases in string.
-
.train_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into train case.
-
.train_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is train case.
-
.underscores_at_start(string) ⇒ String
Return the underscores at the start of the string.
-
.upper_case(string) ⇒ String
Convert all characters inside the string into upper case.
-
.upper_case?(string) ⇒ Boolean
Check if all characters inside the string are upper case.
-
.upper_dash_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper dash case.
-
.upper_dash_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper dash case.
-
.upper_snake_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper snake case.
-
.upper_snake_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper snake case.
-
.upper_word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper word case.
-
.upper_word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper word case.
-
.valid_case_string?(string) ⇒ Boolean
Check if the string matches any of the available cases.
-
.valid_case_type?(case_type) ⇒ Boolean
Check if given case type is a valid case type.
-
.word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into word case.
-
.word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is word case.
Class Method Details
.camel_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into camel case
292 293 294 295 296 297 298 299 300 |
# File 'lib/lucky_case.rb', line 292 def self.camel_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = ([a[0]] + a[1..-1].map { |e| capital e }).join('') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.camel_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is camel case
307 308 309 310 311 312 313 314 |
# File 'lib/lucky_case.rb', line 307 def self.camel_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :camel_case end |
.capital(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to capital
567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'lib/lucky_case.rb', line 567 def self.capital(string, skip_prefixed_underscores: false) return string if string.empty? s = if skip_prefixed_underscores cut_underscores_at_start string else string end s = s[0].upcase + s[1..-1] if skip_prefixed_underscores underscores_at_start(string) + s else s end end |
.capital?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a capital letter
596 597 598 599 600 601 602 603 |
# File 'lib/lucky_case.rb', line 596 def self.capital?(string, skip_prefixed_underscores: false) s = if skip_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :capital end |
.capital_word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into capital word case
497 498 499 500 501 502 503 504 505 |
# File 'lib/lucky_case.rb', line 497 def self.capital_word_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.map { |e| capital e }.join(' ') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.capital_word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is capital word case
512 513 514 515 516 517 518 519 |
# File 'lib/lucky_case.rb', line 512 def self.capital_word_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :capital_word_case end |
.capitalize(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to capital
587 588 589 |
# File 'lib/lucky_case.rb', line 587 def self.capitalize(string, skip_prefixed_underscores: false) capital string, skip_prefixed_underscores: skip_prefixed_underscores end |
.capitalized?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a capital letter
610 611 612 |
# File 'lib/lucky_case.rb', line 610 def self.capitalized?(string, skip_prefixed_underscores: false) capital? string, skip_prefixed_underscores: skip_prefixed_underscores end |
.case(string, allow_prefixed_underscores: true) ⇒ Symbol?
Get type of case of string (one key of LuckyCase.CASES)
If more than one case matches, the first match wins. Match prio is the order of the regex in LuckyCase.CASES
If you want or need to know all cases, use plural version of this method
If you want to check explicitly for one case, use its check method, e.g. snake_case? for snake_case, etc…
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lucky_case.rb', line 47 def self.case(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end CASES.each do |case_type, regex| if s =~ regex return case_type end end nil end |
.cases(string, allow_prefixed_underscores: true) ⇒ Array<Symbol>?
Get types of cases of string (keys of LuckyCase.CASES)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lucky_case.rb', line 66 def self.cases(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end matched_cases = [] CASES.each do |case_type, regex| if s =~ regex matched_cases.push case_type end end if matched_cases.empty? nil elsif matched_cases.size > 1 # reject :mixed_case if there are other matches # because it would always be included if one other case matches matched_cases.reject { |e| e == :mixed_case } else matched_cases end end |
.constantize(string) ⇒ Constant
Convert the string from any case into pascal case and casts it into a constant
755 756 757 758 759 760 761 762 763 764 |
# File 'lib/lucky_case.rb', line 755 def self.constantize(string) s = string.gsub('/', '::') constants = if s.include? '::' s.split('::') else [s] end final_constant = constants.map { |e| pascal_case(e, preserve_prefixed_underscores: false) }.reject(&:empty?).join('::') Object.const_get(final_constant) end |
.convert_case(string, case_type, preserve_prefixed_underscores: true) ⇒ String
Convert a string into the given case type
95 96 97 98 99 100 101 102 |
# File 'lib/lucky_case.rb', line 95 def self.convert_case(string, case_type, preserve_prefixed_underscores: true) type = case_type.to_sym if CASES.include? case_type.to_sym return self.send type.to_s, string, preserve_prefixed_underscores: preserve_prefixed_underscores end = "Invalid case type '#{case_type}'. Valid types are: #{CASES.keys.join(', ')}" raise InvalidCaseError.new end |
.cut_underscores_at_start(string) ⇒ String
Return string without underscores at the start
808 809 810 811 812 813 814 815 816 817 818 |
# File 'lib/lucky_case.rb', line 808 def self.cut_underscores_at_start(string) underscore_counter = 0 string.split('').each do |c| if c == '_' underscore_counter += 1 else break end end string[underscore_counter..-1] end |
.dash_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into dash case
329 330 331 332 333 334 335 336 337 |
# File 'lib/lucky_case.rb', line 329 def self.dash_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.join('-') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.dash_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is dash case
344 345 346 347 348 349 350 351 |
# File 'lib/lucky_case.rb', line 344 def self.dash_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :dash_case end |
.decapitalize(string, skip_prefixed_underscores: false) ⇒ String
Convert the first character to lower case
619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/lucky_case.rb', line 619 def self.decapitalize(string, skip_prefixed_underscores: false) return string if string.empty? s = if skip_prefixed_underscores cut_underscores_at_start string else string end s = s[0].downcase + s[1..-1] if skip_prefixed_underscores underscores_at_start(string) + s else s end end |
.decapitalized?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a lower letter
648 649 650 |
# File 'lib/lucky_case.rb', line 648 def self.decapitalized?(string, skip_prefixed_underscores: false) !(capital? string, skip_prefixed_underscores) end |
.deconstantize(constant, target: :path, case_type: nil) ⇒ String
Deconverts the constant back into specified target type
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/lucky_case.rb', line 775 def self.deconstantize(constant, target: :path, case_type: nil) # defaults if none is set, depending on target unless case_type case_type = case target.to_sym when :path :snake_case when :string :pascal_case end end s = constant.name split = if s.include? '::' s.split('::') else [s] end reconverted_parts = split.map { |e| convert_case e, case_type } case target.to_sym when :path reconverted_parts.join('/') when :string reconverted_parts.join('::') end end |
.lower_case(string) ⇒ String
Convert all characters inside the string into lower case
160 161 162 |
# File 'lib/lucky_case.rb', line 160 def self.lower_case(string) string.downcase end |
.lower_case?(string) ⇒ Boolean
Check if all characters inside the string are lower case
168 169 170 |
# File 'lib/lucky_case.rb', line 168 def self.lower_case?(string) string == lower_case(string) end |
.mixed_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into mixed case.
The new string is ensured to be different from the input.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
# File 'lib/lucky_case.rb', line 667 def self.mixed_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = nil loop do converted = '' a.each do |part| converted += self.convert_case part, CASES.keys.sample, preserve_prefixed_underscores: preserve_prefixed_underscores end converted = self.convert_case converted, CASES.keys.sample, preserve_prefixed_underscores: preserve_prefixed_underscores break if converted != string && underscores_at_start(string) + converted != string end if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.mixed_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is a valid mixed case (without special characters!)
689 690 691 692 693 694 695 696 |
# File 'lib/lucky_case.rb', line 689 def self.mixed_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :mixed_case end |
.not_capital?(string, skip_prefixed_underscores: false) ⇒ Boolean
Check if the strings first character is a lower letter
639 640 641 |
# File 'lib/lucky_case.rb', line 639 def self.not_capital?(string, skip_prefixed_underscores: false) decapitalized? string, skip_prefixed_underscores end |
.pascal_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into pascal case
255 256 257 258 259 260 261 262 263 |
# File 'lib/lucky_case.rb', line 255 def self.pascal_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.map { |e| capital e }.join('') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.pascal_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper pascal case
270 271 272 273 274 275 276 277 |
# File 'lib/lucky_case.rb', line 270 def self.pascal_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :pascal_case end |
.sentence_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into sentence case
534 535 536 537 538 539 540 541 542 |
# File 'lib/lucky_case.rb', line 534 def self.sentence_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = capital(a.join(' ')) if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.sentence_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is sentence case
549 550 551 552 553 554 555 556 |
# File 'lib/lucky_case.rb', line 549 def self.sentence_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :sentence_case end |
.snake_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into snake case
185 186 187 188 189 190 191 192 193 |
# File 'lib/lucky_case.rb', line 185 def self.snake_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.join('_') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.snake_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is snake case
200 201 202 203 204 205 206 207 |
# File 'lib/lucky_case.rb', line 200 def self.snake_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :snake_case end |
.split_case_string(string) ⇒ Array<String>
Split the string into parts It is splitted by all (different) case separators
841 842 843 844 845 846 847 848 |
# File 'lib/lucky_case.rb', line 841 def self.split_case_string(string) s = cut_underscores_at_start string s = s.gsub(/([[:upper:]])/, '_\1') unless upper_case? s # prepend all upper characters with underscore s = s.gsub(' ', '_') # replace all spaces with underscore s = s.gsub('-', '_') # replace all dashes with underscore s = cut_underscores_at_start s s.downcase.split('_').reject(&:empty?) # split everything by underscore end |
.swap_case(string, preserve_prefixed_underscores: false) ⇒ String
Swaps character cases in string
lower case to upper case upper case to lower case dash to underscore underscore to dash
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/lucky_case.rb', line 715 def self.swap_case(string, preserve_prefixed_underscores: false) s = if preserve_prefixed_underscores cut_underscores_at_start string else string end sp = s.split('') sp.each_with_index do |char, i| if char == '_' sp[i] = '-' elsif char == '-' sp[i] = '_' elsif lower_case? char sp[i] = upper_case char elsif upper_case? char sp[i] = lower_case char end end sp = sp.join('') if preserve_prefixed_underscores underscores_at_start(string) + sp else sp end end |
.train_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into train case
394 395 396 397 398 399 400 401 402 |
# File 'lib/lucky_case.rb', line 394 def self.train_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.map { |e| capital e }.join('-') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.train_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is train case
409 410 411 412 413 414 415 416 |
# File 'lib/lucky_case.rb', line 409 def self.train_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :train_case end |
.underscores_at_start(string) ⇒ String
Return the underscores at the start of the string
824 825 826 827 828 829 830 831 832 833 834 |
# File 'lib/lucky_case.rb', line 824 def self.underscores_at_start(string) underscore_counter = 0 string.split('').each do |c| if c == '_' underscore_counter += 1 else break end end '_' * underscore_counter end |
.upper_case(string) ⇒ String
Convert all characters inside the string into upper case
136 137 138 |
# File 'lib/lucky_case.rb', line 136 def self.upper_case(string) string.upcase end |
.upper_case?(string) ⇒ Boolean
Check if all characters inside the string are upper case
144 145 146 |
# File 'lib/lucky_case.rb', line 144 def self.upper_case?(string) string == upper_case(string) end |
.upper_dash_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper dash case
362 363 364 365 |
# File 'lib/lucky_case.rb', line 362 def self.upper_dash_case(string, preserve_prefixed_underscores: true) s = dash_case string, preserve_prefixed_underscores: preserve_prefixed_underscores upper_case s end |
.upper_dash_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper dash case
372 373 374 375 376 377 378 379 |
# File 'lib/lucky_case.rb', line 372 def self.upper_dash_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :upper_dash_case end |
.upper_snake_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper snake case
218 219 220 221 222 223 224 225 226 |
# File 'lib/lucky_case.rb', line 218 def self.upper_snake_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.map { |e| upper_case e }.join('_') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.upper_snake_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper snake case
233 234 235 236 237 238 239 240 |
# File 'lib/lucky_case.rb', line 233 def self.upper_snake_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :upper_snake_case end |
.upper_word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into upper word case
464 465 466 467 468 469 470 471 472 |
# File 'lib/lucky_case.rb', line 464 def self.upper_word_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.map { |e| upper_case e }.join(' ') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.upper_word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is upper word case
479 480 481 482 483 484 485 486 |
# File 'lib/lucky_case.rb', line 479 def self.upper_word_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :upper_word_case end |
.valid_case_string?(string) ⇒ Boolean
Check if the string matches any of the available cases
120 121 122 |
# File 'lib/lucky_case.rb', line 120 def self.valid_case_string?(string) self.case(string) != nil end |
.valid_case_type?(case_type) ⇒ Boolean
Check if given case type is a valid case type
108 109 110 111 112 113 114 |
# File 'lib/lucky_case.rb', line 108 def self.valid_case_type?(case_type) if CASES.keys.include? case_type.to_sym true else false end end |
.word_case(string, preserve_prefixed_underscores: true) ⇒ String
Convert the given string from any case into word case
431 432 433 434 435 436 437 438 439 |
# File 'lib/lucky_case.rb', line 431 def self.word_case(string, preserve_prefixed_underscores: true) a = split_case_string string converted = a.join(' ') if preserve_prefixed_underscores underscores_at_start(string) + converted else converted end end |
.word_case?(string, allow_prefixed_underscores: true) ⇒ Boolean
Check if the string is word case
446 447 448 449 450 451 452 453 |
# File 'lib/lucky_case.rb', line 446 def self.word_case?(string, allow_prefixed_underscores: true) s = if allow_prefixed_underscores cut_underscores_at_start string else string end _case_match? s, :word_case end |