Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Convert Chronic Duration functions to TypeScript

Alexander Brandon Colese7ce1739dff4
diff --git a/frontend/src/app/core/config/configuration.service.ts b/frontend/src/app/core/config/configuration.service.ts
index 135ee4e6861..b44289244da 100644
--- a/frontend/src/app/core/config/configuration.service.ts
+++ b/frontend/src/app/core/config/configuration.service.ts
@@ -31,6 +31,7 @@ import moment from 'moment';
import { ConfigurationResource } from 'core-app/features/hal/resources/configuration-resource';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { type DurationFormat } from 'core-app/shared/helpers/chronic_duration';
@Injectable({ providedIn: 'root' })
export class ConfigurationService {
@@ -103,7 +104,7 @@ export class ConfigurationService {
return this.systemPreference('dateFormat');
}
- public durationFormat():string {
+ public durationFormat():DurationFormat {
return this.systemPreference('durationFormat');
}
diff --git a/frontend/src/app/shared/helpers/chronic_duration.spec.ts b/frontend/src/app/shared/helpers/chronic_duration.spec.ts
index 22d678a7469..b7c9fe3f01d 100644
--- a/frontend/src/app/shared/helpers/chronic_duration.spec.ts
+++ b/frontend/src/app/shared/helpers/chronic_duration.spec.ts
@@ -40,6 +40,7 @@ import {
parseChronicDuration,
outputChronicDuration,
DurationParseError,
+ type DurationFormat,
} from './chronic_duration';
@@ -273,18 +274,19 @@ describe('outputChronicDuration', () => {
hours_only: '12960h',
chrono: '18:00:00:00:00',
},
- };
+ } as const;
Object.entries(EXEMPLARS).forEach(([k, v]) => {
const kf = parseFloat(k);
- Object.entries(v).forEach(([key, val]) => {
+ (Object.keys(v) as DurationFormat[]).forEach((key) => {
+ const val = v[key];
it(`properly outputs a duration of ${kf} seconds as ${val} using the ${key} format option`, () => {
expect(outputChronicDuration(kf, { format: key })).toBe(val);
});
});
});
- const KEEP_ZERO_EXEMPLARS = {
+ const KEEP_ZERO_EXEMPLARS:Record<string, Partial<Record<DurationFormat, string|null>>> = {
true: {
micro: '0s',
short: '0s',
@@ -303,7 +305,8 @@ describe('outputChronicDuration', () => {
Object.entries(KEEP_ZERO_EXEMPLARS).forEach(([k, v]) => {
const kb = Boolean(k);
- Object.entries(v).forEach(([key, val]) => {
+ (Object.keys(v) as Partial<DurationFormat>[]).forEach((key) => {
+ const val = v[key] ?? null;
it(`should properly output a duration of 0 seconds as ${val} using the ${key} format option, if the .keepZero option is ${kb}`, () => {
expect(outputChronicDuration(0, { format: key, keepZero: kb })).toBe(val);
});
@@ -387,11 +390,11 @@ describe('outputChronicDuration', () => {
Object.entries(EXEMPLARS).forEach(([seconds, formatSpec]) => {
const secondsF = parseFloat(seconds);
- Object.keys(formatSpec).forEach((format) => {
+ (Object.keys(formatSpec) as DurationFormat[]).forEach((format) => {
if (format === 'days_and_hours' || format === 'hours_only') return;
it(`outputs a duration for ${seconds} that parses back to the same thing when using the ${format} format`, () => {
- expect(parseChronicDuration(outputChronicDuration(secondsF, { format }))).toBe(secondsF);
+ expect(parseChronicDuration(outputChronicDuration(secondsF, { format })!)).toBe(secondsF);
});
});
});
diff --git a/frontend/src/app/shared/helpers/chronic_duration.js b/frontend/src/app/shared/helpers/chronic_duration.ts
similarity index 81%
rename from frontend/src/app/shared/helpers/chronic_duration.js
rename to frontend/src/app/shared/helpers/chronic_duration.ts
index ac6deba1ef9..e57aca0c645 100644
--- a/frontend/src/app/shared/helpers/chronic_duration.js
+++ b/frontend/src/app/shared/helpers/chronic_duration.ts
@@ -48,9 +48,27 @@ const HOURS_PER_DAY = 24;
const DAYS_PER_MONTH = 30;
const FLOAT_MATCHER = /[0-9]*\.?[0-9]+/g;
-const DURATION_UNITS_LIST = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'];
+const DURATION_UNITS_LIST = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'] as const;
+export type DurationUnit = typeof DURATION_UNITS_LIST[number];
+export type DurationFormat = 'micro'|'short'|'long'|'days_and_hours'|'hours_only'|'chrono'|'default';
+
+export interface ChronicDurationOptions {
+ hoursPerDay?:number;
+ daysPerMonth?:number;
+ defaultUnit?:DurationUnit;
+ raiseExceptions?:boolean;
+ ignoreSecondsWhenColonSeperated?:boolean;
+ keepZero?:boolean;
+ format?:DurationFormat;
+ joiner?:string;
+ limitToHours?:boolean;
+ weeks?:boolean;
+ units?:number;
+}
+
+type Dividers = Partial<Record<DurationUnit, string>> & { pluralize?:boolean; keepZero?:boolean};
-const MAPPINGS = {
+const MAPPINGS:Record<string, DurationUnit> = {
seconds: 'seconds',
second: 'seconds',
secs: 'seconds',
@@ -88,13 +106,17 @@ const MAPPINGS = {
const JOIN_WORDS = ['and', 'with', 'plus'];
-function convertToNumber(string) {
+function convertToNumber(string:string):number {
const f = parseFloat(string);
return f % 1 > 0 ? f : parseInt(string, 10);
}
-function durationUnitsSecondsMultiplier(unit, opts) {
- if (!DURATION_UNITS_LIST.includes(unit)) {
+function isDurationUnit(unit:string):unit is DurationUnit {
+ return (DURATION_UNITS_LIST as readonly string[]).includes(unit);
+}
+
+function durationUnitsSecondsMultiplier(unit:string, opts:ChronicDurationOptions):number {
+ if (!isDurationUnit(unit)) {
return 0;
}
@@ -122,9 +144,9 @@ function durationUnitsSecondsMultiplier(unit, opts) {
}
}
-function calculateFromWords(string, opts) {
+function calculateFromWords(string:string, opts:ChronicDurationOptions):number {
let val = 0;
- let lastExplicitUnit = null;
+ let lastExplicitUnit:DurationUnit|null = null;
const words = string.split(' ');
words.forEach((v, k) => {
@@ -132,10 +154,10 @@ function calculateFromWords(string, opts) {
return;
}
if (v.search(FLOAT_MATCHER) >= 0) {
- const nextWord = words[parseInt(k, 10) + 1];
- let unit;
+ const nextWord = words[k + 1];
+ let unit:DurationUnit;
- if (nextWord && DURATION_UNITS_LIST.includes(nextWord)) {
+ if (nextWord && isDurationUnit(nextWord)) {
// Next word is a valid unit, use it and remember as last explicit unit
unit = nextWord;
lastExplicitUnit = nextWord;
@@ -162,7 +184,7 @@ function calculateFromWords(string, opts) {
}
// Parse 3:41:59 and return 3 hours 41 minutes 59 seconds
-function filterByType(string, opts) {
+function filterByType(string:string, opts:ChronicDurationOptions):string {
const chronoUnitsList = DURATION_UNITS_LIST.filter(function (value) {
if (value === 'weeks') { return false; }
if (opts.ignoreSecondsWhenColonSeperated && value === 'seconds') { return false; }
@@ -175,7 +197,7 @@ function filterByType(string, opts) {
.replace(/ +/g, '')
.search(RegExp(`${FLOAT_MATCHER.source}(:${FLOAT_MATCHER.source})+`, 'g')) >= 0
) {
- const res = [];
+ const res:string[] = [];
string
.replace(/ +/g, '')
.split(':')
@@ -193,8 +215,8 @@ function filterByType(string, opts) {
// Get rid of unknown words and map found
// words to defined time units
-function filterThroughWhiteList(string, opts) {
- const res = [];
+function filterThroughWhiteList(string:string, opts:ChronicDurationOptions):string {
+ const res:(string|number)[] = [];
string.split(' ').forEach((word) => {
if (word === '') {
return;
@@ -213,13 +235,13 @@ function filterThroughWhiteList(string, opts) {
}
});
// add '1' at front if string starts with something recognizable but not with a number, like 'day' or 'minute 30sec'
- if (res.length > 0 && MAPPINGS[res[0]]) {
+ if (res.length > 0 && typeof res[0] === 'string' && MAPPINGS[res[0]]) {
res.splice(0, 0, 1);
}
return res.join(' ');
}
-function cleanup(string, opts) {
+function cleanup(string:string, opts:ChronicDurationOptions):string {
let res = string.toLowerCase();
res = filterByType(res, opts);
res = res
@@ -229,7 +251,12 @@ function cleanup(string, opts) {
return filterThroughWhiteList(res, opts);
}
-function humanizeTimeUnit(number, unit, pluralize, keepZero) {
+function humanizeTimeUnit(
+ number:string,
+ unit:string|undefined,
+ pluralize?:boolean,
+ keepZero?:boolean
+):string|null {
if (number === '0' && !keepZero) {
return null;
}
@@ -247,15 +274,15 @@ function humanizeTimeUnit(number, unit, pluralize, keepZero) {
// Given a string representation of elapsed time,
// return an integer (or float, if fractions of a
// second are input)
-export function parseChronicDuration(string, opts = {}) {
+export function parseChronicDuration(string:string, opts:ChronicDurationOptions = {}):number|null {
const result = calculateFromWords(cleanup(string, opts), opts);
return !opts.keepZero && result === 0 ? null : result;
}
// Given an integer and an optional format,
// returns a formatted string representing elapsed time
-export function outputChronicDuration(seconds, opts = {}) {
- const units = {
+export function outputChronicDuration(seconds:number, opts:ChronicDurationOptions = {}):string|null {
+ const units:Record<DurationUnit, number> = {
years: 0,
months: 0,
weeks: 0,
@@ -317,9 +344,9 @@ export function outputChronicDuration(seconds, opts = {}) {
}
let joiner = opts.joiner || ' ';
- let process = null;
+ let process:((str:string) => string)|null = null;
- let dividers;
+ let dividers:Dividers;
switch (opts.format) {
case 'micro':
dividers = {
@@ -373,7 +400,7 @@ export function outputChronicDuration(seconds, opts = {}) {
}
units.hours += (((units.minutes * 60) + units.seconds) / 3600.0);
- units.hours = parseFloat(Math.round(units.hours * 100)) / 100;
+ units.hours = parseFloat(Math.round(units.hours * 100) / 100 + '');
break;
case 'hours_only':
@@ -382,7 +409,7 @@ export function outputChronicDuration(seconds, opts = {}) {
keepZero: true,
};
- units.hours = parseFloat(Math.round(units.hours * 100)) / 100;
+ units.hours = parseFloat(Math.round(units.hours * 100) / 100 + '');
break;
case 'chrono':
@@ -402,7 +429,7 @@ export function outputChronicDuration(seconds, opts = {}) {
// Get rid of lead off zero
// Get rid of trailing:
const divider = ':';
- const processed = [];
+ const processed:string[] = [];
str.split(divider).forEach((n) => {
if (n === '') {
return;
@@ -411,8 +438,8 @@ export function outputChronicDuration(seconds, opts = {}) {
if (n.search('\\.') >= 0) {
processed.push(
parseFloat(n)
- .toFixed(decimalPlaces)
- .padStart(3 + decimalPlaces, '0'),
+ .toFixed(decimalPlaces ?? 0)
+ .padStart(3 + (decimalPlaces ?? 0), '0'),
);
} else {
processed.push(n.padStart(2, '0'));
@@ -440,19 +467,17 @@ export function outputChronicDuration(seconds, opts = {}) {
break;
}
- let result = [];
- ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'].forEach((t) => {
+ let result:string[] = [];
+ [...DURATION_UNITS_LIST].reverse().forEach((t) => {
if (t === 'weeks' && !opts.weeks) {
return;
}
let num = units[t];
if (t === 'seconds' && num % 0 !== 0) {
- num = num.toFixed(decimalPlaces);
- } else {
- num = num.toString();
+ num = parseFloat(num.toFixed(decimalPlaces ?? 0));
}
const keepZero = !dividers.keepZero && t === 'seconds' ? opts.keepZero : dividers.keepZero;
- const humanized = humanizeTimeUnit(num, dividers[t], dividers.pluralize, keepZero);
+ const humanized = humanizeTimeUnit(num.toString(), dividers[t], dividers.pluralize, keepZero);
if (humanized !== null) {
result.push(humanized);
}
@@ -462,11 +487,11 @@ export function outputChronicDuration(seconds, opts = {}) {
result = result.slice(0, opts.units);
}
- result = result.join(joiner);
+ let resultStr = result.join(joiner);
if (process) {
- result = process(result);
+ resultStr = process(resultStr);
}
- return result.length === 0 ? null : result;
+ return resultStr.length === 0 ? null : resultStr;
}