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

Class Method Details

.camel_case(string, preserve_prefixed_underscores: true) ⇒ String

Convert the given string from any case into camel case

Examples:

conversion

'this-isAnExample_string' => 'thisIsAnExampleString'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

  • string (String)

    to convert

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:



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

Parameters:

  • string (String)

    to check

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'This Is An Example String'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

  • string (String)

    to convert

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:



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

Parameters:

  • string (String)

    to check

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:

  • (Boolean)


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…

Parameters:

  • string (String)
  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Symbol, nil)

    symbol of type, nil if no match



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)

Parameters:

  • string (String)
  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Array<Symbol>, nil)

    symbols of types, nil if no one matches



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

Examples:

conversion

'this-isAnExample_string' => ThisIsAnExampleString
'this/is_an/example_path' => This::IsAn::ExamplePath

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean)

Returns:

  • (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

Parameters:

  • string (String)
  • case_type (Symbol, String)
  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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
  error_message = "Invalid case type '#{case_type}'. Valid types are: #{CASES.keys.join(', ')}"
  raise InvalidCaseError.new error_message
end

.cut_underscores_at_start(string) ⇒ String

Return string without underscores at the start

Parameters:

Returns:

  • (String)

    string without prefixed underscores



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

Examples:

conversion

'this-isAnExample_string' => 'this-is-an-example-string'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

  • string (String)

    to convert

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:



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

Parameters:

  • string (String)

    to check

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:

  • (Boolean)


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

Examples:

deconversion

This::AweSome::Constant => 'this/awe_some/constant'

Parameters:

  • constant (Constant)

    to reconvert

  • target (Symbol, String) (defaults to: :path)

    can be :path, :string

  • case_type (Symbol, String) (defaults to: nil)

    can be any valid case of CASES

Returns:



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

Examples:

conversion

'this-isAnExample_string' => 'this-isanexample_string'

Parameters:

  • string (String)

    to convert

Returns:



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

Parameters:

  • string (String)

    to check

Returns:

  • (Boolean)


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.

Examples:

conversion

'this-isAnExample_string' => 'This-Is_anExample-string'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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!)

Parameters:

  • string (String)

    to check

Returns:

  • (Boolean)


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

Parameters:

  • string (String)

    to check

  • skip_prefixed_underscores (Boolean) (defaults to: false)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'ThisIsAnExampleString'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'This is an example string'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'this_is_an_example_string'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

Returns:



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

Examples:

conversion

'this-isAnExample_string' => 'THIS_ISaNeXAMPLE-STRING'

Parameters:

  • string (String)
  • preserve_prefixed_underscores (Boolean) (defaults to: false)

Returns:



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

Examples:

conversion

'this-isAnExample_string' => 'This-Is-An-Example-String'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

Returns:

  • (String)

    string of underscores or empty if none found



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

Examples:

conversion

'this-isAnExample_string' => 'THIS-ISANEXAMPLE_STRING'

Parameters:

  • string (String)

    to convert

Returns:



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

Parameters:

  • string (String)

    to check

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'THIS-IS-AN-EXAMPLE-STRING'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'THIS_IS_AN_EXAMPLE_STRING'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'THIS IS AN EXAMPLE STRING'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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

Parameters:

Returns:

  • (Boolean)


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

Parameters:

Returns:

  • (Boolean)


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

Examples:

conversion

'this-isAnExample_string' => 'this is an example string'

Parameters:

  • string (String)

    to convert

  • preserve_prefixed_underscores (Boolean) (defaults to: true)

Returns:



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

Parameters:

  • string (String)

    to check

  • allow_prefixed_underscores (Boolean) (defaults to: true)

Returns:

  • (Boolean)


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