Module: Utilities::Strings

Defined in:
lib/utilities/strings.rb

Overview

Methods that recieve or generate a String. This methods in this library should be completely independant (i.e. ultimately gemifiable) from TaxonWorks.

Class Method Summary collapse

Class Method Details

.alphabetic_strings(string) ⇒ Array

Returns whitespace split, then any string containing a digit eliminated.

Parameters:

  • string (String)

Returns:

  • (Array)

    whitespace split, then any string containing a digit eliminated


102
103
104
105
# File 'lib/utilities/strings.rb', line 102

def self.alphabetic_strings(string)
  return [] if string.nil? || string.length == 0
  string.split(/\W/).select{|b| !(b =~ /\d/) }
end

.authorship_sentence(last_names = []) ⇒ String?

TODO: DEPRECATE (doesn't belong here because to_sentence is Rails?

Parameters:

  • last_names (Array) (defaults to: [])

Returns:

  • (String, nil)

94
95
96
97
# File 'lib/utilities/strings.rb', line 94

def self.authorship_sentence(last_names = [])
  return nil if last_names.empty?
  last_names.to_sentence(two_words_connector: ' & ', last_word_connector: ' & ')
end

.encode_with_utf8(string) ⇒ String, false

Returns !! this is a bad sign, you should know your encoding before it gets to needing this.

Parameters:

  • string (String)

Returns:

  • (String, false)

    !! this is a bad sign, you should know your encoding before it gets to needing this


111
112
113
114
115
116
117
118
# File 'lib/utilities/strings.rb', line 111

def self.encode_with_utf8(string)
  return false if string.nil?
  if Encoding.compatible?('test'.encode(Encoding::UTF_8), string) 
    string.force_encoding(Encoding::UTF_8)
  else
    false
  end
end

.escape_single_quote(string) ⇒ String

Adds a second single quote to escape apostrophe in SQL query strings

Parameters:

  • string (String)

Returns:

  • (String)

56
57
58
59
# File 'lib/utilities/strings.rb', line 56

def self.escape_single_quote(string)
  return nil if string.blank?
  string.gsub("'", "''")
end

.generate_md5(text) ⇒ Digest::MD5

Parameters:

  • text (String)

Returns:

  • (Digest::MD5)

36
37
38
39
40
# File 'lib/utilities/strings.rb', line 36

def self.generate_md5(text)
  return nil if text.blank?
  text = text.downcase.gsub(/[\s\.,;:\?!]*/, '')
  Digest::MD5.hexdigest(text)
end

.increment_contained_integer(string) ⇒ String

Returns increments the first integer encountered in the string, wrapping it in only the immediate non integer strings before and after (see tests).

Parameters:

  • string (String)

Returns:

  • (String)

    increments the first integer encountered in the string, wrapping it in only the immediate non integer strings before and after (see tests)


46
47
48
49
50
51
# File 'lib/utilities/strings.rb', line 46

def self.increment_contained_integer(string)
  string =~ /([^\d]*)(\d+)([^\d]*)/
  a, b, c = $1, $2, $3
  return false if b.nil?
  [a,(b.to_i + 1), c].compact.join
end

.integers(string) ⇒ Array

Returns of strings representing integers.

Returns:

  • (Array)

    of strings representing integers


135
136
137
138
# File 'lib/utilities/strings.rb', line 135

def self.integers(string)
  return [] if string.nil? || string.length == 0
  string.split(/\s+/).select{|t| is_i?(t)}
end

.is_i?(string) ⇒ Boolean

see stackoverflow.com/questions/1235863/test-if-a-string-is-basically-an-integer-in-quotes-using-ruby Note: Might checkout CSV::Converters constants to see how they handle this Allows '02' … hmm

Parameters:

  • string (String)

Returns:

  • (Boolean)

    whether the string is an integer (positive or negative)


67
68
69
# File 'lib/utilities/strings.rb', line 67

def self.is_i?(string)
  /\A[-+]?\d+\z/ === string
end

.nil_squish_strip(string) ⇒ String?

Returns strips pre/post fixed space and condenses internal spaces, but returns nil (not empty string) if nothing is left.

Parameters:

  • string (String)

Returns:

  • (String, nil)

    strips pre/post fixed space and condenses internal spaces, but returns nil (not empty string) if nothing is left


26
27
28
29
30
31
32
# File 'lib/utilities/strings.rb', line 26

def self.nil_squish_strip(string)
  if !string.nil?
    string.squish!
    string = nil if string == ''
  end
  string
end

.nil_strip(string) ⇒ String?

Returns strips space, leaves internal whitespace as is, returns nil if nothing is left.

Parameters:

  • string (String)

Returns:

  • (String, nil)

    strips space, leaves internal whitespace as is, returns nil if nothing is left


15
16
17
18
19
20
21
# File 'lib/utilities/strings.rb', line 15

def self.nil_strip(string) # string should have content or be empty
  if !string.nil?
    string.strip!
    string = nil if string == ''
  end
  string
end

.nil_wrap(pre = nil, content = nil, post = nil) ⇒ String

return nil if content.nil?, else wrap and return string if provided

Parameters:

  • pre (String) (defaults to: nil)
  • content (String) (defaults to: nil)
  • post (String) (defaults to: nil)

Returns:

  • (String)

86
87
88
89
# File 'lib/utilities/strings.rb', line 86

def self.nil_wrap(pre = nil, content = nil, post = nil)
  return nil if content.blank?
  [pre, content, post].compact.join.html_safe
end

.only_integers?(string) ⇒ Boolean

Returns true if the query string only contains integers.

Returns:

  • (Boolean)

    true if the query string only contains integers


142
143
144
# File 'lib/utilities/strings.rb', line 142

def self.only_integers?(string)
  !(string =~ /[^\d\s]/i) && !integers(string).empty?
end

.random_string(string_length) ⇒ String?

Returns stub a string of a certain length.

Parameters:

  • string_length (Integer)

Returns:

  • (String, nil)

    stub a string of a certain length


7
8
9
10
# File 'lib/utilities/strings.rb', line 7

def self.random_string(string_length)
  return nil if string_length.to_i == 0
  ('a'..'z').to_a.shuffle[0, string_length].join
end

.sanitize_for_csv(string) ⇒ String, param

Returns the goal is to sanitizie an individual string such that it is usable in TAB delimited, UTF-8, column. See Download TODO: Likely need to handle quotes, and write better UTF compliancy tests ~~ Technically n is allowed!.

Parameters:

  • string (String)

Returns:

  • (String, param)

    the goal is to sanitizie an individual string such that it is usable in TAB delimited, UTF-8, column. See Download TODO: Likely need to handle quotes, and write better UTF compliancy tests ~~ Technically n is allowed!


76
77
78
79
# File 'lib/utilities/strings.rb', line 76

def self.sanitize_for_csv(string)
  return string if string.blank?
  string.to_s.gsub(/\n|\t/, ' ')
end

.year_letter(string) ⇒ String?

Returns the immediately following letter recognized as coming directly past the first year

`Smith, 1920a. ... ` returns `a`.

Returns:

  • (String, nil)

    the immediately following letter recognized as coming directly past the first year

    `Smith, 1920a. ... ` returns `a`
    

129
130
131
# File 'lib/utilities/strings.rb', line 129

def self.year_letter(string)
  string.match(/\d{4}([a-zAZ]+)/).to_a.last
end

.years(string) ⇒ Array

Returns:

  • (Array)

121
122
123
124
# File 'lib/utilities/strings.rb', line 121

def self.years(string)
  return [] if string.nil?
  string.scan(/\d{4}/).to_a.uniq
end