Commit dde56a78 authored by Bryce Johnson's avatar Bryce Johnson

Add support for custom daysPerWeek and hoursPerDay to prettyTime.parseSeconds.

parent e5842838
......@@ -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;
......
......@@ -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 () {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment