Module: Knj::Strings
- Defined in:
- lib/knj/strings.rb
Overview
This module contains various methods to escape, change or treat strings.
Class Method Summary collapse
-
.const_get_full(str) ⇒ Object
Returns the module from the given string - even if formed as SomeClass::SomeNewClass.
-
.email_str_safe(str) ⇒ Object
Email content may only be 1000 characters long.
-
.float_as_human_logic(floatval) ⇒ Object
Returns a float as human locaically readable.
-
.html_links(str, args = {}) ⇒ Object
Search for what looks like links in a string and does something with it based on block given or arguments given.
-
.human_time_str_to_secs(str) ⇒ Object
Turns a human readable time-string into a number of seconds.
-
.is_a?(obj, str) ⇒ Boolean
Same as ‘Class#is_a?’ but takes a string instead of the actual class.
-
.is_email?(str) ⇒ Boolean
Returns boolean if the strings is a correctly formatted email: [email protected].
-
.is_phonenumber?(str) ⇒ Boolean
Returns boolean if the string is a correctly formatted phonenumber as: +4512345678.
-
.is_regex?(str) ⇒ Boolean
Returns true if given string is regex-compatible.
- .js_safe(str, args = {}) ⇒ Object
-
.regex(str) ⇒ Object
Returns a Regexp-object from the string formatted as what you would give to Php’s preg_match.
-
.sanitize_filename(filename) ⇒ Object
Takes a string and converts it to a safe string for filenames.
-
.searchstring(string, &block) ⇒ Object
Partens a string up in blocks for whatever words can be used to search for.
-
.secs_to_human_short_time(secs, args = nil) ⇒ Object
Returns a short time-format for the given amount of seconds.
-
.secs_to_human_time_str(secs, args = nil) ⇒ Object
Returns a human readable time-string from a given number of seconds.
-
.shorten(str, maxlength) ⇒ Object
Shortens a string to maxlength and adds “…” if it was shortened.
-
.strip(origstr, args) ⇒ Object
Strips various given characters from a given string.
-
.unixsafe(string) ⇒ Object
Alias for UnixSafe.
-
.UnixSafe(tha_string) ⇒ Object
Returns a string that is safe to use on the command line.
-
.yn_str(value, str_yes = "Yes", str_no = "No") ⇒ Object
Returns ‘Yes’ or ‘No’ based on a value.
Class Method Details
.const_get_full(str) ⇒ Object
Returns the module from the given string - even if formed as SomeClass::SomeNewClass.
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/knj/strings.rb', line 218 def self.const_get_full(str) raise "Invalid object: '#{str.class.name}'." if !str.is_a?(String) and !str.is_a?(Symbol) module_use = Kernel str.to_s.scan(/(.+?)(::|$)/) do |match| module_use = module_use.const_get(match[0]) end return module_use end |
.email_str_safe(str) ⇒ Object
Email content may only be 1000 characters long. This method shortens them gracefully.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/knj/strings.rb', line 230 def self.email_str_safe(str) str = str.to_s strcopy = "#{str}" str.each_line("\n") do |substr_orig| substr = "#{substr_orig}" next if substr.length <= 1000 lines = [] while substr.length > 1000 do whitespace_index = substr.rindex(/\s/, 1000) if whitespace_index == nil lines << substr.slice(0, 1000) substr = substr.slice(1000, substr.length) else lines << substr.slice(0, whitespace_index + 1) substr = substr.slice(whitespace_index + 1, substr.length) end end lines << substr strcopy.gsub!(/^#{Regexp.escape(substr_orig)}$/, lines.join("\n")) end return strcopy end |
.float_as_human_logic(floatval) ⇒ Object
Returns a float as human locaically readable. 1.0 will be 1, 1.5 will be 1.5 and so on.
261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/knj/strings.rb', line 261 def self.float_as_human_logic(floatval) raise "Not a float." if !floatval.is_a?(Float) float_s = floatval.to_s parts = float_s.split(".") if parts[1].to_i > 0 return float_s else return parts[0].to_s end end |
.html_links(str, args = {}) ⇒ Object
Search for what looks like links in a string and does something with it based on block given or arguments given.
Examples
str = “asd asd asd asdjklqwejqwer www.google.com asdfas df asf” Knj::Strings.html_links(str) #=> “asd asd asd asdjklqwejqwer <a href="www.google.com">www.google.com</a> asdfas df asf” Knj::Strings.html_links(str){ |data| str.gsub(data[0], “!!!#[1]!!!”)} #=> “asd asd asd asdjklqwejqwer !!!www!!! asdfas df asf”
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/knj/strings.rb', line 153 def self.html_links(str, args = {}) Knj::Web.html(str).scan(/(http:\/\/([A-z]+)\S*\.([A-z]{2,4})(\S+))/) do |match| if block_given? str = yield(:str => str, :args => args, :match => match) else if args["target"] html = "<a target=\"#{args["target"]}\"" else html = "<a" end html << " href=\"#{match[0]}\">#{match[0]}</a>" str = str.gsub(match[0], html) end end return str end |
.human_time_str_to_secs(str) ⇒ Object
Turns a human readable time-string into a number of seconds.
307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/knj/strings.rb', line 307 def self.human_time_str_to_secs(str) match = str.match(/^\s*(\d+)\s*:\s*(\d+)(\s*:\s*(\d+)\s*|)/) raise "Could not match string: '#{str}'." if !match hours = match[1].to_i minutes = match[2].to_i secs = match[4].to_i total = (hours * 3600) + (minutes * 60) + secs return total end |
.is_a?(obj, str) ⇒ Boolean
Same as ‘Class#is_a?’ but takes a string instead of the actual class. Then it doesnt get autoloaded or anything like that. It can also test against an array containing string-class-names.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/knj/strings.rb', line 320 def self.is_a?(obj, str) obj_class = obj.class str = str.to_s if !str.is_a?(Array) loop do if str.is_a?(Array) return true if str.index(obj_class.name.to_s) != nil else return true if obj_class.name.to_s == str end obj_class = obj_class.superclass break if !obj_class end return false end |
.is_email?(str) ⇒ Boolean
Returns boolean if the strings is a correctly formatted email: [email protected].
95 96 97 98 |
# File 'lib/knj/strings.rb', line 95 def self.is_email?(str) return true if str.to_s.match(/^\S+@\S+\.\S+$/) return false end |
.is_phonenumber?(str) ⇒ Boolean
Returns boolean if the string is a correctly formatted phonenumber as: +4512345678.
101 102 103 104 |
# File 'lib/knj/strings.rb', line 101 def self.is_phonenumber?(str) return true if str.to_s.match(/^\+\d{2}\d+$/) return false end |
.is_regex?(str) ⇒ Boolean
Returns true if given string is regex-compatible.
18 19 20 21 22 23 24 |
# File 'lib/knj/strings.rb', line 18 def self.is_regex?(str) if str.to_s.match(/^\/(.+)\/(i|m|x|)$/) return true else return false end end |
.js_safe(str, args = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/knj/strings.rb', line 106 def self.js_safe(str, args = {}) str = "#{str}" if args[:quotes_to_single] str.gsub!('"', "'") end str = str.gsub("\r", "").gsub("\n", "\\n").gsub("'", "\\\\'") if !args.key?(:quotes) or args[:quotes] str.gsub!('"', '\"') end return str end |
.regex(str) ⇒ Object
Returns a Regexp-object from the string formatted as what you would give to Php’s preg_match.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/knj/strings.rb', line 27 def self.regex(str) first_char = str[0, 1] raise "First char should be '/' but wasnt: '#{first_char}'." if first_char != "/" first_pos = 1 second_pos = str.rindex("/") pattern = str[first_pos, second_pos - 1] flags = str[second_pos + 1, str.length].to_s arg_two = 0 if flags flags.length.times do |i| arg = flags[i, 1] case arg when "i" arg_two |= Regexp::IGNORECASE when "m" arg_two |= Regexp::MULTILINE when "x" arg_two |= Regexp::EXTENDED when "U" raise ArgumentError, "Ruby does (as far as I know) not support the 'U'-modifier. You should rewrite your regex with non-greedy operators such as '(\d+?)' instead for: '#{str}'." else raise "Unknown argument: '#{arg}'." end end end return Regexp.new(pattern, arg_two) end |
.sanitize_filename(filename) ⇒ Object
Takes a string and converts it to a safe string for filenames.
339 340 341 |
# File 'lib/knj/strings.rb', line 339 def self.sanitize_filename(filename) return filename.gsub(/[^0-9A-z.\-]/, '_').gsub("\\", "_") end |
.searchstring(string, &block) ⇒ Object
Partens a string up in blocks for whatever words can be used to search for. Supports a block or returns an array.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/knj/strings.rb', line 61 def self.searchstring(string, &block) words = [] if !block string = string.to_s matches = string.scan /(\"(.+?)\")/ matches.each do |matcharr| word = matcharr[1] if word and word.length > 0 if block yield(matcharr[1]) else words << matcharr[1] end string = string.gsub(matcharr[0], "") end end string.split(/\s/).each do |word| if word and word.length > 0 if block yield(word) else words << word end end end return nil if block return words end |
.secs_to_human_short_time(secs, args = nil) ⇒ Object
Returns a short time-format for the given amount of seconds.
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/knj/strings.rb', line 274 def self.secs_to_human_short_time(secs, args = nil) secs = secs.to_i return "#{secs}s" if secs < 60 and (!args or !args.key?(:secs) or args[:secs]) mins = (secs.to_f / 60.0).floor if mins < 60 and (!args or !args.key?(:mins) or args[:mins]) return "#{mins.to_i}m" end hours = (mins.to_f / 60.0) return "#{Knj::Locales.number_out(hours, 1)}t" end |
.secs_to_human_time_str(secs, args = nil) ⇒ Object
Returns a human readable time-string from a given number of seconds.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/knj/strings.rb', line 289 def self.secs_to_human_time_str(secs, args = nil) secs = secs.to_i hours = (secs.to_f / 3600.0).floor.to_i secs = secs - (hours * 3600) mins = (secs.to_f / 60).floor.to_i secs = secs - (mins * 60) str = "#{"%02d" % hours}:#{"%02d" % mins}" if !args or !args.key?(:secs) or args[:secs] str << ":#{"%02d" % secs}" end return str end |
.shorten(str, maxlength) ⇒ Object
Shortens a string to maxlength and adds “…” if it was shortened.
Examples
Knj::Strings.shorten(“Kasper Johansen”, 6) #=> “Kasper…”
142 143 144 145 146 |
# File 'lib/knj/strings.rb', line 142 def self.shorten(str, maxlength) str = str.to_s str = str.slice(0..(maxlength - 1)).strip + "..." if str.length > maxlength return str end |
.strip(origstr, args) ⇒ Object
Strips various given characters from a given string.
Examples
Knj::Strings.strip(“…kasper…”, => false, :left => true, :strips => [“.”, “,”]) #=> “kasper…”
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/knj/strings.rb', line 175 def self.strip(origstr, args) newstr = "#{origstr}" if !args.key?(:right) or args[:right] loop do changed = false args[:strips].each do |str| len = str.length endstr = newstr.slice(-len, len) next if !endstr if endstr == str changed = true newstr = newstr.slice(0..newstr.length-len-1) end end break if !changed end end if !args.key?(:left) or args[:left] loop do changed = false args[:strips].each do |str| len = str.length endstr = newstr.slice(0, len) next if !endstr if endstr == str changed = true newstr = newstr.slice(len..-1) end end break if !changed end end return newstr end |
.unixsafe(string) ⇒ Object
Alias for UnixSafe.
13 14 15 |
# File 'lib/knj/strings.rb', line 13 def self.unixsafe(string) return Knj::Strings.UnixSafe(string) end |
.UnixSafe(tha_string) ⇒ Object
Returns a string that is safe to use on the command line.
8 9 10 |
# File 'lib/knj/strings.rb', line 8 def self.UnixSafe(tha_string) return tha_string.to_s.gsub(" ", "\\ ").gsub("&", "\&").gsub("(", "\\(").gsub(")", "\\)").gsub('"', '\"').gsub("\n", "\"\n\"").gsub(":", "\\:").gsub('\'', "\\\\'").gsub("`", "\\\\`") end |
.yn_str(value, str_yes = "Yes", str_no = "No") ⇒ Object
Returns ‘Yes’ or ‘No’ based on a value. The value can be 0, 1, yes, no, true or false.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/knj/strings.rb', line 123 def self.yn_str(value, str_yes = "Yes", str_no = "No") value = value.to_i if (Float(value) rescue false) value_s = value.to_s if value.is_a?(Integer) if value == 0 return str_no else return str_yes end end return str_no if !value or value_s == "no" or value_s == "false" or value_s == "" return str_yes end |