-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
384 additions
and
292 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
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
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,106 @@ | ||
/* | ||
* Copyright 2015, Yahoo Inc. | ||
* Copyrights licensed under the New BSD License. | ||
* See the accompanying LICENSE file for terms. | ||
*/ | ||
|
||
import {Formatters, IntlConfig, IntlFormatters} from '../types'; | ||
|
||
import {createError, filterProps, getNamedFormat} from '../utils'; | ||
|
||
const DATE_TIME_FORMAT_OPTIONS: Array<keyof Intl.DateTimeFormatOptions> = [ | ||
'localeMatcher', | ||
'formatMatcher', | ||
|
||
'timeZone', | ||
'hour12', | ||
|
||
'weekday', | ||
'era', | ||
'year', | ||
'month', | ||
'day', | ||
'hour', | ||
'minute', | ||
'second', | ||
'timeZoneName', | ||
]; | ||
|
||
function getFormatter( | ||
{ | ||
locale, | ||
formats, | ||
onError, | ||
timeZone, | ||
}: Pick<IntlConfig, 'locale' | 'formats' | 'onError' | 'timeZone'>, | ||
type: 'date' | 'time', | ||
getDateTimeFormat: Formatters['getDateTimeFormat'], | ||
options: Parameters<IntlFormatters['formatDate']>[1] = {} | ||
) { | ||
const {format} = options; | ||
let defaults = { | ||
...(timeZone && {timeZone}), | ||
...(format && getNamedFormat(formats!, type, format, onError)), | ||
}; | ||
|
||
let filteredOptions = filterProps( | ||
options, | ||
DATE_TIME_FORMAT_OPTIONS, | ||
defaults | ||
); | ||
|
||
if ( | ||
type === 'time' && | ||
!filteredOptions.hour && | ||
!filteredOptions.minute && | ||
!filteredOptions.second | ||
) { | ||
// Add default formatting options if hour, minute, or second isn't defined. | ||
filteredOptions = {...filteredOptions, hour: 'numeric', minute: 'numeric'}; | ||
} | ||
|
||
return getDateTimeFormat(locale, filteredOptions); | ||
} | ||
|
||
export function formatDateFactory( | ||
config: Pick<IntlConfig, 'locale' | 'formats' | 'onError' | 'timeZone'>, | ||
getDateTimeFormat: Formatters['getDateTimeFormat'] | ||
) { | ||
return ( | ||
value?: Parameters<IntlFormatters['formatDate']>[0], | ||
options: Parameters<IntlFormatters['formatDate']>[1] = {} | ||
) => { | ||
const date = typeof value === 'string' ? new Date(value || 0) : value; | ||
try { | ||
return getFormatter(config, 'date', getDateTimeFormat, options).format( | ||
date | ||
); | ||
} catch (e) { | ||
config.onError(createError('Error formatting date.', e)); | ||
} | ||
|
||
return String(date); | ||
}; | ||
} | ||
|
||
export function formatTimeFactory( | ||
config: Pick<IntlConfig, 'locale' | 'formats' | 'onError' | 'timeZone'>, | ||
getDateTimeFormat: Formatters['getDateTimeFormat'] | ||
) { | ||
return ( | ||
value?: Parameters<IntlFormatters['formatTime']>[0], | ||
options: Parameters<IntlFormatters['formatTime']>[1] = {} | ||
) => { | ||
const date = typeof value === 'string' ? new Date(value || 0) : value; | ||
|
||
try { | ||
return getFormatter(config, 'time', getDateTimeFormat, options).format( | ||
date | ||
); | ||
} catch (e) { | ||
config.onError(createError('Error formatting time.', e)); | ||
} | ||
|
||
return String(date); | ||
}; | ||
} |
Oops, something went wrong.