Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export function init(opts: ConfigureOptions) {
if ('time' in formats) {
Object.assign(options.formats.time, formats.time)
}
if ('dateTime' in formats) {
Object.assign(options.formats.dateTime, formats.dateTime)
}
}

return $locale.set(initialLocale)
Expand Down
22 changes: 21 additions & 1 deletion src/includes/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getOptions, getCurrentLocale } from './utils';
import { monadicMemoize } from './memoize'

const getIntlFormatterOptions = (
type: 'time' | 'number' | 'date',
type: 'time' | 'number' | 'date' | 'dateTime',
name: string
): any => {
const { formats } = getOptions();
Expand Down Expand Up @@ -67,3 +67,23 @@ export const getTimeFormatter: MemoizedIntlFormatter<

return new Intl.DateTimeFormat(locale, options)
})

export const getDateTimeFormatter: MemoizedIntlFormatter<
Intl.DateTimeFormat,
Intl.DateTimeFormatOptions
> = monadicMemoize(({ locale, format, ...options } = {}) => {
locale = locale || getCurrentLocale()
if (locale == null) {
throw new Error(
'[precompile-intl-runtime] A "locale" must be set to format datetime values'
)
}

if (format) {
options = getIntlFormatterOptions('dateTime', format);
} else if (Object.keys(options).length === 0) {
options = getIntlFormatterOptions('dateTime', 'short')
}

return new Intl.DateTimeFormat(locale, options)
})
55 changes: 33 additions & 22 deletions src/includes/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ interface Formats {
number: Record<string, any>
date: Record<string, any>
time: Record<string, any>
dateTime: Record<string, any>
}
interface Options {
fallbackLocale: string
Expand All @@ -12,35 +13,45 @@ interface Options {
warnOnMissingMessages: boolean
}

export const dateFormats = {
short: { month: 'numeric', day: 'numeric', year: '2-digit' },
medium: { month: 'short', day: 'numeric', year: 'numeric' },
long: { month: 'long', day: 'numeric', year: 'numeric' },
full: { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' },
} as const;

export const timeFormats = {
short: { hour: 'numeric', minute: 'numeric' },
medium: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
long: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
full: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
} as const;

const dateTimeFormat = Object.fromEntries(
(["short", "medium", "long", "full"] as const)
.map(f => [f, { dateStyle: dateFormats[f], timeStyle: timeFormats[f] }])
);

export const defaultFormats: Formats = {
number: {
scientific: { notation: 'scientific' },
engineering: { notation: 'engineering' },
compactLong: { notation: 'compact', compactDisplay: 'long' },
compactShort: { notation: 'compact', compactDisplay: 'short' },
},
date: {
short: { month: 'numeric', day: 'numeric', year: '2-digit' },
medium: { month: 'short', day: 'numeric', year: 'numeric' },
long: { month: 'long', day: 'numeric', year: 'numeric' },
full: { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' },
},
time: {
short: { hour: 'numeric', minute: 'numeric' },
medium: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
long: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
full: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
},
date: dateFormats,
time: timeFormats,
dateTime: dateTimeFormat,
}

const defaultOptions: Options = {
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { $isLoading as isLoading } from './stores/loading'
import {
formatTime,
formatDate,
formatDateTime,
formatNumber
} from './stores/formatters';
export {
Expand All @@ -33,6 +34,7 @@ export {
$formatDate as date,
$formatNumber as number,
$formatTime as time,
$formatDateTime as dateTime,
$getJSON as json,
} from './stores/formatters'

Expand All @@ -41,6 +43,7 @@ export {
getDateFormatter,
getNumberFormatter,
getTimeFormatter,
getDateTimeFormatter,
} from './includes/formatters'


Expand Down Expand Up @@ -80,3 +83,7 @@ export function __date(value: Date, format = "short"): string {
export function __time(value: Date, format = "short"): string {
return formatTime(getCurrentLocale(), value, { format });
}

export function __dateTime(value: Date, format = "short"): string {
return formatDateTime(getCurrentLocale(), value, { format });
}
8 changes: 8 additions & 0 deletions src/stores/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MessageFormatter,
TimeFormatter,
DateFormatter,
DateTimeFormatter,
NumberFormatter,
JsonGetter,
} from '../types/index'
Expand All @@ -12,6 +13,7 @@ import { hasLocaleQueue } from '../includes/loaderQueue'
import {
getTimeFormatter,
getDateFormatter,
getDateTimeFormatter,
getNumberFormatter,
} from '../includes/formatters'
import { getOptions, getCurrentLocale, getPossibleLocales } from '../includes/utils';
Expand Down Expand Up @@ -74,6 +76,11 @@ export const formatDate: DateFormatter = (currentLocale, d, options) => {
return getDateFormatter({ locale, ...options }).format(d);
}

export const formatDateTime: DateTimeFormatter = (currentLocale, dt, options) => {
const locale = currentLocale || getCurrentLocale();
return getDateTimeFormatter({ locale, ...options }).format(dt)
}

export const formatNumber: NumberFormatter = (currentLocale, n, options) => {
const locale = currentLocale || getCurrentLocale();
return getNumberFormatter({ locale, ...options }).format(n)
Expand All @@ -82,5 +89,6 @@ export const formatNumber: NumberFormatter = (currentLocale, n, options) => {
export const $format = /*@__PURE__*/derived([$locale, $dictionary], ([currentLocale]) => formatMessage.bind(null, currentLocale));
export const $formatTime = /*@__PURE__*/derived([$locale], ([currentLocale]) => formatTime.bind(null, currentLocale));
export const $formatDate = /*@__PURE__*/derived([$locale], ([currentLocale]) => formatDate.bind(null, currentLocale));
export const $formatDateTime = /*@__PURE__*/derived([$locale], ([currentLocale]) => formatDateTime.bind(null, currentLocale));
export const $formatNumber = /*@__PURE__*/derived([$locale], ([currentLocale]) => formatNumber.bind(null, currentLocale));
export const $getJSON = /*@__PURE__*/derived([$locale, $dictionary], () => getJSON);
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export type DateFormatter = (
options?: IntlFormatterOptions<Intl.DateTimeFormatOptions>
) => string

export type DateTimeFormatter = (
currentLocale: string,
d: Date | number,
options?: IntlFormatterOptions<Intl.DateTimeFormatOptions>
) => string

export type NumberFormatter = (
currentLocale: string,
d: number,
Expand Down