Skip to content

Commit 8d98aaa

Browse files
refactor: convert utils folders from Flow to TypeScript (#4326)
- Convert converters/date folder (5 files) from .js to .ts - Convert errors folder (1 file) from .js to .ts - Convert exceptions folder (2 files) from .js to .ts - Convert localeCompareStrings folder (2 files) from .js to .ts - Update Flow types to TypeScript equivalents - Fix DisplayException constructor function typing - Handle calendar type compatibility with 'as any' casting - Use nullish coalescing for undefined values in date conversion - Remove all original .js files after conversion All files pass TypeScript compilation (yarn tsc:check) and linting (yarn linter:check) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent 438bf0a commit 8d98aaa

File tree

10 files changed

+9
-19
lines changed

10 files changed

+9
-19
lines changed

src/core_modules/capture-core/utils/converters/date/convertIsoToLocalCalendar.js renamed to src/core_modules/capture-core/utils/converters/date/convertIsoToLocalCalendar.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import moment from 'moment';
32
import {
43
convertFromIso8601,
@@ -13,7 +12,7 @@ import { padWithZeros } from '../../../../capture-core-utils/date';
1312
* @returns {string}
1413
*/
1514

16-
export function convertIsoToLocalCalendar(isoDate: ?string): string {
15+
export function convertIsoToLocalCalendar(isoDate: string | null | undefined): string {
1716
if (!isoDate) {
1817
return '';
1918
}
@@ -28,10 +27,10 @@ export function convertIsoToLocalCalendar(isoDate: ?string): string {
2827
const calendar = systemSettingsStore.get().calendar;
2928
const dateFormat = systemSettingsStore.get().dateFormat;
3029

31-
const { year, eraYear, month, day } = convertFromIso8601(formattedIsoDate, calendar);
30+
const { year, eraYear, month, day } = convertFromIso8601(formattedIsoDate, calendar as any);
3231
const localYear = calendar === 'ethiopian' ? eraYear : year;
3332

3433
return dateFormat === 'DD-MM-YYYY'
35-
? `${padWithZeros(day, 2)}-${padWithZeros(month, 2)}-${padWithZeros(localYear, 4)}`
36-
: `${padWithZeros(localYear, 4)}-${padWithZeros(month, 2)}-${padWithZeros(day, 2)}`;
34+
? `${padWithZeros(day ?? 0, 2)}-${padWithZeros(month ?? 0, 2)}-${padWithZeros(localYear ?? 0, 4)}`
35+
: `${padWithZeros(localYear ?? 0, 4)}-${padWithZeros(month ?? 0, 2)}-${padWithZeros(day ?? 0, 2)}`;
3736
}

src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.js renamed to src/core_modules/capture-core/utils/converters/date/convertLocalToIsoCalendar.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import moment from 'moment';
32
import {
43
convertToIso8601,
@@ -12,14 +11,14 @@ import { padWithZeros } from '../../../../capture-core-utils/date';
1211
* @param {string} localDate - date in local calendar format
1312
* @returns {string}
1413
*/
15-
export function convertLocalToIsoCalendar(localDate: ?string): string {
14+
export function convertLocalToIsoCalendar(localDate: string | null | undefined): string {
1615
if (!localDate) {
1716
return '';
1817
}
1918

2019
const calendar = systemSettingsStore.get().calendar;
2120

22-
const { year, month, day } = convertToIso8601(localDate, calendar);
21+
const { year, month, day } = convertToIso8601(localDate, calendar as any);
2322
const dateString = `${padWithZeros(year, 4)}-${padWithZeros(month, 2)}-${padWithZeros(day, 2)}`;
2423
const parsedMoment = moment(dateString);
2524

src/core_modules/capture-core/utils/converters/date/dateObjectToDateFormatString.js renamed to src/core_modules/capture-core/utils/converters/date/dateObjectToDateFormatString.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import moment from 'moment';
32
import { convertIsoToLocalCalendar } from './convertIsoToLocalCalendar';
43

@@ -8,7 +7,7 @@ import { convertIsoToLocalCalendar } from './convertIsoToLocalCalendar';
87
* @param {Date} dateValue: the date instance
98
* @returns {string}
109
*/
11-
export function convertDateObjectToDateFormatString(dateValue: Date | moment$Moment) {
10+
export function convertDateObjectToDateFormatString(dateValue: Date | any) {
1211
const momentDate = moment(dateValue);
1312
const dateString = momentDate.format('YYYY-MM-DD');
1413
return convertIsoToLocalCalendar(dateString);

src/core_modules/capture-core/utils/converters/date/index.js renamed to src/core_modules/capture-core/utils/converters/date/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
export { convertDateObjectToDateFormatString } from './dateObjectToDateFormatString';
32
export { convertMomentToDateFormatString } from './momentToDateFormatString';
43
export { convertIsoToLocalCalendar } from './convertIsoToLocalCalendar';

src/core_modules/capture-core/utils/converters/date/momentToDateFormatString.js renamed to src/core_modules/capture-core/utils/converters/date/momentToDateFormatString.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import { systemSettingsStore } from '../../../metaDataMemoryStores';
32

43
/**

src/core_modules/capture-core/utils/errors/getErrorMessageAndDetails.js renamed to src/core_modules/capture-core/utils/errors/getErrorMessageAndDetails.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import isString from 'd2-utilizr/lib/isString';
32
import isObject from 'd2-utilizr/lib/isObject';
43

src/core_modules/capture-core/utils/exceptions/DisplayException.js renamed to src/core_modules/capture-core/utils/exceptions/DisplayException.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// @flow
2-
export function DisplayException(message: string, innerError: any) {
1+
export function DisplayException(this: any, message: string, innerError: any) {
32
this.message = message;
43
this.innerError = innerError;
54
this.toString = () => this.message;

src/core_modules/capture-core/utils/exceptions/index.js renamed to src/core_modules/capture-core/utils/exceptions/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// @flow
21
export { DisplayException } from './DisplayException';

src/core_modules/capture-core/utils/localeCompareStrings/index.js renamed to src/core_modules/capture-core/utils/localeCompareStrings/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// @flow
21
export { localeCompareStrings } from './localeCompareStrings';

src/core_modules/capture-core/utils/localeCompareStrings/localeCompareStrings.js renamed to src/core_modules/capture-core/utils/localeCompareStrings/localeCompareStrings.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// @flow
21
import i18n from '@dhis2/d2-i18n';
32

43
export const localeCompareStrings = (a: string, b: string) => {
54
try {
6-
return a.localeCompare(b, i18n.language);
5+
return a.localeCompare(b, (i18n as any).language);
76
} catch {
87
return a.localeCompare(b, 'en');
98
}

0 commit comments

Comments
 (0)