subunit 0.8.4 → 0.8.7
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/subunit.rb +78 -66
- data.tar.gz.sig +0 -0
- metadata +26 -27
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7f00d0a7dead20614db36fec22d0ac28cc789a9c4a4c473921bc36ba4a8f320d
|
|
4
|
+
data.tar.gz: 0fa4731410ab2fa2c897ed450a46b9b7cdfa86e6d71c82c0fc28b0853e3c8eda
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6aea09f2bbf2a1dcdfe4b252817150c3cc0bbe67de27bcf15eced3a6b3aeca709910ce2adb4bcd83d7474e7e784028f83acd8b30f895197d353dd62bb7679ccd
|
|
7
|
+
data.tar.gz: bbef449d9d5a448150cf66bf824a8f2a089bfbea362d275caad7874aab47bfd17db2b83a950e217866e54276edb730417f903a46175f818739d4af31c25ad799
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/lib/subunit.rb
CHANGED
|
@@ -3,62 +3,62 @@
|
|
|
3
3
|
# file: subunit.rb
|
|
4
4
|
|
|
5
5
|
module SubunitFracture
|
|
6
|
-
|
|
6
|
+
|
|
7
7
|
refine Integer do
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
def strfunit(s)
|
|
10
10
|
Subunit.seconds(self).strfunit(s)
|
|
11
11
|
end
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
end
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
class Subunit
|
|
18
|
-
|
|
18
|
+
|
|
19
19
|
def self.seconds(val, units: nil)
|
|
20
20
|
new(units={minutes:60, hours:60, days:24}, seconds: val)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
# e.g. Subunit.hms_to_seconds('5 minutes and 4 seconds')
|
|
24
|
-
# => 304
|
|
24
|
+
# => 304
|
|
25
25
|
#
|
|
26
26
|
def self.hms_to_seconds(obj)
|
|
27
27
|
new(units={hours:60, minutes:60,seconds: 60, }, obj).to_i
|
|
28
|
-
end
|
|
28
|
+
end
|
|
29
29
|
|
|
30
30
|
attr_reader :to_a, :to_h, :to_i
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
def initialize(raw_units={}, obj)
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
@debug = false
|
|
35
|
-
|
|
35
|
+
|
|
36
36
|
@raw_units = raw_units
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
accumulate = ->(a) {
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
len = raw_units.to_a.length
|
|
41
41
|
val = ([0]*(len-1) + a).slice(-(len), len).zip(raw_units.values)\
|
|
42
|
-
.inject(0) {|r,x| r * x[1] + x[0] }
|
|
42
|
+
.inject(0) {|r,x| r * x[1] + x[0] }
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
|
|
45
45
|
if obj.is_a? String
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
@to_i = accumulate.call obj.split(/\D+/).map(&:to_i)
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
elsif obj.is_a? Array then
|
|
50
|
-
|
|
50
|
+
|
|
51
51
|
@to_i = accumulate.call obj
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
else
|
|
54
|
-
|
|
54
|
+
|
|
55
55
|
raw_subunit = obj || 0
|
|
56
56
|
method((raw_units.class.to_s.downcase + '_units').to_sym)\
|
|
57
57
|
.call(raw_units, raw_subunit)
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
end
|
|
60
60
|
end
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
# usage: Subunit.new(units={minutes:60, hours:60}, seconds: 661)\
|
|
63
63
|
# .strfunit("%x") #=> 11m 1s
|
|
64
64
|
#
|
|
@@ -67,88 +67,100 @@ class Subunit
|
|
|
67
67
|
# %X e.g. 11 minutes 1 second
|
|
68
68
|
# %Xi e.g. 11 minutes
|
|
69
69
|
# %c e.g. 00:11:01
|
|
70
|
-
# %
|
|
70
|
+
# %sc e.g. 11:01 # supresses leading zeros
|
|
71
|
+
# %s e.g. 11m # returns the 1st most significant value while
|
|
71
72
|
# ignoring the remainder
|
|
72
73
|
#
|
|
73
|
-
def strfunit(s)
|
|
74
|
-
|
|
75
|
-
# The i switch postfixed to the x or X switch will format the output
|
|
74
|
+
def strfunit(s)
|
|
75
|
+
|
|
76
|
+
# The i switch postfixed to the x or X switch will format the output
|
|
76
77
|
# without seconds unless there are no minutes.
|
|
77
|
-
|
|
78
|
+
|
|
78
79
|
s.sub!(/%xi?/) do |x|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
|
|
81
|
+
h = to_h
|
|
82
|
+
a = h.to_a
|
|
83
|
+
a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
|
|
82
84
|
a2.map {|label, val| val.to_s + label[0]}.join(' ')
|
|
83
|
-
|
|
85
|
+
|
|
84
86
|
end
|
|
85
|
-
|
|
87
|
+
|
|
86
88
|
s.sub!(/%Xi?/) do |x|
|
|
87
89
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
h = to_h
|
|
91
|
+
a = h.to_a
|
|
92
|
+
a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
|
|
93
|
+
|
|
94
|
+
a2.map do |label, val|
|
|
95
|
+
|
|
93
96
|
next if val == 0
|
|
94
97
|
label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
|
|
95
98
|
val.to_s + ' ' + label2
|
|
96
|
-
|
|
99
|
+
|
|
97
100
|
end.compact.join(' ')
|
|
98
|
-
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
|
|
101
104
|
s.sub!('%c') do |x|
|
|
102
|
-
|
|
105
|
+
|
|
103
106
|
a = @raw_units.values.reverse
|
|
104
107
|
a << a[-1]
|
|
105
|
-
|
|
108
|
+
|
|
106
109
|
fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
|
|
107
110
|
fmt % to_a
|
|
108
|
-
end
|
|
109
|
-
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
s.sub!('%sc') do |x|
|
|
114
|
+
|
|
115
|
+
a = @raw_units.values.reverse
|
|
116
|
+
a << a[-1]
|
|
117
|
+
|
|
118
|
+
fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
|
|
119
|
+
(fmt % to_a).gsub(/^(?:00:)*(?=[0-9]{2}:[0-9]{2})/,'').sub(/^0/,'')
|
|
120
|
+
end
|
|
121
|
+
|
|
110
122
|
s.sub!('%s') do |x|
|
|
111
|
-
label, val = to_h.first
|
|
123
|
+
label, val = to_h.first
|
|
112
124
|
val.to_s + label[0]
|
|
113
|
-
end
|
|
125
|
+
end
|
|
114
126
|
|
|
115
127
|
s
|
|
116
128
|
end
|
|
117
|
-
|
|
129
|
+
|
|
118
130
|
def to_s(omit: [], verbose: true)
|
|
119
|
-
|
|
131
|
+
|
|
120
132
|
if not verbose then
|
|
121
|
-
|
|
133
|
+
|
|
122
134
|
r = self.to_a.reverse.take_while {|x| x > 0}.reverse\
|
|
123
135
|
.map {|x| "%02d" % x}.join(':')
|
|
124
|
-
|
|
136
|
+
|
|
125
137
|
if r.length < 2 then
|
|
126
138
|
return '00:00'
|
|
127
|
-
elsif r.length == 2
|
|
139
|
+
elsif r.length == 2
|
|
128
140
|
return '00:' + r
|
|
129
141
|
else
|
|
130
142
|
return r
|
|
131
143
|
end
|
|
132
|
-
|
|
144
|
+
|
|
133
145
|
end
|
|
134
|
-
|
|
146
|
+
|
|
135
147
|
h = @to_h
|
|
136
148
|
omit.each {|x| h.delete x}
|
|
137
149
|
|
|
138
150
|
list = h.to_a
|
|
139
151
|
n = list.find {|_,v| v > 0 }
|
|
140
|
-
a = list[list.index(n)..-1].map {|x|"%d %s" % x.reverse}
|
|
141
|
-
|
|
152
|
+
a = list[list.index(n)..-1].map {|x|"%d %s" % x.reverse}
|
|
153
|
+
|
|
142
154
|
duration = case a.length
|
|
143
155
|
|
|
144
156
|
when 1
|
|
145
|
-
a.first
|
|
157
|
+
a.first
|
|
146
158
|
when 2
|
|
147
159
|
a.join ' and '
|
|
148
|
-
else
|
|
160
|
+
else
|
|
149
161
|
"%s and %s" % [a[0..-2].join(', '), a[-1]]
|
|
150
162
|
end
|
|
151
|
-
|
|
163
|
+
|
|
152
164
|
duration.gsub(/(\b1 \w+)s/, '\1')
|
|
153
165
|
|
|
154
166
|
end
|
|
@@ -162,18 +174,18 @@ class Subunit
|
|
|
162
174
|
def fixnum_subunit(v)
|
|
163
175
|
[v, :remainder]
|
|
164
176
|
end
|
|
165
|
-
|
|
177
|
+
|
|
166
178
|
def integer_subunit(v)
|
|
167
179
|
[v, :remainder]
|
|
168
|
-
end
|
|
169
|
-
|
|
180
|
+
end
|
|
181
|
+
|
|
170
182
|
def hash_units(raw_units={}, u=nil)
|
|
171
|
-
|
|
183
|
+
|
|
172
184
|
val, label = method((u.class.to_s.downcase + '_subunit').to_sym).call(u)
|
|
173
185
|
units = multiply_units(raw_units.values).reverse
|
|
174
186
|
puts 'units: ' + units.inspect if @debug
|
|
175
|
-
@to_a = a = scan_units(units, val).map(&:to_i)
|
|
176
|
-
|
|
187
|
+
@to_a = a = scan_units(units, val).map(&:to_i)
|
|
188
|
+
|
|
177
189
|
puts 'a: ' + a.inspect if @debug
|
|
178
190
|
pairs = (raw_units.keys.reverse + [label]).reverse.take(a.length)\
|
|
179
191
|
.reverse.zip(a).select {|x| x[1] > 0}
|
|
@@ -189,8 +201,8 @@ class Subunit
|
|
|
189
201
|
def multiply_units(x) x.inject([]){|r,x| r + [x.to_i * (r[-1] || 1)]} end
|
|
190
202
|
|
|
191
203
|
def scan_units(unit_list,raw_subunit)
|
|
192
|
-
n = unit_list.shift
|
|
193
|
-
unit, subunit = [:/,:%].map{|x| raw_subunit.send x, n}
|
|
204
|
+
n = unit_list.shift
|
|
205
|
+
unit, subunit = [:/,:%].map{|x| raw_subunit.send x, n}
|
|
194
206
|
unit_list.length > 0 ? [unit] + scan_units(unit_list, subunit) : [unit, subunit]
|
|
195
207
|
end
|
|
196
208
|
end
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: subunit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.
|
|
4
|
+
version: 0.8.7
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- James Robertson
|
|
@@ -11,34 +11,34 @@ cert_chain:
|
|
|
11
11
|
- |
|
|
12
12
|
-----BEGIN CERTIFICATE-----
|
|
13
13
|
MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMjIwMzIyMjAyNTUyWhcN
|
|
15
|
+
MjMwMzIyMjAyNTUyWjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
|
|
16
|
+
cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQDDRYH8
|
|
17
|
+
6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
30
|
+
KDnKBVLscp3Lma4jSSz+COLO5NKjyTJzoGNyrVWukcG+Q/od+F4gc9geaWkb4cAN
|
|
31
|
+
rk6xbh2OkmWKttaPYbfLrTCIJ47yjdPDWHTS1gzD/TGc3GkX/kEWEe3TrI1C0J9d
|
|
32
|
+
l8CJhRfqnM/Z7z9NgBcBYqEPcH50oZQfj+jPuC/aPCHYH6P6ERPCWxVrwjHdOa9P
|
|
33
|
+
0LCJ5kn6RqrhzfRKcO1+8c0whliNP+98DluwP+5pdM7HfbSpeArtA9DVf3vn+n+I
|
|
34
|
+
RpHzYUh8G2ASjUDDp1J7aNqtvJpa6M/a2HcDJJyAMw65Gq+QBaIYS/wOfvX8NJql
|
|
35
|
+
w3QFiPkdCGAc/OB+os5J5edPeaH3ohMnM+eLfMzu1TAb37QeREb9T3/+gCTv79XF
|
|
36
|
+
5EfsI7uk1Aib+1DtRwjU8kB0
|
|
37
37
|
-----END CERTIFICATE-----
|
|
38
|
-
date:
|
|
38
|
+
date: 2022-03-22 00:00:00.000000000 Z
|
|
39
39
|
dependencies: []
|
|
40
40
|
description:
|
|
41
|
-
email:
|
|
41
|
+
email: digital.robertson@gmail.com
|
|
42
42
|
executables: []
|
|
43
43
|
extensions: []
|
|
44
44
|
extra_rdoc_files: []
|
|
@@ -63,8 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
63
63
|
- !ruby/object:Gem::Version
|
|
64
64
|
version: '0'
|
|
65
65
|
requirements: []
|
|
66
|
-
|
|
67
|
-
rubygems_version: 2.7.10
|
|
66
|
+
rubygems_version: 3.2.22
|
|
68
67
|
signing_key:
|
|
69
68
|
specification_version: 4
|
|
70
69
|
summary: Input a raw unit and it returns the denomination.
|
metadata.gz.sig
CHANGED
|
Binary file
|