dotiw 5.3.0 → 5.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1c8a832bcdb740a88002dec2f5c843daeca75b2674b2e31c26abc17a144c6a2
4
- data.tar.gz: 163a34f6e15e34026375176118399cc65ed73b2f723dab436bc9aedaf36dc0b7
3
+ metadata.gz: 156f786ed11b28af2e890b6612fb07fb09bd462dc818a5bb8bb9075a518044d7
4
+ data.tar.gz: ca01c5a48869011c7c1fe9f08cf8c6ad953920b4ea5bc4dd26e21850131b6363
5
5
  SHA512:
6
- metadata.gz: 6e1a5291d51d629e4fc27d3f915f84586089be0869a676107154ef5f865c46fac768c4a2e7d86ac5fdfc2aadc879b668923f130f2f75ad0c355cbb86a5b18cc4
7
- data.tar.gz: f575e5e232a5adfefd8f81aa15f132dc352d020233bd1f66f6cddca473031d5cce0abf76bfd0842e0b55c3c60d4a44e2e7cf56a28b6e6fc89245e9fd17705bd7
6
+ metadata.gz: 807d4628412b8d810c072ea5cf4e10ed2cb7e280bdeaeb1b65906f1e203bd1155c5a7457d21d4c8c8c112a5a7d0a9b9b9a3c9ff5f1100d6ff0a255f64380a1f3
7
+ data.tar.gz: 78c14c9c5ea09eb378a380fe686672e925665fdebd9f75096a5cdecc1f9d8497d9cf7839a8cad4dd9d84e4938441e1380c076ecb3ccbf0a24359aa70993da9a2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 5.3.3 (2022/04/25)
2
+
3
+ * [#128](https://github.com/radar/distance_of_time_in_words/pull/128): Adds support for numeric values for both the `from_time` and `to_time` arguments in the `#distance_of_time_in_words` helper - [@bjedrocha](https://github.com/bjedrocha).
4
+
5
+ ## 5.3.2 (2021/11/08)
6
+
7
+ * [#126](https://github.com/radar/distance_of_time_in_words/pull/126): Fixes `#distance_of_time_in_words_to_now` with `vague: true` when supplied without `include_seconds` argument - [@mozcomp](https://github.com/mozcomp).
8
+
9
+ ## 5.3.1 (2021/03/26)
10
+
11
+ * [#124](https://github.com/radar/distance_of_time_in_words/pull/124): Fixes compact formatting for distance_of_time_in_words - [@rposborne](https://github.com/rposborne).
12
+
1
13
  ## 5.3.0 (2021/03/18)
2
14
 
3
15
  * [#115](https://github.com/radar/distance_of_time_in_words/pull/115): Use constants for time durations (2x faster, 3.5x less memory) - [@krzysiek1507](https://github.com/krzysiek1507).
data/README.markdown CHANGED
@@ -58,6 +58,22 @@ Better than "about 1 year", am I right? Of course I am.
58
58
  => "1 second"
59
59
  ```
60
60
 
61
+ It also supports numeric arguments like the original Rails version:
62
+
63
+ ```ruby
64
+ >> distance_of_time_in_words(0, 150)
65
+ => "2 minutes and 30 seconds"
66
+ ```
67
+
68
+ as an alternative to:
69
+
70
+ ```ruby
71
+ >> distance_of_time_in_words(Time.now, Time.now + 2.5.minutes)
72
+ => "2 minutes and 30 seconds"
73
+ ```
74
+
75
+ This is useful if you're just interested in "stringifying" the length of time. Alternatively, you can use the `#distance_of_time` helper as described [below](#distance\_of\_time).
76
+
61
77
  The third argument for this method is whether or not to include seconds. By default this is `false` (because in Rails' `distance_of_time_in_words` it is), you can turn it on though by passing `true` as the third argument:
62
78
 
63
79
  ```ruby
@@ -67,6 +83,8 @@ The third argument for this method is whether or not to include seconds. By defa
67
83
 
68
84
  Yes this could just be merged into the options hash but I'm leaving it here to ensure "backwards-compatibility", because that's just an insanely radical thing to do. \m/
69
85
 
86
+ Alternatively this can be included in the options hash as `include_seconds: true` removing this argument altogether.
87
+
70
88
  The last argument is an optional options hash that can be used to manipulate behavior and (which uses `to_sentence`).
71
89
 
72
90
  Don't like having to pass in `Time.now` all the time? Then use `time_ago_in_words` or `distance_of_time_in_words_to_now` which also will *rock your
@@ -99,6 +117,10 @@ This will also be passed to `to_sentence`.
99
117
 
100
118
  Specify this if you want it to use the old `distance_of_time_in_words`. The value can be anything except `nil` or `false`.
101
119
 
120
+ #### :include_seconds
121
+
122
+ As described above this option is the equivalent to the third argument whether to include seconds.
123
+
102
124
  #### :accumulate_on
103
125
 
104
126
  Specifies the maximum output unit which will accumulate all the surplus. Say you set it to seconds and your time difference is of 2 minutes then the output would be 120 seconds.
@@ -9,15 +9,17 @@ module ActionView
9
9
  include DOTIW::Methods
10
10
 
11
11
  def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {})
12
- return _distance_of_time_in_words(from_time, to_time, options) if options[:vague]
12
+ options = merge_options(include_seconds_or_options, options)
13
+ return _distance_of_time_in_words(from_time, to_time, options.except(:vague)) if options[:vague]
13
14
 
14
- DOTIW::Methods.distance_of_time_in_words(from_time, to_time, include_seconds_or_options, options.except(:vague))
15
+ DOTIW::Methods.distance_of_time_in_words(from_time, to_time, options.except(:vague))
15
16
  end
16
17
 
17
18
  def distance_of_time_in_words_to_now(to_time = 0, include_seconds_or_options = {}, options = {})
18
- return _distance_of_time_in_words(Time.now, to_time, options) if options[:vague]
19
+ options = merge_options(include_seconds_or_options, options)
20
+ return _distance_of_time_in_words(Time.now, to_time, options.except(:vague)) if options[:vague]
19
21
 
20
- DOTIW::Methods.distance_of_time_in_words(Time.now, to_time, include_seconds_or_options, options.except(:vague))
22
+ DOTIW::Methods.distance_of_time_in_words(Time.now, to_time, options.except(:vague))
21
23
  end
22
24
 
23
25
  def distance_of_time_in_percent(from_time, current_time, to_time, options = {})
@@ -27,6 +29,16 @@ module ActionView
27
29
  result = ((current_time - from_time) / distance) * 100
28
30
  number_with_precision(result, options).to_s + '%'
29
31
  end
32
+
33
+ private
34
+ def merge_options(include_seconds_or_options, options)
35
+ if include_seconds_or_options.is_a?(Hash)
36
+ options.merge(include_seconds_or_options)
37
+ else
38
+ options.merge(include_seconds: !!include_seconds_or_options)
39
+ end
40
+ end
41
+
30
42
  end
31
43
  end
32
44
  end
data/lib/dotiw/methods.rb CHANGED
@@ -20,7 +20,9 @@ module DOTIW
20
20
  end
21
21
 
22
22
  def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options = {}, options = {})
23
- raise ArgumentError, "nil can't be converted to a Time value" if from_time.nil? || to_time.nil?
23
+ from_time = normalize_distance_of_time_argument_to_time(from_time)
24
+ to_time = normalize_distance_of_time_argument_to_time(to_time)
25
+ from_time, to_time = to_time, from_time if from_time > to_time
24
26
 
25
27
  if include_seconds_or_options.is_a?(Hash)
26
28
  options = include_seconds_or_options
@@ -28,7 +30,8 @@ module DOTIW
28
30
  options = options.dup
29
31
  options[:include_seconds] ||= !!include_seconds_or_options
30
32
  end
31
- return distance_of_time(from_time, options) if to_time == 0
33
+
34
+ return distance_of_time(to_time.to_i, options) if from_time.to_i == 0
32
35
 
33
36
  options = options_with_scope(options)
34
37
  hash = distance_of_time_in_words_hash(from_time, to_time, options)
@@ -41,6 +44,16 @@ module DOTIW
41
44
 
42
45
  private
43
46
 
47
+ def normalize_distance_of_time_argument_to_time(value)
48
+ if value.is_a?(Numeric)
49
+ Time.at(value)
50
+ elsif value.respond_to?(:to_time)
51
+ value.to_time
52
+ else
53
+ raise ArgumentError, "#{value.inspect} can't be converted to a Time value"
54
+ end
55
+ end
56
+
44
57
  def options_with_scope(options)
45
58
  if options.key?(:compact)
46
59
  options.merge(scope: DOTIW::DEFAULT_I18N_SCOPE_COMPACT)
@@ -102,7 +115,7 @@ module DOTIW
102
115
  default: :'support.array.last_word_connector',
103
116
  locale: options[:locale]
104
117
 
105
- output.to_sentence(options.except(:accumulate_on))
118
+ output.to_sentence(options.except(:accumulate_on, :compact))
106
119
  end
107
120
  end
108
121
  end
data/lib/dotiw/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DOTIW
4
- VERSION = '5.3.0'
4
+ VERSION = '5.3.3'
5
5
  end
@@ -101,11 +101,6 @@ describe 'A better distance_of_time_in_words' do
101
101
  expect(DOTIW.languages.map(&:to_s).sort).to eq languages.sort
102
102
  end
103
103
 
104
- it 'raises ArgumentError when nil is passed for time' do
105
- expect { distance_of_time_in_words(nil) }.to raise_error(ArgumentError)
106
- expect { distance_of_time_in_words(nil, nil) }.to raise_error(ArgumentError)
107
- end
108
-
109
104
  DOTIW.languages.each do |lang|
110
105
  context lang do
111
106
  YAML.safe_load(
@@ -135,32 +130,33 @@ describe 'A better distance_of_time_in_words' do
135
130
  end
136
131
 
137
132
  [
138
- [START_TIME, START_TIME + 5.days + 3.minutes, '5 days and 3 minutes'],
139
- [START_TIME, START_TIME + 1.minute, '1 minute'],
140
- [START_TIME, START_TIME + 3.years, '3 years'],
141
- [START_TIME, START_TIME + 10.years, '10 years'],
142
- [START_TIME, START_TIME + 8.months, '8 months'],
143
- [START_TIME, START_TIME + 3.hour, '3 hours'],
144
- [START_TIME, START_TIME + 13.months, '1 year and 1 month'],
133
+ [START_TIME, START_TIME + 5.days + 3.minutes, '5 days and 3 minutes', '5d3m'],
134
+ [START_TIME, START_TIME + 1.minute, '1 minute', '1m'],
135
+ [START_TIME, START_TIME + 3.years, '3 years', '3y'],
136
+ [START_TIME, START_TIME + 10.years, '10 years', '10y'],
137
+ [START_TIME, START_TIME + 8.months, '8 months', '8mo'],
138
+ [START_TIME, START_TIME + 3.hour, '3 hours', '3h'],
139
+ [START_TIME, START_TIME + 13.months, '1 year and 1 month', '1y1mo'],
145
140
  # Any numeric sequence is merely coincidental.
146
141
  [START_TIME, START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
147
- '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds'],
148
- ['2009-3-16'.to_time, '2008-4-14'.to_time, '11 months and 2 days'],
149
- ['2009-3-16'.to_time + 1.minute, '2008-4-14'.to_time, '11 months, 2 days, and 1 minute'],
150
- ['2009-4-14'.to_time, '2008-3-16'.to_time, '1 year, 4 weeks, and 1 day'],
151
- ['2009-2-01'.to_time, '2009-3-01'.to_time, '1 month'],
152
- ['2008-2-01'.to_time, '2008-3-01'.to_time, '1 month'],
153
- [Date.parse('31.03.2015').to_time, Time.parse('01.03.2016'), '10 months, 4 weeks, and 2 days'],
154
- [Date.new(2014, 1, 31), Date.new(2014, 3, 1), '4 weeks and 1 day'],
155
- ['2008-2-01'.to_time, '2008-3-01'.to_time, '1 month'],
156
- ['2014-1-31'.to_time, '2014-3-01'.to_time, '4 weeks and 1 day'],
157
- ['2014-1-31'.to_time, '2014-3-02'.to_time, '4 weeks and 2 days'],
158
- ['2016-1-31'.to_time, '2016-3-01'.to_time, '4 weeks and 2 days'],
159
- ['2016-1-31'.to_time, '2016-3-02'.to_time, '1 month']
160
- ].each do |start, finish, output|
142
+ '1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes, and 7 seconds', '1y2mo3w4d5h6m7s'],
143
+ ['2009-3-16'.to_time, '2008-4-14'.to_time, '11 months and 2 days', '11mo2d'],
144
+ ['2009-3-16'.to_time + 1.minute, '2008-4-14'.to_time, '11 months, 2 days, and 1 minute', '11mo2d1m'],
145
+ ['2009-4-14'.to_time, '2008-3-16'.to_time, '1 year, 4 weeks, and 1 day', '1y4w1d'],
146
+ ['2009-2-01'.to_time, '2009-3-01'.to_time, '1 month', '1mo'],
147
+ ['2008-2-01'.to_time, '2008-3-01'.to_time, '1 month', '1mo'],
148
+ [Date.parse('31.03.2015').to_time, Time.parse('01.03.2016'), '10 months, 4 weeks, and 2 days', '10mo4w2d'],
149
+ [Date.new(2014, 1, 31), Date.new(2014, 3, 1), '4 weeks and 1 day', '4w1d'],
150
+ ['2008-2-01'.to_time, '2008-3-01'.to_time, '1 month', '1mo'],
151
+ ['2014-1-31'.to_time, '2014-3-01'.to_time, '4 weeks and 1 day', '4w1d'],
152
+ ['2014-1-31'.to_time, '2014-3-02'.to_time, '4 weeks and 2 days', '4w2d'],
153
+ ['2016-1-31'.to_time, '2016-3-01'.to_time, '4 weeks and 2 days', '4w2d'],
154
+ ['2016-1-31'.to_time, '2016-3-02'.to_time, '1 month', '1mo']
155
+ ].each do |start, finish, output, compact_result|
161
156
  it "should be #{output}" do
162
157
  expect(distance_of_time_in_words(start, finish, true)).to eq(output)
163
158
  expect(distance_of_time_in_words(finish, start, true)).to eq(output)
159
+ expect(distance_of_time_in_words(start, finish, true, compact: true)).to eq(compact_result)
164
160
  end
165
161
  end
166
162
 
@@ -240,6 +236,38 @@ describe 'A better distance_of_time_in_words' do
240
236
  end
241
237
  end
242
238
  end
239
+
240
+ describe 'with mixed inputs' do
241
+ # Further backwards compatability with the original rails
242
+ # distance_of_time_in_words helper.
243
+ [
244
+ [0, 2.5.minutes.to_i, '2 minutes and 30 seconds'],
245
+ [10.minutes.to_i, 0, '10 minutes'],
246
+ [0, 24.weeks.to_i, '5 months, 2 weeks, and 1 day'],
247
+ [0, 0, 'less than 1 second'],
248
+ ].each do |start, finish, output|
249
+ it "should be #{output}" do
250
+ expect(distance_of_time_in_words(start, finish)).to eq(output)
251
+ end
252
+ end
253
+
254
+ context "of which one or both can't be converted to a Time value" do
255
+ let(:invalid_inputs) do
256
+ [
257
+ [nil, 5.minutes],
258
+ [nil, double(:does_not_respond_to_time)],
259
+ [nil],
260
+ [nil, nil],
261
+ ]
262
+ end
263
+
264
+ it "should raise an ArgumentError" do
265
+ invalid_inputs.each do |start, finish|
266
+ expect { distance_of_time_in_words(start, finish) }.to raise_error(ArgumentError)
267
+ end
268
+ end
269
+ end
270
+ end
243
271
  end
244
272
 
245
273
  describe 'with output options' do
@@ -319,7 +347,34 @@ describe 'A better distance_of_time_in_words' do
319
347
  end
320
348
 
321
349
  if defined?(ActionView)
322
- describe 'ActionView' do
350
+ describe 'ActionView without include seconds argument' do
351
+ [
352
+ [START_TIME,
353
+ START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
354
+ { vague: true },
355
+ 'about 1 year'],
356
+ [START_TIME,
357
+ START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
358
+ { vague: 'Yes please' },
359
+ 'about 1 year'],
360
+ [START_TIME,
361
+ START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
362
+ { vague: false },
363
+ '1 year, 2 months, 3 weeks, 4 days, 5 hours, and 6 minutes'],
364
+ [START_TIME,
365
+ START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
366
+ { vague: nil },
367
+ '1 year, 2 months, 3 weeks, 4 days, 5 hours, and 6 minutes']
368
+ ].each do |start, finish, options, output|
369
+ it "should be #{output}" do
370
+ expect(distance_of_time_in_words(start, finish, options)).to eq(output)
371
+ end
372
+ end
373
+ end
374
+ end
375
+
376
+ if defined?(ActionView)
377
+ describe 'ActionView with include seconds argument' do
323
378
  [
324
379
  [START_TIME,
325
380
  START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds,
@@ -342,7 +397,7 @@ describe 'A better distance_of_time_in_words' do
342
397
  expect(distance_of_time_in_words(start, finish, true, options)).to eq(output)
343
398
  end
344
399
  end
345
-
400
+
346
401
  context 'via ActionController::Base.helpers' do
347
402
  it '#distance_of_time_in_words' do
348
403
  end_time = START_TIME + 1.year + 2.months + 3.weeks + 4.days + 5.hours + 6.minutes + 7.seconds
@@ -368,32 +423,93 @@ describe 'A better distance_of_time_in_words' do
368
423
  end
369
424
 
370
425
  context 'without options' do
371
- it 'shows detailed duration' do
426
+ it 'shows detailed duration without seconds' do
372
427
  expected = '3 days and 14 minutes'
373
428
  actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(Time.now - 3.days - 14.minutes)
374
429
  expect(actual).to eq(expected)
375
430
  end
376
431
  end
377
432
 
378
- context 'with vague option true' do
433
+ context 'with seconds false and vague option true' do
434
+ it 'shows vague duration' do
435
+ expected = '3 days'
436
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
437
+ Time.now - 3.days - 14.minutes - 20.seconds, false, vague: true
438
+ )
439
+ expect(actual).to eq(expected)
440
+ end
441
+ end
442
+
443
+ context 'with seconds true and vague option true' do
379
444
  it 'shows vague duration' do
380
445
  expected = '3 days'
381
446
  actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
382
- Time.now - 3.days - 14.minutes, false, vague: true
447
+ Time.now - 3.days - 14.minutes - 20.seconds, true, vague: true
383
448
  )
384
449
  expect(actual).to eq(expected)
385
450
  end
386
451
  end
387
452
 
388
- context 'with vague option false' do
389
- it 'shows detailed duration' do
453
+ context 'with seconds false and vague option false' do
454
+ it 'shows detailed duration without seconds' do
390
455
  expected = '3 days and 14 minutes'
391
456
  actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
392
- Time.now - 3.days - 14.minutes, false, vague: false
457
+ Time.now - 3.days - 14.minutes - 20.seconds, false, vague: false
393
458
  )
394
459
  expect(actual).to eq(expected)
395
460
  end
396
461
  end
462
+
463
+ context 'with seconds true and vague option false' do
464
+ it 'shows detailed duration with seconds' do
465
+ expected = '3 days, 14 minutes, and 20 seconds'
466
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
467
+ Time.now - 3.days - 14.minutes - 20.seconds, true, vague: false
468
+ )
469
+ expect(actual).to eq(expected)
470
+ end
471
+ end
472
+
473
+ context 'without seconds and vague option false' do
474
+ it 'shows detailed duration without seconds' do
475
+ expected = '3 days and 14 minutes'
476
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
477
+ Time.now - 3.days - 14.minutes - 20.seconds, vague: false
478
+ )
479
+ expect(actual).to eq(expected)
480
+ end
481
+ end
482
+
483
+ context 'without seconds and vague option true' do
484
+ it 'shows vague duration' do
485
+ expected = '3 days'
486
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
487
+ Time.now - 3.days - 14.minutes - 20.seconds, vague: true
488
+ )
489
+ expect(actual).to eq(expected)
490
+ end
491
+ end
492
+
493
+ context 'with options include_seconds true and vague option false' do
494
+ it 'shows detailed duration with seconds' do
495
+ expected = '3 days, 14 minutes, and 20 seconds'
496
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
497
+ Time.now - 3.days - 14.minutes - 20.seconds, {include_seconds: true, vague: false}
498
+ )
499
+ expect(actual).to eq(expected)
500
+ end
501
+ end
502
+
503
+ context 'with options include_seconds false and vague option true' do
504
+ it 'shows vague duration' do
505
+ expected = '3 days'
506
+ actual = ActionController::Base.helpers.distance_of_time_in_words_to_now(
507
+ Time.now - 3.days - 14.minutes - 20.seconds, {include_seconds: false, vague: true}
508
+ )
509
+ expect(actual).to eq(expected)
510
+ end
511
+ end
512
+
397
513
  end
398
514
  end
399
515
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotiw
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.3.0
4
+ version: 5.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Bigg
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-03-18 00:00:00.000000000 Z
12
+ date: 2022-04-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport