@messageformat/date-skeleton
Advanced tools
Comparing version
@@ -29,3 +29,3 @@ import { DateFormatError } from './options.js'; | ||
*/ | ||
export declare function getDateFormatter(locales: string | string[], tokens: string | DateToken[], onError?: (error: DateFormatError) => void): (date: Date | number) => string; | ||
export declare function getDateFormatter(locales: string | string[], tokens: string | DateToken[], timeZone?: string | ((error: DateFormatError) => void), onError?: (error: DateFormatError) => void): (date: Date | number) => string; | ||
/** | ||
@@ -70,2 +70,2 @@ * Returns a string of JavaScript source that evaluates to a date formatter | ||
*/ | ||
export declare function getDateFormatterSource(locales: string | string[], tokens: string | DateToken[], onError?: (err: DateFormatError) => void): string; | ||
export declare function getDateFormatterSource(locales: string | string[], tokens: string | DateToken[], timeZone?: string | ((err: DateFormatError) => void), onError?: (err: DateFormatError) => void): string; |
@@ -29,6 +29,10 @@ import { getDateFormatOptions } from './options.js'; | ||
*/ | ||
export function getDateFormatter(locales, tokens, onError) { | ||
export function getDateFormatter(locales, tokens, timeZone, onError) { | ||
if (typeof tokens === 'string') | ||
tokens = parseDateTokens(tokens); | ||
const opt = getDateFormatOptions(tokens, onError); | ||
if (typeof timeZone === 'function') { | ||
onError = timeZone; | ||
timeZone = undefined; | ||
} | ||
const opt = getDateFormatOptions(tokens, timeZone, onError); | ||
const dtf = new Intl.DateTimeFormat(locales, opt); | ||
@@ -76,6 +80,10 @@ return (date) => dtf.format(date); | ||
*/ | ||
export function getDateFormatterSource(locales, tokens, onError) { | ||
export function getDateFormatterSource(locales, tokens, timeZone, onError) { | ||
if (typeof tokens === 'string') | ||
tokens = parseDateTokens(tokens); | ||
const opt = getDateFormatOptions(tokens, onError); | ||
if (typeof timeZone === 'function') { | ||
onError = timeZone; | ||
timeZone = undefined; | ||
} | ||
const opt = getDateFormatOptions(tokens, timeZone, onError); | ||
const lines = [ | ||
@@ -82,0 +90,0 @@ `(function() {`, |
@@ -20,2 +20,2 @@ import { DateToken } from './tokens.js'; | ||
} | ||
export declare function getDateFormatOptions(tokens: DateToken[], onError?: (error: DateFormatError) => void): Intl.DateTimeFormatOptions; | ||
export declare function getDateFormatOptions(tokens: DateToken[], timeZone?: string, onError?: (error: DateFormatError) => void): Intl.DateTimeFormatOptions; |
@@ -54,4 +54,5 @@ /** | ||
const { char, desc, width } = token; | ||
if (char === 'd') | ||
if (char === 'd') { | ||
return numeric(width); | ||
} | ||
else { | ||
@@ -139,6 +140,8 @@ onError(`${desc} is not supported`); | ||
} | ||
export function getDateFormatOptions(tokens, onError = error => { | ||
export function getDateFormatOptions(tokens, timeZone, onError = error => { | ||
throw error; | ||
}) { | ||
const options = {}; | ||
const options = { | ||
timeZone | ||
}; | ||
const fields = []; | ||
@@ -145,0 +148,0 @@ for (const token of tokens) { |
@@ -1,2 +0,2 @@ | ||
export declare type DateField = 'era' | 'year' | 'quarter' | 'month' | 'week' | 'day' | 'weekday' | 'period' | 'hour' | 'min' | 'sec' | 'sec-frac' | 'ms' | 'tz'; | ||
export type DateField = 'era' | 'year' | 'quarter' | 'month' | 'week' | 'day' | 'weekday' | 'period' | 'hour' | 'min' | 'sec' | 'sec-frac' | 'ms' | 'tz'; | ||
export declare const fields: { | ||
@@ -13,3 +13,3 @@ [symbol: string]: { | ||
*/ | ||
export declare type DateToken = { | ||
export type DateToken = { | ||
char: string; | ||
@@ -16,0 +16,0 @@ error?: Error; |
{ | ||
"name": "@messageformat/date-skeleton", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A parser & formatter for ICU DateFormat skeleton strings", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
"type": "git", | ||
"url": "https://github.com/messageformat/messageformat.git", | ||
"url": "git+https://github.com/messageformat/messageformat.git", | ||
"directory": "packages/date-skeleton" | ||
@@ -23,0 +23,0 @@ }, |
@@ -21,19 +21,21 @@ # @messageformat/date-skeleton | ||
| Class | Description | | ||
| ----------------------------------------------------------------------------------------------------------------------------- | ------------------------ | | ||
| [DateFormatError](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.dateformaterror.md) | Parent class for errors. | | ||
| Class | Description | | ||
| --------------------------------------------------------------------------------------------------- | ------------------------ | | ||
| [DateFormatError](https://messageformat.github.io/messageformat/api/date-skeleton.dateformaterror/) | Parent class for errors. | | ||
## Functions | ||
| Function | Description | | ||
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| [getDateFormatter(locales, tokens, onError)](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.getdateformatter.md) | Returns a date formatter function for the given locales and date skeleton | | ||
| [getDateFormatterSource(locales, tokens, onError)](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.getdateformattersource.md) | Returns a string of JavaScript source that evaluates to a date formatter function with the same <code>(date: Date | number) => string</code> signature as the function returned by [getDateFormatter()](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.getdateformatter.md)<!-- -->. | | ||
| [parseDateTokens(src)](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.parsedatetokens.md) | Parse an [ICU DateFormat skeleton](http://userguide.icu-project.org/formatparse/datetime) string into a [DateToken](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.datetoken.md) array. | | ||
| Function | Description | | ||
| ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| [getDateFormatter(locales, tokens, onError)](https://messageformat.github.io/messageformat/api/date-skeleton.getdateformatter/) | Returns a date formatter function for the given locales and date skeleton | | ||
| [getDateFormatter(locales, tokens, timeZone, onError)](https://messageformat.github.io/messageformat/api/date-skeleton.getdateformatter/) | Returns a date formatter function for the given locales and date skeleton with time zone overrride. | | ||
| [getDateFormatterSource(locales, tokens, onError)](https://messageformat.github.io/messageformat/api/date-skeleton.getdateformattersource/) | Returns a string of JavaScript source that evaluates to a date formatter function with the same <code>(date: Date | number) => string</code> signature as the function returned by [getDateFormatter()](https://github.com/messageformat/messageformat/blob/main/docs/api/date-skeleton.getdateformatter.md)<!-- -->. | | ||
| [getDateFormatterSource(locales, tokens, timeZone, onError)](https://messageformat.github.io/messageformat/api/date-skeleton.getdateformattersource/) | Returns a string of JavaScript source that evaluates to a date formatter function with the same <code>(date: Date | number) => string</code> signature as the function returned by [getDateFormatter()](https://github.com/messageformat/messageformat/blob/main/docs/api/date-skeleton.getdateformatter.md) with time zone override <!-- -->. | | ||
| [parseDateTokens(src)](https://messageformat.github.io/messageformat/api/date-skeleton.parsedatetokens/) | Parse an [ICU DateFormat skeleton](http://userguide.icu-project.org/formatparse/datetime) string into a [DateToken](https://github.com/messageformat/messageformat/blob/main/docs/api/date-skeleton.datetoken.md) array. | | ||
## Type Aliases | ||
| Type Alias | Description | | ||
| ----------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | | ||
| [DateToken](https://github.com/messageformat/skeletons/blob/master/docs/messageformat-date-skeleton.datetoken.md) | An object representation of a parsed date skeleton token | | ||
| Type Alias | Description | | ||
| --------------------------------------------------------------------------------------- | -------------------------------------------------------- | | ||
| [DateToken](https://messageformat.github.io/messageformat/api/date-skeleton.datetoken/) | An object representation of a parsed date skeleton token | | ||
@@ -45,3 +47,3 @@ --- | ||
[messageformat]: https://messageformat.github.io/ | ||
[code of conduct]: https://github.com/openjs-foundation/cross-project-council/blob/master/CODE_OF_CONDUCT.md | ||
[code of conduct]: https://code-of-conduct.openjsf.org/ | ||
@@ -48,0 +50,0 @@ <a href="https://openjsf.org"> |
28176
4.46%585
1.92%51
4.08%