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

Format options for Interval or Duration? #43

Open
chexxor opened this issue Jan 26, 2018 · 0 comments
Open

Format options for Interval or Duration? #43

chexxor opened this issue Jan 26, 2018 · 0 comments

Comments

@chexxor
Copy link

chexxor commented Jan 26, 2018

MomentJS has a way to turn a duration into a human-readable string:

https://momentjs.com/docs/#/durations/humanize/

moment.duration(1, "minutes").humanize(); // a minute
moment.duration(2, "minutes").humanize(); // 2 minutes
moment.duration(24, "hours").humanize();  // a day

I've found myself wanting to turn an IsoDuration into a string. I'm not sure if that would belong in this library or not, as it seems tricky to specify the output format, as I haven't seen a similar specification like date's "YYYY". I imagine one way to implement this is like:

formatDuration :: { biggestFirst :: Boolean, ... } -> Duration -> String

-- It might be better to move the unit conversion into a separate function.
msFromDuration :: Duration -> Milliseconds
formatMS :: { fitToBiggestUnit :: Boolean, forceToUnit :: DurationComponent } -> Milliseconds -> String

-- I imagine would need a way to customize this for a locale.
newtype FormatDurationLocale :: FormatDurationLocale (DurationComponent -> String)
formatMS :: FormatDurationLocale -> FormatDurationOpts -> Milliseconds -> String

-- Test it out
-- > formatMS englishDurationLocale defaultDurationOpts { fitToBiggest = true } (msFromDuration tenMinutes)
-- 10 minutes

-- > formatMS (^ same as that ^) (msFromDuration tenDaysTenMinutes)
-- 1 week, 3 days, 10 minutes

-- > formatMS (englishDurationLocale defaultDurationOpts { forceToUnit = Week }) (msFromDuration tenDaysTenMinutes)
-- 1.31 weeks

Have you guys thought about this? Looks like current formatting is only to-from the ISO-8601 duration format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant