Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_foundation/case.rb,
lib/string_foundation/like.rb,
lib/string_foundation/with.rb,
lib/string_foundation/convert.rb,
lib/string_foundation/convertible.rb

Overview

LIB - STRING FOUNDATION - WITH

frozen_string_literal: true

Instance Method Summary collapse

Instance Method Details

#is_lower?Boolean

Whether a character is lower case or not.

Returns:

  • (Boolean)


93
94
95
# File 'lib/string_foundation/case.rb', line 93

def is_lower?
  /[[:lower:]]/.match(self) ? true : false
end

#is_upper?Boolean

Whether a character is uppder case or not.

Returns:

  • (Boolean)


88
89
90
# File 'lib/string_foundation/case.rb', line 88

def is_upper?
  /[[:upper:]]/.match(self) ? true : false
end

#like_f?Boolean

Check whether a string is a floating point number.

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/string_foundation/like.rb', line 17

def like_f?
  return false unless self.to_f?

  num = self.without_leading_zeros
  (num.to_i != num.to_f) || num.include?('.')
end

#like_i?Boolean

Check whether a string is an integral number.

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/string_foundation/like.rb', line 9

def like_i?
  return false unless self.to_i?

  num = self.without_leading_zeros
  (num.to_i == num.to_i) && !num.include?('.')
end

#make_head_lowerObject

Make first character lower case.



98
99
100
# File 'lib/string_foundation/case.rb', line 98

def make_head_lower
  self[0].downcase + self[1..-1]
end

#nl_to(char) ⇒ Object Also known as: nl2

Convert from newline character to specific characters.



39
40
41
42
# File 'lib/string_foundation/convert.rb', line 39

def nl_to(char)
  char = '' if char.nil?
  self.gsub(/(\r\n|\n)/, char)
end

#nl_to_brObject Also known as: nl2br

Convert from newline character to a HTML tag “
”.



45
46
47
# File 'lib/string_foundation/convert.rb', line 45

def nl_to_br
  self.nl_to('<br>')
end

#split_camelObject

Split string according to camel case.



83
84
85
# File 'lib/string_foundation/case.rb', line 83

def split_camel
  self.split /(?=[A-Z])/
end

#to_boolObject

Convert to TrueClass or FalseClass.



11
12
13
14
15
16
17
# File 'lib/string_foundation/convert.rb', line 11

def to_bool
  unless self.to_bool?
    raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass")
  end

  (self == 'true')
end

#to_bool?Boolean

Whether or not to be possible to covert String to Boolean.

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/string_foundation/convertible.rb', line 25

def to_bool?
  return true if %w(true false).include?(self)
  false
end

#to_boolyObject

Convert a booly string to TrueClass or FalseClass.



20
21
22
23
24
25
26
27
# File 'lib/string_foundation/convert.rb', line 20

def to_booly
  unless self.to_booly?
    raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass")
  end

  return true if self == 'true'  || (self.to_f? && self.to_f > 0)
  false
end

#to_booly?Boolean

Whether or not to be possible to covert String to something which behaves like boolean types.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/string_foundation/convertible.rb', line 32

def to_booly?
  return true if self.length == 0
  return true if %w(true false).include?(self)
  return true if self.to_f?

  false
end

#to_f?Boolean

Whether or not to be possible to covert String to Float.

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/string_foundation/convertible.rb', line 17

def to_f?
  Float(self.without_leading_zeros)
  true
rescue ArgumentError
  false
end

#to_i?Boolean

Whether or not to be possible to covert String to Integer.

Returns:

  • (Boolean)


9
10
11
12
13
14
# File 'lib/string_foundation/convertible.rb', line 9

def to_i?
  Integer(Float(self.without_leading_zeros))
  true
rescue ArgumentError
  false
end

#to_lcamelObject

Convert to lowerCamelCase.



8
9
10
11
# File 'lib/string_foundation/case.rb', line 8

def to_lcamel
  ucamel = self.to_ucamel
  ucamel.make_head_lower
end

#to_ldotObject

Convert to lower.dot.case.



68
69
70
71
# File 'lib/string_foundation/case.rb', line 68

def to_ldot
  udot = self.to_udot
  udot.downcase
end

#to_lkebabObject

Convert to lower-kebab-case.



38
39
40
41
# File 'lib/string_foundation/case.rb', line 38

def to_lkebab
  ukebab = self.to_ukebab
  ukebab.downcase
end

#to_lsnakeObject

Convert to lower_snake_case.



23
24
25
26
# File 'lib/string_foundation/case.rb', line 23

def to_lsnake
  usnake = self.to_usnake
  usnake.downcase
end

#to_lspaceObject

Convert to lower space case.



53
54
55
56
# File 'lib/string_foundation/case.rb', line 53

def to_lspace
  uspace = self.to_uspace
  uspace.downcase
end

#to_prettyObject

Convert to a pretty value.



30
31
32
33
34
35
36
# File 'lib/string_foundation/convert.rb', line 30

def to_pretty
  return self.without_leading_zeros.to_i if self.like_i?
  return self.without_leading_zeros.to_f if self.like_f?
  return self.to_bool                    if self.to_bool?

  (self.length > 0) ? self : nil
end

#to_ucamelObject

Convert to UpperCamelCase.



14
15
16
17
18
19
20
# File 'lib/string_foundation/case.rb', line 14

def to_ucamel
  self.split_camel.map do |cw|
    cw.split(/\.|_|-|\s/).map do |w|
      w.capitalize
    end.join
  end.join
end

#to_udotObject

Convert to Upper.Dot.Case.



74
75
76
77
78
79
80
# File 'lib/string_foundation/case.rb', line 74

def to_udot
  self.split_camel.map do |cw|
    cw.split(/\.|_|-|\s/).map do |w|
      w.capitalize
    end.join('.')
  end.join('.')
end

#to_ukebabObject

Convert to Upper-Kebab-Case.



44
45
46
47
48
49
50
# File 'lib/string_foundation/case.rb', line 44

def to_ukebab
  self.split_camel.map do |cw|
    cw.split(/\.|_|-|\s/).map do |w|
      w.capitalize
    end.join('-')
  end.join('-')
end

#to_usnakeObject

Convert to Upper_Snake_Case.



29
30
31
32
33
34
35
# File 'lib/string_foundation/case.rb', line 29

def to_usnake
  self.split_camel.map do |cw|
    cw.split(/\.|_|-|\s/).map do |w|
      w.capitalize
    end.join('_')
  end.join('_')
end

#to_uspaceObject

Convert to Upper Space Case.



59
60
61
62
63
64
65
# File 'lib/string_foundation/case.rb', line 59

def to_uspace
  self.split_camel.map do |cw|
    cw.split(/\.|_|-|\s/).map do |w|
      w.capitalize
    end.join(' ')
  end.join(' ')
end

#without_leading_zerosObject Also known as: without_zero_pad

Remove leading zeros.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/string_foundation/with.rb', line 8

def without_leading_zeros
  return self if self == '0'

  is_positive = self.start_with?('0')
  is_negative = self.start_with?('-0')
  if is_positive || is_negative
    sig = self[0, self.length].gsub(/(^0+)|(^-0+)/, '')

    sig = '0' + sig if sig.start_with?('.') || sig.length == 0
    sig = '-' + sig if is_negative && sig != '0'

    sig
  else
    self
  end
end