diff options
author | Bryce Johnson <bryce@gitlab.com> | 2017-08-31 15:07:57 -0400 |
---|---|---|
committer | Bryce Johnson <bryce@gitlab.com> | 2017-08-31 15:07:57 -0400 |
commit | dde56a783752a08561b19cf3b7d0f53635801002 (patch) | |
tree | 8c9db7be133f27f667ed23468417ca7a1f161a1a | |
parent | e58428382265f02639a7fc8c1bfcc6311564c0d0 (diff) | |
download | gitlab-ce-dde56a783752a08561b19cf3b7d0f53635801002.tar.gz |
Add support for custom daysPerWeek and hoursPerDay to prettyTime.parseSeconds.pretty-time-refactor
-rw-r--r-- | app/assets/javascripts/lib/utils/pretty_time.js | 13 | ||||
-rw-r--r-- | spec/javascripts/pretty_time_spec.js | 81 |
2 files changed, 88 insertions, 6 deletions
diff --git a/app/assets/javascripts/lib/utils/pretty_time.js b/app/assets/javascripts/lib/utils/pretty_time.js index 716aefbfcb7..227bf65b560 100644 --- a/app/assets/javascripts/lib/utils/pretty_time.js +++ b/app/assets/javascripts/lib/utils/pretty_time.js @@ -2,19 +2,20 @@ import _ from 'underscore'; (() => { /* - * TODO: Make these methods more configurable (e.g. parseSeconds timePeriodContstraints, - * stringifyTime condensed or non-condensed, abbreviateTimelengths) + * TODO: Make these methods more configurable (e.g. stringifyTime condensed or + * non-condensed, abbreviateTimelengths) * */ const utils = window.gl.utils = gl.utils || {}; const prettyTime = utils.prettyTime = { /* * Accepts seconds and returns a timeObject { weeks: #, days: #, hours: #, minutes: # } - * Seconds can be negative or positive, zero or non-zero. + * Seconds can be negative or positive, zero or non-zero. Can be configured for any day + * or week length. */ - parseSeconds(seconds) { - const DAYS_PER_WEEK = 5; - const HOURS_PER_DAY = 8; + parseSeconds(seconds, { daysPerWeek = 5, hoursPerDay = 8 } = {}) { + const DAYS_PER_WEEK = daysPerWeek; + const HOURS_PER_DAY = hoursPerDay; const MINUTES_PER_HOUR = 60; const MINUTES_PER_WEEK = DAYS_PER_WEEK * HOURS_PER_DAY * MINUTES_PER_HOUR; const MINUTES_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR; diff --git a/spec/javascripts/pretty_time_spec.js b/spec/javascripts/pretty_time_spec.js index de99e7e3894..0a6c479a95b 100644 --- a/spec/javascripts/pretty_time_spec.js +++ b/spec/javascripts/pretty_time_spec.js @@ -76,6 +76,87 @@ import '~/lib/utils/pretty_time'; expect(aboveOneWeek.days).toBe(3); expect(aboveOneWeek.weeks).toBe(173); }); + + it('should correctly accept a custom param for hoursPerDay', function () { + const parser = prettyTime.parseSeconds; + const config = { hoursPerDay: 24 }; + + const aboveOneHour = parser(4800, config); + + expect(aboveOneHour.minutes).toBe(20); + expect(aboveOneHour.hours).toBe(1); + expect(aboveOneHour.days).toBe(0); + expect(aboveOneHour.weeks).toBe(0); + + const aboveOneDay = parser(110000, config); + + expect(aboveOneDay.minutes).toBe(33); + expect(aboveOneDay.hours).toBe(6); + expect(aboveOneDay.days).toBe(1); + expect(aboveOneDay.weeks).toBe(0); + + const aboveOneWeek = parser(25000000, config); + + expect(aboveOneWeek.minutes).toBe(26); + expect(aboveOneWeek.hours).toBe(8); + expect(aboveOneWeek.days).toBe(4); + + expect(aboveOneWeek.weeks).toBe(57); + }); + + it('should correctly accept a custom param for daysPerWeek', function () { + const parser = prettyTime.parseSeconds; + const config = { daysPerWeek: 7 }; + + const aboveOneHour = parser(4800, config); + + expect(aboveOneHour.minutes).toBe(20); + expect(aboveOneHour.hours).toBe(1); + expect(aboveOneHour.days).toBe(0); + expect(aboveOneHour.weeks).toBe(0); + + const aboveOneDay = parser(110000, config); + + expect(aboveOneDay.minutes).toBe(33); + expect(aboveOneDay.hours).toBe(6); + expect(aboveOneDay.days).toBe(3); + expect(aboveOneDay.weeks).toBe(0); + + const aboveOneWeek = parser(25000000, config); + + expect(aboveOneWeek.minutes).toBe(26); + expect(aboveOneWeek.hours).toBe(0); + expect(aboveOneWeek.days).toBe(0); + + expect(aboveOneWeek.weeks).toBe(124); + }); + + it('should correctly accept custom params for daysPerWeek and hoursPerDay', function () { + const parser = prettyTime.parseSeconds; + const config = { daysPerWeek: 55, hoursPerDay: 14 }; + + const aboveOneHour = parser(4800, config); + + expect(aboveOneHour.minutes).toBe(20); + expect(aboveOneHour.hours).toBe(1); + expect(aboveOneHour.days).toBe(0); + expect(aboveOneHour.weeks).toBe(0); + + const aboveOneDay = parser(110000, config); + + expect(aboveOneDay.minutes).toBe(33); + expect(aboveOneDay.hours).toBe(2); + expect(aboveOneDay.days).toBe(2); + expect(aboveOneDay.weeks).toBe(0); + + const aboveOneWeek = parser(25000000, config); + + expect(aboveOneWeek.minutes).toBe(26); + expect(aboveOneWeek.hours).toBe(0); + expect(aboveOneWeek.days).toBe(1); + + expect(aboveOneWeek.weeks).toBe(9); + }); }); describe('stringifyTime', function () { |