subunit 0.8.5 → 0.8.6
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 +72 -62
- 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: 0c676bd895436a156711b39cfa244a46524112df81d678cd6c2f790560f3155f
|
4
|
+
data.tar.gz: 54a45610aa7c71050fe011d6b74fee408f378fc7c00564d0546cbdacba187f12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 247d4cd835390d9452d31cea931177c3f4b1cf52bd6104460c600d21e5652114b7b6a7447de4d66733956ca93c3a47638cf985ba8b7e58f2cb51ac010bc4b745
|
7
|
+
data.tar.gz: 0605cff7b8f350e8785c82366f365300f72fb70b5a698f6b5361e958fa3c898460d4ded752d6a4cfc751ffa58fe9ad422a8424a638ac8086fc9448dcd275d1de
|
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,90 +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
|
+
|
80
81
|
h = to_h
|
81
82
|
a = h.to_a
|
82
83
|
a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
|
83
84
|
a2.map {|label, val| val.to_s + label[0]}.join(' ')
|
84
|
-
|
85
|
+
|
85
86
|
end
|
86
|
-
|
87
|
+
|
87
88
|
s.sub!(/%Xi?/) do |x|
|
88
89
|
|
89
90
|
h = to_h
|
90
91
|
a = h.to_a
|
91
92
|
a2 = (a.length > 1 and x[-1][/i$/] and h[:seconds]) ? a[0..-2] : a
|
92
|
-
|
93
|
-
a2.map do |label, val|
|
94
|
-
|
93
|
+
|
94
|
+
a2.map do |label, val|
|
95
|
+
|
95
96
|
next if val == 0
|
96
97
|
label2 = val > 1 ? label.to_s : label.to_s.sub(/s$/,'')
|
97
98
|
val.to_s + ' ' + label2
|
98
|
-
|
99
|
+
|
99
100
|
end.compact.join(' ')
|
100
|
-
|
101
|
-
end
|
102
|
-
|
101
|
+
|
102
|
+
end
|
103
|
+
|
103
104
|
s.sub!('%c') do |x|
|
104
|
-
|
105
|
+
|
105
106
|
a = @raw_units.values.reverse
|
106
107
|
a << a[-1]
|
107
|
-
|
108
|
+
|
108
109
|
fmt = a.map {|v| "%0" + v.to_s.length.to_s + "d"}.join(":")
|
109
110
|
fmt % to_a
|
110
|
-
end
|
111
|
-
|
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:)+/,'').sub(/^0/,'')
|
120
|
+
end
|
121
|
+
|
112
122
|
s.sub!('%s') do |x|
|
113
|
-
label, val = to_h.first
|
123
|
+
label, val = to_h.first
|
114
124
|
val.to_s + label[0]
|
115
|
-
end
|
125
|
+
end
|
116
126
|
|
117
127
|
s
|
118
128
|
end
|
119
|
-
|
129
|
+
|
120
130
|
def to_s(omit: [], verbose: true)
|
121
|
-
|
131
|
+
|
122
132
|
if not verbose then
|
123
|
-
|
133
|
+
|
124
134
|
r = self.to_a.reverse.take_while {|x| x > 0}.reverse\
|
125
135
|
.map {|x| "%02d" % x}.join(':')
|
126
|
-
|
136
|
+
|
127
137
|
if r.length < 2 then
|
128
138
|
return '00:00'
|
129
|
-
elsif r.length == 2
|
139
|
+
elsif r.length == 2
|
130
140
|
return '00:' + r
|
131
141
|
else
|
132
142
|
return r
|
133
143
|
end
|
134
|
-
|
144
|
+
|
135
145
|
end
|
136
|
-
|
146
|
+
|
137
147
|
h = @to_h
|
138
148
|
omit.each {|x| h.delete x}
|
139
149
|
|
140
150
|
list = h.to_a
|
141
151
|
n = list.find {|_,v| v > 0 }
|
142
|
-
a = list[list.index(n)..-1].map {|x|"%d %s" % x.reverse}
|
143
|
-
|
152
|
+
a = list[list.index(n)..-1].map {|x|"%d %s" % x.reverse}
|
153
|
+
|
144
154
|
duration = case a.length
|
145
155
|
|
146
156
|
when 1
|
147
|
-
a.first
|
157
|
+
a.first
|
148
158
|
when 2
|
149
159
|
a.join ' and '
|
150
|
-
else
|
160
|
+
else
|
151
161
|
"%s and %s" % [a[0..-2].join(', '), a[-1]]
|
152
162
|
end
|
153
|
-
|
163
|
+
|
154
164
|
duration.gsub(/(\b1 \w+)s/, '\1')
|
155
165
|
|
156
166
|
end
|
@@ -164,18 +174,18 @@ class Subunit
|
|
164
174
|
def fixnum_subunit(v)
|
165
175
|
[v, :remainder]
|
166
176
|
end
|
167
|
-
|
177
|
+
|
168
178
|
def integer_subunit(v)
|
169
179
|
[v, :remainder]
|
170
|
-
end
|
171
|
-
|
180
|
+
end
|
181
|
+
|
172
182
|
def hash_units(raw_units={}, u=nil)
|
173
|
-
|
183
|
+
|
174
184
|
val, label = method((u.class.to_s.downcase + '_subunit').to_sym).call(u)
|
175
185
|
units = multiply_units(raw_units.values).reverse
|
176
186
|
puts 'units: ' + units.inspect if @debug
|
177
|
-
@to_a = a = scan_units(units, val).map(&:to_i)
|
178
|
-
|
187
|
+
@to_a = a = scan_units(units, val).map(&:to_i)
|
188
|
+
|
179
189
|
puts 'a: ' + a.inspect if @debug
|
180
190
|
pairs = (raw_units.keys.reverse + [label]).reverse.take(a.length)\
|
181
191
|
.reverse.zip(a).select {|x| x[1] > 0}
|
@@ -191,8 +201,8 @@ class Subunit
|
|
191
201
|
def multiply_units(x) x.inject([]){|r,x| r + [x.to_i * (r[-1] || 1)]} end
|
192
202
|
|
193
203
|
def scan_units(unit_list,raw_subunit)
|
194
|
-
n = unit_list.shift
|
195
|
-
unit, subunit = [:/,:%].map{|x| raw_subunit.send x, n}
|
204
|
+
n = unit_list.shift
|
205
|
+
unit, subunit = [:/,:%].map{|x| raw_subunit.send x, n}
|
196
206
|
unit_list.length > 0 ? [unit] + scan_units(unit_list, subunit) : [unit, subunit]
|
197
207
|
end
|
198
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.6
|
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
|
MIIEXjCCAsagAwIBAgIBATAN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8wHYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0
|
27
27
|
c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1hc3RlckBqYW1lc3JvYmVydHNvbi5ldTAN
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
BgkqhkiG9w0BAQsFAAOCAYEAkFvP0dx51fJOkit9bSNU+ZDmsGj5yRas5hN3Mpr8
|
29
|
+
zPr58Tek+sPXZZgJOxSSofA6XOoaw/Qf54Zvi12QvlACr8b9sBDh7LvKOJYr7AAx
|
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
|