forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move joinArray & parseDate to utils (freeCodeCamp#54240)
- Loading branch information
Showing
3 changed files
with
34 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { parseDate, formatYears } from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { TFunction } from 'i18next'; | ||
import { getLangCode } from '../../../../../../shared/config/i18n'; | ||
import envData from '../../../../../config/env.json'; | ||
const { clientLocale } = envData; | ||
|
||
const localeCode = getLangCode(clientLocale); | ||
|
||
const formatYears = (array: string[], t: TFunction): string => { | ||
return array.reduce((string, item, index, array) => { | ||
if (string.length > 0) { | ||
if (index === array.length - 1) { | ||
return `${string} ${t('misc.and')} ${item}`; | ||
} else { | ||
return `${string}, ${item}`; | ||
} | ||
} else { | ||
return item; | ||
} | ||
}); | ||
}; | ||
|
||
const parseDate = (joinDate: string, t: TFunction): string => { | ||
const convertedJoinDate = new Date(joinDate); | ||
const date = convertedJoinDate.toLocaleString([localeCode, 'en-US'], { | ||
year: 'numeric', | ||
month: 'long' | ||
}); | ||
return t('profile.joined', { date: date }); | ||
}; | ||
|
||
export { formatYears, parseDate }; |