Skip to content

Commit

Permalink
Clean types a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Oct 26, 2023
1 parent 572e4b5 commit c4910dd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/TIMEZONES.js → src/TIMEZONES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,4 @@ export default [
'Pacific/Tongatapu',
'Pacific/Wake',
'Pacific/Wallis',
];
] as const;
1 change: 0 additions & 1 deletion src/languages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ type TranslationFlatObject = {

export type {
TranslationBase,
TranslateType,
TranslationPaths,
EnglishTranslation,
TranslationFlatObject,
Expand Down
29 changes: 12 additions & 17 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
import * as Localize from './Localize';
import * as CurrentDate from './actions/CurrentDate';
import {Timezone} from '../types/onyx/PersonalDetails';
import TIMEZONES from '../TIMEZONES';
import {Timezone, SelectedTimezone} from '../types/onyx/PersonalDetails';

type Locale = ValueOf<typeof CONST.LOCALES>;

let currentUserAccountID: number | undefined;
Onyx.connect({
Expand Down Expand Up @@ -60,7 +61,7 @@ Onyx.connect({
/**
* Gets the locale string and setting default locale for date-fns
*/
function setLocale(localeString: ValueOf<typeof CONST.LOCALES>) {
function setLocale(localeString: Locale) {
switch (localeString) {
case CONST.LOCALES.EN:
setDefaultOptions({locale: enGB});
Expand All @@ -77,7 +78,7 @@ function setLocale(localeString: ValueOf<typeof CONST.LOCALES>) {
* Gets the user's stored time zone NVP and returns a localized
* Date object for the given ISO-formatted datetime string
*/
function getLocalDateFromDatetime(locale: ValueOf<typeof CONST.LOCALES>, datetime: string, currentSelectedTimezone: (typeof TIMEZONES)[number] = timezone.selected): Date {
function getLocalDateFromDatetime(locale: Locale, datetime: string, currentSelectedTimezone: SelectedTimezone = timezone.selected): Date {
setLocale(locale);
if (!datetime) {
return utcToZonedTime(new Date(), currentSelectedTimezone);
Expand All @@ -93,7 +94,7 @@ function getLocalDateFromDatetime(locale: ValueOf<typeof CONST.LOCALES>, datetim
* @param timeZone - The time zone to consider.
* @returns True if the date is today; otherwise, false.
*/
function isToday(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean {
function isToday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const currentDateInTimeZone = utcToZonedTime(currentDate, timeZone);
return isSameDay(date, currentDateInTimeZone);
Expand All @@ -106,7 +107,7 @@ function isToday(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean {
* @param timeZone - The time zone to consider.
* @returns True if the date is tomorrow; otherwise, false.
*/
function isTomorrow(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean {
function isTomorrow(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const tomorrow = addDays(currentDate, 1); // Get the date for tomorrow in the current time zone
const tomorrowInTimeZone = utcToZonedTime(tomorrow, timeZone);
Expand All @@ -120,7 +121,7 @@ function isTomorrow(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean {
* @param timeZone - The time zone to consider.
* @returns True if the date is yesterday; otherwise, false.
*/
function isYesterday(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean {
function isYesterday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const yesterday = subDays(currentDate, 1); // Get the date for yesterday in the current time zone
const yesterdayInTimeZone = utcToZonedTime(yesterday, timeZone);
Expand All @@ -135,13 +136,7 @@ function isYesterday(date: Date, timeZone: (typeof TIMEZONES)[number]): boolean
* Jan 20 at 5:30 PM within the past year
* Jan 20, 2019 at 5:30 PM anything over 1 year ago
*/
function datetimeToCalendarTime(
locale: ValueOf<typeof CONST.LOCALES>,
datetime: string,
includeTimeZone = false,
currentSelectedTimezone: (typeof TIMEZONES)[number] = timezone.selected,
isLowercase = false,
): string {
function datetimeToCalendarTime(locale: Locale, datetime: string, includeTimeZone = false, currentSelectedTimezone: SelectedTimezone = timezone.selected, isLowercase = false): string {
const date = getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone);
const tz = includeTimeZone ? ' [UTC]Z' : '';
let todayAt = Localize.translate(locale, 'common.todayAt');
Expand Down Expand Up @@ -186,7 +181,7 @@ function datetimeToCalendarTime(
* Jan 20 within the past year
* Jan 20, 2019 anything over 1 year
*/
function datetimeToRelative(locale: ValueOf<typeof CONST.LOCALES>, datetime: string): string {
function datetimeToRelative(locale: Locale, datetime: string): string {
const date = getLocalDateFromDatetime(locale, datetime);
return formatDistanceToNow(date, {addSuffix: true});
}
Expand All @@ -204,7 +199,7 @@ function datetimeToRelative(locale: ValueOf<typeof CONST.LOCALES>, datetime: str
* @param selectedTimezone
* @returns
*/
function getZoneAbbreviation(datetime: string, selectedTimezone: (typeof TIMEZONES)[number]): string {
function getZoneAbbreviation(datetime: string, selectedTimezone: SelectedTimezone): string {
return formatInTimeZone(datetime, selectedTimezone, 'zzz');
}

Expand Down Expand Up @@ -258,7 +253,7 @@ function startCurrentDateUpdater() {
function getCurrentTimezone(): Required<Timezone> {
const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (timezone.automatic && timezone.selected !== currentTimezone) {
return {...timezone, selected: currentTimezone};
return {...timezone, selected: currentTimezone as SelectedTimezone};
}
return timezone;
}
Expand Down
8 changes: 5 additions & 3 deletions src/libs/LocaleDigitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import CONST from '../CONST';

import * as NumberFormatUtils from './NumberFormatUtils';

type Locale = ValueOf<typeof CONST.LOCALES>;

const STANDARD_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', ','];

const INDEX_DECIMAL = 10;
const INDEX_MINUS_SIGN = 11;
const INDEX_GROUP = 12;

const getLocaleDigits = _.memoize((locale: ValueOf<typeof CONST.LOCALES>): string[] => {
const getLocaleDigits = _.memoize((locale: Locale): string[] => {
const localeDigits = [...STANDARD_DIGITS];
for (let i = 0; i <= 9; i++) {
localeDigits[i] = NumberFormatUtils.format(locale, i);
Expand Down Expand Up @@ -41,7 +43,7 @@ const getLocaleDigits = _.memoize((locale: ValueOf<typeof CONST.LOCALES>): strin
*
* @throws If `digit` is not a valid standard digit.
*/
function toLocaleDigit(locale: ValueOf<typeof CONST.LOCALES>, digit: string): string {
function toLocaleDigit(locale: Locale, digit: string): string {
const index = STANDARD_DIGITS.indexOf(digit);
if (index < 0) {
throw new Error(`"${digit}" must be in ${JSON.stringify(STANDARD_DIGITS)}`);
Expand All @@ -57,7 +59,7 @@ function toLocaleDigit(locale: ValueOf<typeof CONST.LOCALES>, digit: string): st
*
* @throws If `localeDigit` is not a valid locale digit.
*/
function fromLocaleDigit(locale: ValueOf<typeof CONST.LOCALES>, localeDigit: string): string {
function fromLocaleDigit(locale: Locale, localeDigit: string): string {
const index = getLocaleDigits(locale).indexOf(localeDigit);
if (index < 0) {
throw new Error(`"${localeDigit}" must be in ${JSON.stringify(getLocaleDigits(locale))}`);
Expand Down
6 changes: 4 additions & 2 deletions src/types/onyx/PersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import TIMEZONES from '../../TIMEZONES';

type SelectedTimezone = (typeof TIMEZONES)[number];

type Timezone = {
/** Value of selected timezone */
selected?: (typeof TIMEZONES)[number];
selected?: SelectedTimezone;

/** Whether timezone is automatically set */
automatic?: boolean;
Expand Down Expand Up @@ -46,6 +48,6 @@ type PersonalDetails = {
timezone?: Timezone;
};

export type {Timezone};
export type {Timezone, SelectedTimezone};

export default PersonalDetails;

0 comments on commit c4910dd

Please sign in to comment.