Class: PgVerify::StringUtil
- Inherits:
-
Object
- Object
- PgVerify::StringUtil
- Defined in:
- lib/pg-verify/core/util.rb
Class Method Summary collapse
- .auto_complete(string, options) ⇒ Object
- .indented(string, num_indents: 1, indent_string: "\t") ⇒ Object
- .levenshtein_distance(a, b) ⇒ Object
- .levenshtein_suggest(string, options, suggestions: 5) ⇒ Object
- .limit_width(string, width) ⇒ Object
- .line_combine(string1, string2, separator: " ") ⇒ Object
- .make_unique(string, strings, &blk) ⇒ Object
- .shorten_unique(strings) ⇒ Object
Class Method Details
.auto_complete(string, options) ⇒ Object
70 71 72 73 74 |
# File 'lib/pg-verify/core/util.rb', line 70 def self.auto_complete(string, ) perfect_match = .select { |o| o == string }.uniq return perfect_match unless perfect_match.empty? .select { |o| o.start_with?(string) }.uniq end |
.indented(string, num_indents: 1, indent_string: "\t") ⇒ Object
128 129 130 |
# File 'lib/pg-verify/core/util.rb', line 128 def self.indented(string, num_indents: 1, indent_string: "\t") string.split("\n").map { |l| "#{indent_string * num_indents}#{l}" }.join("\n") end |
.levenshtein_distance(a, b) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/pg-verify/core/util.rb', line 132 def self.levenshtein_distance(a, b) a, b = a.downcase, b.downcase costs = Array(0..b.length) # i == 0 (1..a.length).each do |i| costs[0], nw = i, i - 1 # j == 0; nw is lev(i-1, j) (1..b.length).each do |j| costs[j], nw = [costs[j] + 1, costs[j-1] + 1, a[i-1] == b[j-1] ? nw : nw + 1].min, costs[j] end end costs[b.length] end |
.levenshtein_suggest(string, options, suggestions: 5) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/pg-verify/core/util.rb', line 76 def self.levenshtein_suggest(string, , suggestions: 5) .map { |o| [o, levenshtein_distance(string, o)] } .sort_by{ |a| a[1] } .vsu_limit(suggestions) .map { |a| a[0] } end |
.limit_width(string, width) ⇒ Object
65 66 67 68 |
# File 'lib/pg-verify/core/util.rb', line 65 def self.limit_width(string, width) return string if string.nil? || string.length <= width return string.chars.each_slice(width).map(&:join).join("\n") end |
.line_combine(string1, string2, separator: " ") ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pg-verify/core/util.rb', line 83 def self.line_combine(string1, string2, separator: " ") return string2 if string1.empty? lines1, lines2 = string1.split("\n"), string2.split("\n") both = [lines1, lines2] height = both.map(&:length).max l_width = lines1.map(&:display_length).max # Fill up empty lines to match height both.each { |lines| loop { break if lines.length >= height; lines << "" } } # Fill up left lines to align right side lines1 = lines1.map { |l| l + " " * (l_width - l.display_length) } # Combine left and right. string = (0...height).map { |index| lines1[index] + separator + lines2[index] }.join("\n") return string end |
.make_unique(string, strings, &blk) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/pg-verify/core/util.rb', line 55 def self.make_unique(string, strings, &blk) base_string = string index = 0 while strings.include?(string) index += 1 string = blk.call(base_string, index) end string end |
.shorten_unique(strings) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/pg-verify/core/util.rb', line 104 def self.shorten_unique(strings) # TODO: Implement return strings.each_with_index.map { |s, i| [s, i.to_s] }.to_h # chars = ".- _".chars # regex = /#{chars.map { |c| "\\#{c}" }.join("|")}/ # map = {} # strings.each do |str| # index = 0 # loop do # split = str.gsub(regex, " ").split # short = split.map { |word| word[0, index] }.join("") # puts short # sleep(1) # next if map.values.include?(short) # map[str] = short # break # end # end # map end |