Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short format option #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/read_my_time/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def seconds_in_words(opts = {})

# options by default
opts[:skip_seconds] = false if opts[:skip_seconds].nil?
opts[:short_format] = false if opts[:short_format].nil?

unit_time_dividers.each_with_object([]) do |(unit_time, divider), s|

Expand All @@ -16,6 +17,7 @@ def seconds_in_words(opts = {})

if rest.nonzero? && !(unit_time == :seconds && opts[:skip_seconds])
word = I18n.t(unit_time.to_s, count: rest, scope: locale_prefix)
word = (opts[:short_format] ? word[0] : word)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about instead of do word[0], to have a short_format in our locales somehow? No idea if word[0] works for all locales

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea.

s.unshift("#{rest} #{word}")
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/test_read_my_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def test_dont_return_seconds_explicitly
assert_equal('', 10.seconds_in_words(skip_seconds: true))
end

def test_return_seconds_using_short_format
assert_equal('10 s', 10.seconds_in_words(short_format: true))
end

def test_handle_float_values
assert_equal('10 seconds', 10.1.seconds_in_words)
end
Expand All @@ -40,6 +44,10 @@ def test_return_word_days
assert_equal('1 day 1 hour 1 minute 40 seconds', (1.day + 1.hour + 1.minute + 40).seconds_in_words)
end

def test_return_word_days_using_short_format
assert_equal('1 d 1 h 1 m 40 s', (1.day + 1.hour + 1.minute + 40).seconds_in_words(short_format: true))
end

# skip show 0 values
def test_dont_return_zero_values
assert_equal('1 day 1 minute 40 seconds', (1.day + 1.minute + 40).seconds_in_words)
Expand Down