lite-ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Ruby
5
+ module DateHelper
6
+
7
+ DATE_STAMPS ||= {
8
+ weekday: '%d',
9
+ weekday_blank: '%_d',
10
+ weekday_padded: '%d',
11
+ weekday_unpadded: '%-d',
12
+ weekday_name: '%A',
13
+ weekday_name_abbr: '%a',
14
+ month: '%m',
15
+ month_blank: '%_m',
16
+ month_padded: '%m',
17
+ month_unpadded: '%-m',
18
+ month_name: '%B',
19
+ month_name_abbr: '%b',
20
+ week_iso: '%V',
21
+ week_monday: '%W',
22
+ week_sunday: '%U',
23
+ year: '%Y',
24
+ yr: '%y',
25
+ year_abbr: '%y',
26
+ day: '%B %-d',
27
+ day_abbr: '%b %-d',
28
+ day_iso: '%m-%d',
29
+ month_year: '%m %Y',
30
+ month_blank_year: '%_m %Y',
31
+ month_padded_year: '%m %Y',
32
+ month_unpadded_year: '%-m %Y',
33
+ month_name_year: '%B %Y',
34
+ month_name_abbr_year: '%b %Y',
35
+ week_year_iso: '%V-%G',
36
+ year_day: '%Y-%m-%d',
37
+ year_month: '%Y-%m',
38
+ year_week: '%G-%V',
39
+ date: '%B %-d, %Y',
40
+ date_abbr: '%b %-d, %Y',
41
+ date_iso: '%Y-%m-%d'
42
+ }.freeze
43
+ DATE_UNITS ||= {
44
+ w: 'u',
45
+ weekday: 'u',
46
+ ww: 'w',
47
+ weekday_offset: 'w',
48
+ www: 'A',
49
+ weekday_name: 'A',
50
+ wwww: 'a',
51
+ weekday_name_abbr: 'a',
52
+ d: 'd',
53
+ day: 'd',
54
+ day_padded: 'd',
55
+ dd: '-d',
56
+ Day: '-d',
57
+ day_unpadded: '-d',
58
+ ddd: '_d',
59
+ DAY: '_d',
60
+ day_blank: '_d',
61
+ dddd: 'j',
62
+ day_of_the_year: 'j',
63
+ m: 'm',
64
+ month: 'm',
65
+ month_padded: 'm',
66
+ mm: '-m',
67
+ Month: '-m',
68
+ month_unpadded: '-m',
69
+ mmm: '_m',
70
+ MONTH: '_m',
71
+ month_blank: '_m',
72
+ mmmm: 'B',
73
+ month_name: 'B',
74
+ mmmmm: 'b',
75
+ month_name_abbr: 'b',
76
+ swe: 'U',
77
+ sunday_week: 'U',
78
+ mwe: 'W',
79
+ monday_week: 'W',
80
+ we: 'V',
81
+ week: 'V',
82
+ yy: 'y',
83
+ yr: 'y',
84
+ year: 'Y',
85
+ yyyy: 'Y'
86
+ }.freeze
87
+
88
+ def format(string = nil)
89
+ string ||= default_format
90
+ delimiters = string.scan(/\W+/)
91
+ formatters = string.scan(/[a-z0-9_]+/i)
92
+ string = formatters.map { |key| "%#{format_for(key.to_sym)}#{delimiters.shift}" }
93
+ strftime(string.join)
94
+ end
95
+
96
+ def stamp(key = nil)
97
+ key = stamp_for(key&.to_sym || default_stamp)
98
+ strftime(key)
99
+ end
100
+
101
+ alias to_format stamp
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Ruby
5
+ module TimeHelper
6
+
7
+ TIME_STAMPS ||= {
8
+ hour: '%H',
9
+ hour_blank: '%k',
10
+ hour_padded: '%H',
11
+ hour_12: '%I',
12
+ hour_12_padded: '%I',
13
+ hour_12_blank: '%l',
14
+ minute: '%M',
15
+ second: '%S',
16
+ ampm: '%P',
17
+ meridian: '%p',
18
+ time_zone: '%z',
19
+ time_zone_offset: '%:z',
20
+ time_zone_offset_full: '%::z',
21
+ time_zone_name: '%Z',
22
+ time: '%H:%M',
23
+ time_blank: '%k:%M',
24
+ time_padded: '%H:%M',
25
+ time_tz: '%H:%M %z',
26
+ time_tzn: '%H:%M %Z',
27
+ time_12: '%I:%M %P',
28
+ time_12_padded: '%I:%M %P',
29
+ time_12_blank: '%l:%M %P',
30
+ time_12_tz: '%I:%M %P %z',
31
+ time_12_tzn: '%I:%M %P %Z',
32
+ daytime: '%B %-d %H:%M',
33
+ daytime_abbr: '%b %-d %H:%M',
34
+ daytime_iso: '%m-%d %H:%M',
35
+ daytime_12: '%B %-d %I:%M %P',
36
+ daytime_12_abbr: '%b %-d %I:%M %P',
37
+ daytime_12_iso: '%m-%d %I:%M %P',
38
+ datetime: '%B %-d, %Y %H:%M',
39
+ datetime_abbr: '%b %-d, %Y %H:%M',
40
+ datetime_iso: '%Y-%m-%d %H:%M',
41
+ datetime_12: '%B %-d, %Y %I:%M %P',
42
+ datetime_12_abbr: '%b %-d, %Y %I:%M %P',
43
+ datetime_12_iso: '%Y-%m-%d %I:%M %P',
44
+ datetime_tzn: '%B %-d, %Y %H:%M %Z',
45
+ datetime_abbr_tzn: '%b %-d, %Y %H:%M %Z',
46
+ datetime_iso_tzn: '%Y-%m-%d %H:%M %z',
47
+ datetime_12_tzn: '%B %-d, %Y %I:%M %P %Z',
48
+ datetime_12_abbr_tzn: '%b %-d, %Y %I:%M %P %Z',
49
+ datetime_12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
50
+ }.freeze
51
+ TIME_UNITS = {
52
+ h: 'H',
53
+ hour: 'H',
54
+ hour_padded: 'H',
55
+ hh: 'k',
56
+ HOUR: 'k',
57
+ hour_blank: 'k',
58
+ hhh: 'I',
59
+ hour_12: 'I',
60
+ hour_12_padded: 'I',
61
+ hhhh: 'l',
62
+ HOUR_12: 'l',
63
+ hour_12_blank: 'l',
64
+ n: 'M',
65
+ minute: 'M',
66
+ s: 'S',
67
+ second: 'S',
68
+ ampm: 'P',
69
+ meridian: 'P',
70
+ AMPM: 'p',
71
+ MERIDIAN: 'p',
72
+ z: 'z',
73
+ time_zone: 'z',
74
+ zz: ':z',
75
+ time_zone_offset: ':z',
76
+ zzz: '::z',
77
+ time_zone_offset_full: '::z',
78
+ zzzz: 'Z',
79
+ time_zone_name: 'Z'
80
+ }.freeze
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Integer
4
+
5
+ ROMAN_NUMERALS ||= {
6
+ M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4,
7
+ I: 1
8
+ }.freeze
9
+
10
+ def factorial
11
+ return 1 if zero?
12
+
13
+ 2.upto(self).inject(1) { |acc, i| acc * i }
14
+ end
15
+
16
+ def factors
17
+ limit = Math.sqrt(self).floor
18
+
19
+ (1..limit).each_with_object([]) do |i, array|
20
+ next unless (self % i).zero?
21
+
22
+ sq = (self / i)
23
+ array.push(i)
24
+ array.push(sq) if sq != i
25
+ end
26
+ end
27
+
28
+ def of(&block)
29
+ Array.new(self, &block)
30
+ end
31
+
32
+ def roman_numeral
33
+ return '' if zero?
34
+ return "-#{(-self).roman_numeral}" if negative?
35
+
36
+ ROMAN_NUMERALS.each { |key, val| break "#{key}#{(self - val).roman_numeral}" if val <= self }
37
+ end
38
+
39
+ def to_time
40
+ Time.at(self)
41
+ end
42
+
43
+ alias to_t to_time unless defined?(to_t)
44
+
45
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kernel
4
+
5
+ # rubocop:disable Lint/RescueException, Security/Eval
6
+ def safe_eval
7
+ eval(self)
8
+ rescue Exception
9
+ self
10
+ end
11
+
12
+ def try_eval
13
+ val = /\[\d*,?\d*,?\d*\]/.match(to_s).to_s
14
+ return val if val.nil?
15
+
16
+ eval(val)
17
+ end
18
+ # rubocop:enable Lint/RescueException, Security/Eval
19
+
20
+ private
21
+
22
+ def caller_name(depth = 0)
23
+ val = caller[depth][/`([^']*)'/, 1]
24
+ return val if depth.zero? || !val.include?('<top (required)>')
25
+
26
+ caller[depth - 1][/`([^']*)'/, 1]
27
+ end
28
+
29
+ end
@@ -0,0 +1,210 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Numeric
4
+
5
+ def add(num)
6
+ self + num
7
+ end
8
+
9
+ # rubocop:disable Metrics/MethodLength
10
+ def clamp(minimum, maximum = nil)
11
+ if minimum.is_a?(Range)
12
+ maximum = minimum.max
13
+ minimum = minimum.min
14
+ end
15
+
16
+ if minimum > self
17
+ minimum
18
+ elsif maximum < self
19
+ maximum
20
+ else
21
+ self
22
+ end
23
+ end
24
+ # rubocop:enable Metrics/MethodLength
25
+
26
+ def decrement(amount = 1.0)
27
+ self - amount
28
+ end
29
+
30
+ def distance(num)
31
+ (self - num).abs
32
+ end
33
+
34
+ def divide(num)
35
+ return num if num.zero?
36
+
37
+ self / num
38
+ end
39
+
40
+ def equal_to?(num)
41
+ self == num
42
+ end
43
+
44
+ alias eq? equal_to?
45
+
46
+ def fraction
47
+ (self - truncate).abs
48
+ end
49
+
50
+ def fraction?
51
+ fraction != 0.0
52
+ end
53
+
54
+ def greater_than?(num)
55
+ num < self
56
+ end
57
+
58
+ alias gt? greater_than?
59
+
60
+ def greater_than_or_equal_to?(num)
61
+ num <= self
62
+ end
63
+
64
+ alias gteq? greater_than_or_equal_to?
65
+
66
+ def increment(amount = 1.0)
67
+ self + amount
68
+ end
69
+
70
+ def inside?(start, finish)
71
+ (start < self) && (finish > self)
72
+ end
73
+
74
+ def less_than?(num)
75
+ num > self
76
+ end
77
+
78
+ alias lt? less_than?
79
+
80
+ def less_than_or_equal_to?(num)
81
+ num >= self
82
+ end
83
+
84
+ alias lteq? less_than_or_equal_to?
85
+
86
+ def multiply(num)
87
+ self * num
88
+ end
89
+
90
+ def multiple_of?(number)
91
+ return zero? if number.zero?
92
+
93
+ modulo(number).zero?
94
+ end
95
+
96
+ def not_equal_to?(num)
97
+ self != num
98
+ end
99
+
100
+ alias not_eq? not_equal_to?
101
+ alias inequal_to? not_equal_to?
102
+ alias ineq? not_equal_to?
103
+
104
+ def ordinal
105
+ return 'th' if (11..13).cover?(abs % 100)
106
+
107
+ case abs % 10
108
+ when 1 then 'st'
109
+ when 2 then 'nd'
110
+ when 3 then 'rd'
111
+ else 'th'
112
+ end
113
+ end
114
+
115
+ def ordinalize
116
+ "#{self}#{ordinal}"
117
+ end
118
+
119
+ def outside?(start, finish)
120
+ (start > self) || (finish < self)
121
+ end
122
+
123
+ def pad(options = {})
124
+ pad_number = options[:pad_number] || 0
125
+ precision = options[:precision] || 3
126
+
127
+ to_s.rjust(precision, pad_number.to_s)
128
+ end
129
+
130
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
131
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
132
+ def pad_precision(options = {})
133
+ pad_number = options[:pad_number] || 0
134
+ precision = options[:precision] || 2
135
+ separator = options[:separator] || '.'
136
+ string = to_s
137
+
138
+ string << separator unless string.include?(separator)
139
+ ljust_count = string.split(separator).first.length
140
+ ljust_count += (string.count(separator) + precision) if precision.positive?
141
+
142
+ if ljust_count >= string.length
143
+ string.ljust(ljust_count, pad_number.to_s)
144
+ else
145
+ string[0..(ljust_count - 1)]
146
+ end
147
+ end
148
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
149
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
150
+
151
+ def percentage_of(number)
152
+ return 0 if zero? || number.zero?
153
+
154
+ (self / number.to_f) * 100.0
155
+ end
156
+
157
+ def power(num)
158
+ self**num
159
+ end
160
+
161
+ def root(num)
162
+ self**(1.0 / num)
163
+ end
164
+
165
+ def subtract(num)
166
+ self - num
167
+ end
168
+
169
+ def to_currency(options = {})
170
+ unit = options[:unit] || '$'
171
+
172
+ "#{unit}#{pad_precision(options.only(:precision))}"
173
+ end
174
+
175
+ def to_nearest_value(values = [])
176
+ return self if values.length.zero?
177
+
178
+ value = values.first
179
+ difference = (self - value).abs
180
+
181
+ values.each do |val|
182
+ next unless (self - val).abs < difference
183
+
184
+ difference = (self - val).abs
185
+ value = val
186
+ end
187
+
188
+ value
189
+ end
190
+
191
+ def to_percentage(options = {})
192
+ unit = options[:unit] || '%'
193
+
194
+ "#{pad_precision(options.only(:precision))}#{unit}"
195
+ end
196
+
197
+ def within?(number, epsilon = 0.01)
198
+ return number == self if epsilon.zero?
199
+
200
+ alpha = to_f
201
+ beta = number.to_f
202
+
203
+ if alpha.zero? || beta.zero?
204
+ (alpha - beta).abs < epsilon
205
+ else
206
+ (alpha / beta - 1).abs < epsilon
207
+ end
208
+ end
209
+
210
+ end