clir 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,304 @@
1
+ =begin
2
+ String extension for CLIR
3
+ =end
4
+
5
+ class String
6
+
7
+ CHIFFRE_HAUT = {
8
+ 0 => '⁰',
9
+ 1 => '¹',
10
+ 2 => '²',
11
+ 3 => '³',
12
+ 4 => '⁴',
13
+ 5 => '⁵',
14
+ 6 => '⁶',
15
+ 7 => '⁷',
16
+ 8 => '⁸',
17
+ 9 => '⁹'
18
+ }
19
+ CHIFFRE_BAS = {
20
+ 0 => '₀',
21
+ 1 => '₁',
22
+ 2 => '₂',
23
+ 3 => '₃',
24
+ 4 => '₄',
25
+ 5 => '₅',
26
+ 6 => '₆',
27
+ 7 => '₇',
28
+ 8 => '₈',
29
+ 9 => '₉'
30
+ }
31
+
32
+ DATA_NORMALIZE = {
33
+ :from => "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
34
+ :to => "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
35
+ }
36
+
37
+ def self.columnize(lines, delimitor = ',', gutter = ' ')
38
+ # lines = lines.join("\n") if lines.is_a?(Array)
39
+ lines = lines.split("\n") if lines.is_a?(String)
40
+ #
41
+ # Nombre de colonnes
42
+ #
43
+ nombre_colonnes = 0
44
+ colonnes_widths = []
45
+ lines = lines.map do |line|
46
+ line.strip.split(delimitor).map {|e| e.strip}
47
+ end.each do |line|
48
+ nb = line.count # nombre de colonnes
49
+ nombre_colonnes = nb if nb > nombre_colonnes
50
+ end
51
+ #
52
+ # On met le même nombre de colonnes à toutes les lignes
53
+ #
54
+ lines.map do |line|
55
+ while line.count < nombre_colonnes
56
+ line << ''
57
+ end
58
+ line
59
+ end.each do |line|
60
+ line.each_with_index do |str, col_idx|
61
+ colonnes_widths[col_idx] = 0 if colonnes_widths[col_idx].nil?
62
+ colonnes_widths[col_idx] = str.length if str.length > colonnes_widths[col_idx]
63
+ end
64
+ end.each do |line|
65
+ #
66
+ # Mettre toutes les colonnes à la même taille
67
+ #
68
+ line.each_with_index do |str, col_idx|
69
+ line[col_idx] = str.ljust(colonnes_widths[col_idx])
70
+ end
71
+ end
72
+
73
+ lines.map do |line|
74
+ line.join(gutter)
75
+ end.join("\n").strip
76
+
77
+ end
78
+
79
+ # --- Predicate Methods ---
80
+
81
+ # @return TRUE is +str+ is a number (integer or float) in a string.
82
+ def numeric?
83
+ self.match?(/^[0-9.]+$/)
84
+ end
85
+
86
+ # @return TRUE if +ary+, as a String or an Array, includes
87
+ # self. If it's an Hash, has key self.
88
+ def in?(ary)
89
+ case ary
90
+ when Array
91
+ ary.include?(self)
92
+ when String
93
+ ary.match?(self)
94
+ when Hash
95
+ ary.key?(self)
96
+ else
97
+ raise "in? waits for a String, an Hash or a Array. Given: #{ary.class}."
98
+ end
99
+ end
100
+
101
+ # --- Helpers Methods ---
102
+
103
+
104
+ ##
105
+ # @return self with +len+ length. Cut it if necessary.
106
+ # @note
107
+ # Up to 10, cut at the end with '…' separator
108
+ # Up to 15, cut at the middle and if diff < 5, the separator is '…'
109
+ # @example
110
+ # "Sentence".max(5) # => "Sent…"
111
+ # "Long sentence".max(10) # => "Long…tence"
112
+ # "Very long and long sentence".max(16)
113
+ # # => "Very lo[…]ntence"
114
+ #
115
+ # @param [Integer] len Lenght required (> 1)
116
+ #
117
+ def max(len)
118
+ len.is_a?(Integer) || raise(ArgumentError.new("Argument should be a Integer"))
119
+ len > 1 || raise(ArgumentError.new("Minimum length should be 2. You give #{len}."))
120
+ return "#{self}" if self.length <= len
121
+ return self[0...len-1]+'…' if len <= 10
122
+ cur_len = self.length
123
+ diff = cur_len - len
124
+
125
+ sep, moitie =
126
+ if len > 15 && diff > 4
127
+ ['[…]', len / 2 - 2]
128
+ else
129
+ ['…', len / 2]
130
+ end
131
+
132
+ midav = self[0..moitie-1] + sep
133
+ reste = len - midav.length
134
+ midap = self[-reste..-1]
135
+
136
+ return midav + midap
137
+ end
138
+
139
+ def max!(len)
140
+ self.replace(self.max(len))
141
+ return true
142
+ end
143
+
144
+
145
+ # As ljust (which align to the left) ans rjust (which align to the
146
+ # right), cjust align to the center depending length
147
+ # @example
148
+ # "good".cjust(10) # => " good "
149
+ # "good".cjust(10, '+') # => "+++good+++"
150
+ def cjust(length, fill_with = ' ')
151
+ if self.length == length
152
+ return self
153
+ elsif self.length > length
154
+ return self[0...length]
155
+ else
156
+ nombre_moitie = (length - self.length) / 2
157
+ ret = (fill_with * nombre_moitie) + self + (fill_with * nombre_moitie)
158
+ ret = ret + fill_with if ret.length < length
159
+ return ret
160
+ end
161
+ end
162
+
163
+ # Si le texte est :
164
+ #
165
+ # Mon titre
166
+ #
167
+ # … cette méthode retourne :
168
+ #
169
+ # Mon titre
170
+ # ---------
171
+ #
172
+ #
173
+ def as_title(sous = '=', indent = 2)
174
+ len = self.length
175
+ ind = ' ' * indent
176
+ del = ind + sous * (len + 2)
177
+ "\n#{del}\n#{ind} #{self.upcase}\n#{del}"
178
+ end
179
+
180
+
181
+ def strike
182
+ "\033[9m#{self}\033[0m"
183
+ end
184
+ def underline
185
+ "\033[4m#{self}\033[0m"
186
+ end
187
+ def italic
188
+ "\033[3m#{self}\033[0m"
189
+ end
190
+
191
+
192
+ def blanc
193
+ "\033[0;38m#{self}\033[0m"
194
+ end
195
+ alias :white :blanc
196
+
197
+ def blanc_clair
198
+ "\033[0;37m#{self}\033[0m"
199
+ end
200
+
201
+ def bleu
202
+ "\033[0;96m#{self}\033[0m"
203
+ end
204
+ alias :blue :bleu
205
+
206
+ def bleu_clair
207
+ "\033[0;36m#{self}\033[0m"
208
+ end
209
+
210
+ def fond_bleu
211
+ "\033[0;44m#{self}\033[0m"
212
+ end
213
+
214
+ def fond_bleu_clair
215
+ "\033[0;46m#{self}\033[0m"
216
+ end
217
+
218
+ def vert
219
+ "\033[0;92m#{self}\033[0m"
220
+ end
221
+ alias :green :vert
222
+
223
+ def vert_clair
224
+ "\033[0;32m#{self}\033[0m"
225
+ end
226
+ alias :ligth_green :vert_clair
227
+
228
+ def fond_vert
229
+ "\033[0;42m#{self}\033[0m"
230
+ end
231
+
232
+ def rouge
233
+ "\033[0;91m#{self}\033[0m"
234
+ end
235
+ alias :red :rouge
236
+
237
+ def gris
238
+ "\033[0;90m#{self}\033[0m"
239
+ end
240
+ alias :grey :gris
241
+
242
+ def orange
243
+ "\033[38;5;214m#{self}\033[0m"
244
+ end
245
+
246
+ def jaune
247
+ "\033[0;93m#{self}\033[0m"
248
+ end
249
+ alias :yellow :jaune
250
+
251
+ def jaune_dark
252
+ "\033[0;33m#{self}\033[0m"
253
+ end
254
+
255
+ def mauve
256
+ "\033[1;94m#{self}\033[0m"
257
+ end
258
+ alias :purple :mauve
259
+
260
+
261
+ # --- Transform Methods ---
262
+
263
+ def camelize
264
+ str = "#{self}"
265
+ str[0] = str[0].upcase
266
+ str.split(' ').map do |seg|
267
+ seg.gsub(/(?:_+([a-z]))/i){$1.upcase}
268
+ end.join(' ')
269
+ end
270
+
271
+ def decamelize
272
+ str = self
273
+ str[0] = str[0].downcase
274
+ str.split(' ').map do |seg|
275
+ seg.gsub(/([A-Z])/){ "_#{$1.downcase}"}
276
+ end.join(' ')
277
+ end
278
+
279
+ def titleize
280
+ str = self
281
+ str.split(' ').map { |n| n[0].upcase + n[1..-1].downcase }.join(' ')
282
+ end
283
+
284
+ def patronize
285
+ str = self
286
+ str.split(/( |\-)/).map do |n|
287
+ n = n.downcase
288
+ if n == 'de'
289
+ 'de'
290
+ else
291
+ n.capitalize
292
+ end
293
+ end.join('')
294
+ end
295
+
296
+ def normalize
297
+ self
298
+ .force_encoding('utf-8')
299
+ .gsub(/[œŒæÆ]/,{'œ'=>'oe', 'Œ' => 'Oe', 'æ'=> 'ae', 'Æ' => 'Ae'})
300
+ .tr(DATA_NORMALIZE[:from], DATA_NORMALIZE[:to])
301
+ end
302
+ alias :normalized :normalize
303
+
304
+ end #/class String
@@ -0,0 +1,20 @@
1
+ class Symbol
2
+
3
+ # @return TRUE if self is inside +ary+ or has key self
4
+ # @param ary {Range|Array|Hash}
5
+ def in?(ary)
6
+ case ary
7
+ when Array
8
+ ary.include?(self)
9
+ when Hash
10
+ ary.key?(self)
11
+ else
12
+ raise "Symbol#in? waits for a Array or an Hash. Given: #{ary.class}."
13
+ end
14
+ end
15
+
16
+ def camelize
17
+ self.to_s.camelize
18
+ end
19
+
20
+ end