lite-ruby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+
5
+ FALSE_VALUES ||= [
6
+ false, 0, '0', 'false', 'FALSE', 'f', 'F'
7
+ ].freeze
8
+ TRUE_VALUES ||= [
9
+ true, 1, '1', 'true', 'TRUE', 't', 'T'
10
+ ].freeze
11
+
12
+ def array?
13
+ is_a?(Array)
14
+ end
15
+
16
+ def blank?
17
+ object = self
18
+ object = object.strip if respond_to?(:strip)
19
+ return object.empty? if respond_to?(:empty?)
20
+
21
+ !object
22
+ end
23
+
24
+ def bool?
25
+ true? || false?
26
+ end
27
+
28
+ def boolean?
29
+ TRUE_VALUES.include?(self) || FALSE_VALUES.include?(self)
30
+ end
31
+
32
+ def date?
33
+ is_a?(Date)
34
+ end
35
+
36
+ # rubocop:disable Style/YodaCondition
37
+ def false?
38
+ false == self
39
+ end
40
+ # rubocop:enable Style/YodaCondition
41
+
42
+ def falsey?
43
+ nil? || FALSE_VALUES.include?(self)
44
+ end
45
+
46
+ def float?
47
+ is_a?(Float)
48
+ end
49
+
50
+ def hash?
51
+ is_a?(Hash)
52
+ end
53
+
54
+ def integer?
55
+ is_a?(Integer)
56
+ end
57
+
58
+ def numeral?
59
+ !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
60
+ end
61
+
62
+ def numeric?
63
+ is_a?(Numeric)
64
+ end
65
+
66
+ def open_struct?
67
+ is_a?(OpenStruct)
68
+ end
69
+
70
+ def palindrome?
71
+ to_s == to_s.reverse
72
+ end
73
+
74
+ def present?
75
+ !blank?
76
+ end
77
+
78
+ def range?
79
+ is_a?(Range)
80
+ end
81
+
82
+ def safe_call(*keys)
83
+ try_call(*keys) || self
84
+ end
85
+
86
+ def safe_send(*keys)
87
+ try_send(*keys) || self
88
+ end
89
+
90
+ def safe_try(*obj, &block)
91
+ try(*obj, &block) || self
92
+ end
93
+
94
+ def salvage(placeholder = '---')
95
+ blank? ? placeholder : self
96
+ end
97
+
98
+ def send_chain(*keys)
99
+ Array(keys).inject(self) { |obj, key| obj.send(*key) }
100
+ end
101
+
102
+ def send_chain_if(*keys)
103
+ Array(keys).inject(self) { |obj, key| obj.send_if(*key) }
104
+ end
105
+
106
+ def send_if(key, *args)
107
+ return self unless respond_to?(key)
108
+
109
+ send(key, *args)
110
+ end
111
+
112
+ def set?
113
+ is_a?(Set)
114
+ end
115
+
116
+ def string?
117
+ is_a?(String)
118
+ end
119
+
120
+ def struct?
121
+ is_a?(Struct)
122
+ end
123
+
124
+ def symbol?
125
+ is_a?(Symbol)
126
+ end
127
+
128
+ def time?
129
+ is_a?(Time)
130
+ end
131
+
132
+ # rubocop:disable Style/YodaCondition
133
+ def true?
134
+ true == self
135
+ end
136
+ # rubocop:enable Style/YodaCondition
137
+
138
+ def truthy?
139
+ TRUE_VALUES.include?(self)
140
+ end
141
+
142
+ def try(*obj, &block)
143
+ try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
144
+ end
145
+
146
+ def try!(*obj, &block)
147
+ if obj.empty? && block_given?
148
+ block.arity.zero? ? instance_eval(&block) : yield(self)
149
+ else
150
+ public_send(*obj, &block)
151
+ end
152
+ end
153
+
154
+ def try_call(*keys)
155
+ return unless respond_to?(:call)
156
+
157
+ keys.blank? ? call : call(*keys)
158
+ end
159
+
160
+ def try_send(*keys)
161
+ send(*keys)
162
+ rescue StandardError
163
+ nil
164
+ end
165
+
166
+ end
167
+
168
+ class FalseClass
169
+
170
+ def to_i
171
+ 0
172
+ end
173
+
174
+ end
175
+
176
+ class TrueClass
177
+
178
+ def to_i
179
+ 1
180
+ end
181
+
182
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Range
4
+
5
+ def combine(other)
6
+ to_a.concat(other.to_a)
7
+ end
8
+
9
+ def include_with_range?(other)
10
+ return include?(other) unless other.is_a?(Range)
11
+
12
+ operator = exclude_end? && !other.exclude_end? ? :< : :<=
13
+ include?(other.first) && other.last.send(operator, last)
14
+ end
15
+
16
+ def overlaps?(other)
17
+ cover?(other.first) || other.cover?(first)
18
+ end
19
+
20
+ def sample
21
+ to_a.sample
22
+ end
23
+
24
+ def shuffle
25
+ to_a.shuffle
26
+ end
27
+
28
+ def within?(other)
29
+ cover?(other.first) && cover?(other.last)
30
+ end
31
+
32
+ end
@@ -0,0 +1,412 @@
1
+ # frozen_string_literal: false
2
+
3
+ class String
4
+
5
+ TRANSLITERATIONS ||= {
6
+ 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'Ae',
7
+ 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I',
8
+ 'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O',
9
+ 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U',
10
+ 'Ý' => 'Y', 'Þ' => 'Th', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
11
+ 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
12
+ 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n',
13
+ 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u',
14
+ 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y', 'Ā' => 'A',
15
+ 'ā' => 'a', 'Ă' => 'A', 'ă' => 'a', 'Ą' => 'A', 'ą' => 'a', 'Ć' => 'C', 'ć' => 'c',
16
+ 'Ĉ' => 'C', 'ĉ' => 'c', 'Ċ' => 'C', 'ċ' => 'c', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D',
17
+ 'ď' => 'd', 'Đ' => 'D', 'đ' => 'd', 'Ē' => 'E', 'ē' => 'e', 'Ĕ' => 'E', 'ĕ' => 'e',
18
+ 'Ė' => 'E', 'ė' => 'e', 'Ę' => 'E', 'ę' => 'e', 'Ě' => 'E', 'ě' => 'e', 'Ĝ' => 'G',
19
+ 'ĝ' => 'g', 'Ğ' => 'G', 'ğ' => 'g', 'Ġ' => 'G', 'ġ' => 'g', 'Ģ' => 'G', 'ģ' => 'g',
20
+ 'Ĥ' => 'H', 'ĥ' => 'h', 'Ħ' => 'H', 'ħ' => 'h', 'Ĩ' => 'I', 'ĩ' => 'i', 'Ī' => 'I',
21
+ 'ī' => 'i', 'Ĭ' => 'I', 'ĭ' => 'i', 'Į' => 'I', 'į' => 'i', 'İ' => 'I', 'ı' => 'i',
22
+ 'IJ' => 'Ij', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ķ' => 'K', 'ķ' => 'k', 'ĸ' => 'k',
23
+ 'Ĺ' => 'L', 'ĺ' => 'l', 'Ļ' => 'L', 'ļ' => 'l', 'Ľ' => 'L', 'ľ' => 'l', 'Ŀ' => 'L',
24
+ 'ŀ' => 'l', 'Ł' => 'L', 'ł' => 'l', 'Ń' => 'N', 'ń' => 'n', 'Ņ' => 'N', 'ņ' => 'n',
25
+ 'Ň' => 'N', 'ň' => 'n', 'ʼn' => 'n', 'Ŋ' => 'Ng', 'ŋ' => 'ng', 'Ō' => 'O', 'ō' => 'o',
26
+ 'Ŏ' => 'O', 'ŏ' => 'o', 'Ő' => 'O', 'ő' => 'o', 'Œ' => 'OE', 'œ' => 'oe', 'Ŕ' => 'R',
27
+ 'ŕ' => 'r', 'Ŗ' => 'R', 'ŗ' => 'r', 'Ř' => 'R', 'ř' => 'r', 'Ś' => 'S', 'ś' => 's',
28
+ 'Ŝ' => 'S', 'ŝ' => 's', 'Ş' => 'S', 'ş' => 's', 'Š' => 'S', 'š' => 's', 'Ţ' => 'T',
29
+ 'ţ' => 't', 'Ť' => 'T', 'ť' => 't', 'Ŧ' => 'T', 'ŧ' => 't', 'Ũ' => 'U', 'ũ' => 'u',
30
+ 'Ū' => 'U', 'ū' => 'u', 'Ŭ' => 'U', 'ŭ' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ű' => 'U',
31
+ 'ű' => 'u', 'Ų' => 'U', 'ų' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y',
32
+ 'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z'
33
+ }.freeze
34
+
35
+ def any?(*keys)
36
+ keys.any? { |key| include?(key) }
37
+ end
38
+
39
+ def at(position)
40
+ self[position]
41
+ end
42
+
43
+ def camelize(first_letter = :upper)
44
+ if first_letter.to_sym != :lower
45
+ regex_last = Regexp.last_match(1).upcase
46
+ to_s.gsub(%r{\/(.?)}) { "::#{regex_last}" }.gsub(%r{^/(?:^|_)(.)}) { regex_last }
47
+ else
48
+ "#{to_s.first.chr.downcase}#{camelize(self)[1..-1]}"
49
+ end
50
+ end
51
+
52
+ alias camelcase camelize
53
+
54
+ def camelize!(first_letter = :upper)
55
+ replace(camelize(first_letter))
56
+ end
57
+
58
+ alias camelcase! camelize!
59
+
60
+ def classify
61
+ to_s.sub(/.*\./, '').camelize
62
+ end
63
+
64
+ def classify!
65
+ replace(classify)
66
+ end
67
+
68
+ def constantize
69
+ Object.const_get(self)
70
+ end
71
+
72
+ def dasherize
73
+ tr(/_/, '-')
74
+ end
75
+
76
+ def dasherize!
77
+ replace(dasherize)
78
+ end
79
+
80
+ def deconstantize
81
+ to_s[0, rindex('::') || 0]
82
+ end
83
+
84
+ def deconstantize!
85
+ replace(deconstantize)
86
+ end
87
+
88
+ def demodulize
89
+ to_s.gsub(/^.*::/, '')
90
+ end
91
+
92
+ def demodulize!
93
+ replace(demodulize)
94
+ end
95
+
96
+ def domain
97
+ return self unless self =~ %r{^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)}
98
+
99
+ Regexp.last_match(1)
100
+ end
101
+
102
+ def downcase?
103
+ downcase == self
104
+ end
105
+
106
+ def ellipsize(ellipsize_at, options = {})
107
+ return self if length <= ellipsize_at
108
+
109
+ separator = options[:separator] || '...'
110
+ offset = options[:offset] || 4
111
+
112
+ "#{self[0, offset]}#{separator}#{self[-offset, offset]}"
113
+ end
114
+
115
+ def first(limit = 1)
116
+ if limit.zero?
117
+ ''
118
+ elsif limit >= length
119
+ self
120
+ else
121
+ to(limit - 1)
122
+ end
123
+ end
124
+
125
+ def format(*args)
126
+ super(self, *args.flatten)
127
+ end
128
+
129
+ def from(position)
130
+ self[position..-1]
131
+ end
132
+
133
+ def headerize
134
+ squish.split(' ').map(&:capitalize).join(' ')
135
+ end
136
+
137
+ def headerize!
138
+ replace(headerize)
139
+ end
140
+
141
+ def humanize(options = {})
142
+ capitalize = options[:capitalize] || true
143
+
144
+ underscore.gsub(/_id\z/, '')
145
+ .tr('_', ' ')
146
+ .squish
147
+ .gsub(/([a-z\d]*)/i, &:downcase)
148
+ .gsub(/\A\w/) { |str| capitalize ? str.upcase : str }
149
+ end
150
+
151
+ def humanize!(options = {})
152
+ replace(humanize(options))
153
+ end
154
+
155
+ def indent(amount, indent_string = nil, indent_empty_lines = false)
156
+ indent_string = indent_string || self[/^[ \t]/] || ' '
157
+ substitutes = indent_empty_lines ? /^/ : /^(?!$)/
158
+
159
+ gsub(substitutes, indent_string * amount)
160
+ end
161
+
162
+ def indent!(amount, indent_string = nil, indent_empty_lines = false)
163
+ replace(indent(amount, indent_string, indent_empty_lines))
164
+ end
165
+
166
+ def index_all(pattern)
167
+ pattern = pattern.to_s if pattern.is_a?(Numeric)
168
+ arr_indexes = []
169
+ srch_index = rindex(pattern)
170
+
171
+ while srch_index
172
+ temp_string = self[0..(srch_index - 1)]
173
+ arr_indexes << srch_index
174
+ srch_index = srch_index.zero? ? nil : temp_string.rindex(pattern)
175
+ end
176
+
177
+ arr_indexes.reverse
178
+ end
179
+
180
+ def labelize(options = {})
181
+ capitalize = options[:capitalize] || true
182
+
183
+ underscore.tr('_', ' ')
184
+ .squish
185
+ .gsub(/([a-z\d]*)/i, &:downcase)
186
+ .gsub(/\A\w/) { |str| capitalize ? str.upcase : str }
187
+ .gsub(/ id\z/, ' ID')
188
+ end
189
+
190
+ alias labelcase labelize
191
+
192
+ def labelize!(options = {})
193
+ replace(labelize(options))
194
+ end
195
+
196
+ alias labelcase! labelize!
197
+
198
+ def last(limit = 1)
199
+ if limit.zero?
200
+ ''
201
+ elsif limit >= length
202
+ self
203
+ else
204
+ from(-limit)
205
+ end
206
+ end
207
+
208
+ def mixedcase?
209
+ !upcase? && !downcase?
210
+ end
211
+
212
+ def ordinal
213
+ to_i.ordinal
214
+ end
215
+
216
+ def ordinalize
217
+ to_i.ordinalize
218
+ end
219
+
220
+ def parameterize(separator: '-')
221
+ underscore.gsub(/\s+/, separator).downcase
222
+ end
223
+
224
+ def parameterize!(separator: '-')
225
+ replace(parameterize(separator: separator))
226
+ end
227
+
228
+ def pollute(delimiter = '^--^--^')
229
+ split('').map { |chr| "#{chr}#{delimiter}" }.join
230
+ end
231
+
232
+ def pollute!(delimiter = '^--^--^')
233
+ replace(pollute(delimiter))
234
+ end
235
+
236
+ def pop
237
+ self[-1]
238
+ end
239
+
240
+ def push(string)
241
+ replace(concat(string))
242
+ end
243
+
244
+ def remove(*patterns)
245
+ patterns.each_with_object(dup) do |pat, str|
246
+ pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
247
+ end
248
+ end
249
+
250
+ def remove!(*patterns)
251
+ replace(remove(*patterns))
252
+ end
253
+
254
+ def remove_tags
255
+ gsub(%r{<\/?[^>]*>}, '')
256
+ end
257
+
258
+ def remove_tags!
259
+ replace(remove_tags)
260
+ end
261
+
262
+ def sample(separator = ' ')
263
+ split(separator).sample
264
+ end
265
+
266
+ def sample!(separator = ' ')
267
+ replace(sample(separator))
268
+ end
269
+
270
+ def shift(*patterns)
271
+ return self[0] if patterns.empty?
272
+
273
+ patterns.each_with_object(dup) { |pat, str| str.sub!(pat, '') }
274
+ end
275
+
276
+ def shift!(*patterns)
277
+ replace(shift(*patterns))
278
+ end
279
+
280
+ def shuffle(separator = '')
281
+ split(separator).shuffle.join
282
+ end
283
+
284
+ def shuffle!(separator = '')
285
+ replace(shuffle(separator))
286
+ end
287
+
288
+ def sift(keep)
289
+ keep = case keep
290
+ when String then keep.chars
291
+ when Array then keep.map(&:to_s)
292
+ when Range then keep.to_a.map(&:to_s)
293
+ else raise TypeError, "Invalid parameter #{keep.inspect}"
294
+ end
295
+
296
+ chars.keep_if { |chr| keep.include?(chr) }.join
297
+ end
298
+
299
+ def sift!(keep)
300
+ replace(sift(keep))
301
+ end
302
+
303
+ def slugify
304
+ to_s.gsub(/[^\x00-\x7F]+/, '')
305
+ .gsub(/[^\w_ \-]+/i, '')
306
+ .gsub(/[ \-]+/i, '-')
307
+ .gsub(/^\-|\-$/i, '')
308
+ .downcase
309
+ end
310
+
311
+ def slugify!
312
+ replace(slugify)
313
+ end
314
+
315
+ def squish
316
+ strip.gsub(/\s+/, ' ')
317
+ end
318
+
319
+ def squish!
320
+ replace(squish)
321
+ end
322
+
323
+ def sort
324
+ chars.sort.join
325
+ end
326
+
327
+ def sort!
328
+ replace(sort)
329
+ end
330
+
331
+ def titleize
332
+ underscore.humanize.gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
333
+ end
334
+
335
+ alias titlecase titleize
336
+
337
+ def titleize!
338
+ replace(titleize)
339
+ end
340
+
341
+ alias titlecase! titleize!
342
+
343
+ def to(position)
344
+ self[0..position]
345
+ end
346
+
347
+ def transliterize
348
+ dup.transliterize!
349
+ end
350
+
351
+ def transliterize!
352
+ TRANSLITERATIONS.each_with_object(self) { |(k, v), str| str.gsub!(k, v) }
353
+ end
354
+
355
+ def truncate(truncate_at, options = {})
356
+ return dup unless length > truncate_at
357
+
358
+ seperator = options[:separator]
359
+ omission = options[:omission] || '...'
360
+ size_with_room_for_omission = truncate_at - omission.length
361
+
362
+ stop = if seperator
363
+ rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
364
+ else
365
+ size_with_room_for_omission
366
+ end
367
+
368
+ "#{self[0, stop]}#{omission}"
369
+ end
370
+
371
+ def truncate_words(words_count, options = {})
372
+ sep = options[:separator] || /\s+/
373
+ sep = Regexp.escape(sep.to_s) unless sep.is_a(Regexp)
374
+ return self unless self =~ /\A((?:.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
375
+
376
+ "#{::Regexp.last_match(1)}#{options[:omissio] || '...'}"
377
+ end
378
+
379
+ def underscore
380
+ to_s.gsub(/::/, '/')
381
+ .gsub(/([A-Z\d]+)([A-Z][a-z])/, "\1_\2")
382
+ .gsub(/([a-z\d])([A-Z])/, "\1_\2")
383
+ .tr('-', '_')
384
+ .downcase
385
+ end
386
+
387
+ def underscore!
388
+ replace(underscore)
389
+ end
390
+
391
+ def unpollute(delimiter = '^--^--^')
392
+ gsub(delimiter, '')
393
+ end
394
+
395
+ def unpollute!(delimiter = '^--^--^')
396
+ replace(unpollute(delimiter))
397
+ end
398
+
399
+ def upcase?
400
+ upcase == self
401
+ end
402
+
403
+ def unshift(*patterns)
404
+ patterns.each_with_object('') { |pat, str| str << pat }
405
+ .concat(self)
406
+ end
407
+
408
+ def unshift!(*patterns)
409
+ replace(unshift(*patterns))
410
+ end
411
+
412
+ end