use-intl 2.20.2 → 2.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ type RelativeTimeFormatOptions = {
2
+ now?: number | Date;
3
+ unit?: Intl.RelativeTimeFormatUnit;
4
+ };
5
+ export default RelativeTimeFormatOptions;
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
2
2
  import Formats from './Formats';
3
3
  import IntlError from './IntlError';
4
4
  import NumberFormatOptions from './NumberFormatOptions';
5
+ import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
5
6
  import TimeZone from './TimeZone';
6
7
  type Props = {
7
8
  locale: string;
@@ -13,7 +14,7 @@ type Props = {
13
14
  export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
14
15
  dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
15
16
  number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
16
- relativeTime: (date: number | Date, now?: number | Date) => string;
17
+ relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
17
18
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
18
19
  };
19
20
  export {};
@@ -3,5 +3,5 @@ import createFormatter from './createFormatter';
3
3
  export default function createIntl(...args: Parameters<typeof createFormatter>): {
4
4
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
5
5
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
6
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
6
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("./RelativeTimeFormatOptions").default | undefined) => string;
7
7
  };
@@ -2,43 +2,53 @@ import { extends as _extends } from '../_virtual/use-intl.esm.js';
2
2
  import IntlError, { IntlErrorCode } from './use-intl.esm.js';
3
3
  import { defaultOnError } from './use-intl.esm6.js';
4
4
 
5
- var MINUTE = 60;
5
+ var SECOND = 1;
6
+ var MINUTE = SECOND * 60;
6
7
  var HOUR = MINUTE * 60;
7
8
  var DAY = HOUR * 24;
8
9
  var WEEK = DAY * 7;
9
10
  var MONTH = DAY * (365 / 12); // Approximation
11
+ var QUARTER = MONTH * 3;
10
12
  var YEAR = DAY * 365;
11
- function getRelativeTimeFormatConfig(seconds) {
13
+ var UNIT_SECONDS = {
14
+ second: SECOND,
15
+ seconds: SECOND,
16
+ minute: MINUTE,
17
+ minutes: MINUTE,
18
+ hour: HOUR,
19
+ hours: HOUR,
20
+ day: DAY,
21
+ days: DAY,
22
+ week: WEEK,
23
+ weeks: WEEK,
24
+ month: MONTH,
25
+ months: MONTH,
26
+ quarter: QUARTER,
27
+ quarters: QUARTER,
28
+ year: YEAR,
29
+ years: YEAR
30
+ };
31
+ function resolveRelativeTimeUnit(seconds) {
12
32
  var absValue = Math.abs(seconds);
13
- var value, unit;
14
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
15
- // will include fractions like '2.1 hours ago'.
16
33
  if (absValue < MINUTE) {
17
- unit = 'second';
18
- value = Math.round(seconds);
34
+ return 'second';
19
35
  } else if (absValue < HOUR) {
20
- unit = 'minute';
21
- value = Math.round(seconds / MINUTE);
36
+ return 'minute';
22
37
  } else if (absValue < DAY) {
23
- unit = 'hour';
24
- value = Math.round(seconds / HOUR);
38
+ return 'hour';
25
39
  } else if (absValue < WEEK) {
26
- unit = 'day';
27
- value = Math.round(seconds / DAY);
40
+ return 'day';
28
41
  } else if (absValue < MONTH) {
29
- unit = 'week';
30
- value = Math.round(seconds / WEEK);
42
+ return 'week';
31
43
  } else if (absValue < YEAR) {
32
- unit = 'month';
33
- value = Math.round(seconds / MONTH);
34
- } else {
35
- unit = 'year';
36
- value = Math.round(seconds / YEAR);
44
+ return 'month';
37
45
  }
38
- return {
39
- value: value,
40
- unit: unit
41
- };
46
+ return 'year';
47
+ }
48
+ function calculateRelativeTimeValue(seconds, unit) {
49
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
50
+ // will include fractions like '2.1 hours ago'.
51
+ return Math.round(seconds / UNIT_SECONDS[unit]);
42
52
  }
43
53
  function createFormatter(_ref) {
44
54
  var formats = _ref.formats,
@@ -100,25 +110,32 @@ function createFormatter(_ref) {
100
110
  return new Intl.NumberFormat(locale, options).format(value);
101
111
  });
102
112
  }
113
+ function getGlobalNow() {
114
+ if (globalNow) {
115
+ return globalNow;
116
+ } else {
117
+ onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" : undefined));
118
+ return new Date();
119
+ }
120
+ }
121
+ function extractNowDate(nowOrOptions) {
122
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
123
+ return new Date(nowOrOptions);
124
+ }
125
+ if ((nowOrOptions == null ? void 0 : nowOrOptions.now) !== undefined) {
126
+ return new Date(nowOrOptions.now);
127
+ }
128
+ return getGlobalNow();
129
+ }
103
130
  function relativeTime( /** The date time that needs to be formatted. */
104
131
  date, /** The reference point in time to which `date` will be formatted in relation to. */
105
- now) {
132
+ nowOrOptions) {
106
133
  try {
107
- if (!now) {
108
- if (globalNow) {
109
- now = globalNow;
110
- } else {
111
- onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production' ? "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" : undefined));
112
- }
113
- }
114
- var dateDate = date instanceof Date ? date : new Date(date);
115
- var nowDate = now instanceof Date ? now :
116
- // @ts-expect-error -- `undefined` is fine for the `Date` constructor
117
- new Date(now);
134
+ var dateDate = new Date(date);
135
+ var nowDate = extractNowDate(nowOrOptions);
118
136
  var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
119
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
120
- unit = _getRelativeTimeForma.unit,
121
- value = _getRelativeTimeForma.value;
137
+ var unit = typeof nowOrOptions === 'number' || nowOrOptions instanceof Date || (nowOrOptions == null ? void 0 : nowOrOptions.unit) === undefined ? resolveRelativeTimeUnit(seconds) : nowOrOptions.unit;
138
+ var value = calculateRelativeTimeValue(seconds, unit);
122
139
  return new Intl.RelativeTimeFormat(locale, {
123
140
  numeric: 'auto'
124
141
  }).format(value, unit);
@@ -1 +1 @@
1
- {"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createFormatter.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport TimeZone from './TimeZone';\nimport {defaultOnError} from './defaults';\n\nconst MINUTE = 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst YEAR = DAY * 365;\n\nfunction getRelativeTimeFormatConfig(seconds: number) {\n const absValue = Math.abs(seconds);\n let value, unit: Intl.RelativeTimeFormatUnit;\n\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n\n if (absValue < MINUTE) {\n unit = 'second';\n value = Math.round(seconds);\n } else if (absValue < HOUR) {\n unit = 'minute';\n value = Math.round(seconds / MINUTE);\n } else if (absValue < DAY) {\n unit = 'hour';\n value = Math.round(seconds / HOUR);\n } else if (absValue < WEEK) {\n unit = 'day';\n value = Math.round(seconds / DAY);\n } else if (absValue < MONTH) {\n unit = 'week';\n value = Math.round(seconds / WEEK);\n } else if (absValue < YEAR) {\n unit = 'month';\n value = Math.round(seconds / MONTH);\n } else {\n unit = 'year';\n value = Math.round(seconds / YEAR);\n }\n\n return {value, unit};\n}\n\ntype Props = {\n locale: string;\n timeZone?: TimeZone;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createFormatter({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone: globalTimeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n process.env.NODE_ENV !== 'production'\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function dateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (!options?.timeZone) {\n if (globalTimeZone) {\n options = {...options, timeZone: globalTimeZone};\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`timeZone\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone`\n : undefined\n )\n );\n }\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function number(\n value: number | bigint,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function relativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n now?: number | Date\n ) {\n try {\n if (!now) {\n if (globalNow) {\n now = globalNow;\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`now\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`\n : undefined\n )\n );\n }\n }\n\n const dateDate = date instanceof Date ? date : new Date(date);\n const nowDate =\n now instanceof Date\n ? now\n : // @ts-expect-error -- `undefined` is fine for the `Date` constructor\n new Date(now);\n\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n const {unit, value} = getRelativeTimeFormatConfig(seconds);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n function list(\n value: Iterable<string>,\n formatOrOptions?: string | Intl.ListFormatOptions\n ) {\n return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>\n new Intl.ListFormat(locale, options).format(value)\n );\n }\n\n return {dateTime, number, relativeTime, list};\n}\n"],"names":["MINUTE","HOUR","DAY","WEEK","MONTH","YEAR","getRelativeTimeFormatConfig","seconds","absValue","Math","abs","value","unit","round","createFormatter","_ref","formats","locale","globalNow","now","_ref$onError","onError","defaultOnError","globalTimeZone","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","process","env","NODE_ENV","undefined","getFormattedValue","formatter","String","FORMATTING_ERROR","message","dateTime","_options","_extends","ENVIRONMENT_FALLBACK","Intl","DateTimeFormat","format","number","NumberFormat","relativeTime","date","dateDate","Date","nowDate","getTime","_getRelativeTimeForma","RelativeTimeFormat","numeric","list","ListFormat"],"mappings":";;;;AAOA,IAAMA,MAAM,GAAG,EAAE,CAAA;AACjB,IAAMC,IAAI,GAAGD,MAAM,GAAG,EAAE,CAAA;AACxB,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAE,CAAA;AACrB,IAAME,IAAI,GAAGD,GAAG,GAAG,CAAC,CAAA;AACpB,IAAME,KAAK,GAAGF,GAAG,IAAI,GAAG,GAAG,EAAE,CAAC,CAAC;AAC/B,IAAMG,IAAI,GAAGH,GAAG,GAAG,GAAG,CAAA;AAEtB,SAASI,2BAA2BA,CAACC,OAAe,EAAA;AAClD,EAAA,IAAMC,QAAQ,GAAGC,IAAI,CAACC,GAAG,CAACH,OAAO,CAAC,CAAA;EAClC,IAAII,KAAK,EAAEC,IAAiC,CAAA;AAE5C;AACA;EAEA,IAAIJ,QAAQ,GAAGR,MAAM,EAAE;AACrBY,IAAAA,IAAI,GAAG,QAAQ,CAAA;AACfD,IAAAA,KAAK,GAAGF,IAAI,CAACI,KAAK,CAACN,OAAO,CAAC,CAAA;AAC5B,GAAA,MAAM,IAAIC,QAAQ,GAAGP,IAAI,EAAE;AAC1BW,IAAAA,IAAI,GAAG,QAAQ,CAAA;IACfD,KAAK,GAAGF,IAAI,CAACI,KAAK,CAACN,OAAO,GAAGP,MAAM,CAAC,CAAA;AACrC,GAAA,MAAM,IAAIQ,QAAQ,GAAGN,GAAG,EAAE;AACzBU,IAAAA,IAAI,GAAG,MAAM,CAAA;IACbD,KAAK,GAAGF,IAAI,CAACI,KAAK,CAACN,OAAO,GAAGN,IAAI,CAAC,CAAA;AACnC,GAAA,MAAM,IAAIO,QAAQ,GAAGL,IA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AO,EAAA;AAAA,MAAA,OACtE,IAAImB,IAAI,CAACe,UAAU,CAAC7C,MAAM,EAAEW,OAAO,CAAC,CAACqB,MAAM,CAACtC,KAAK,CAAC,CAAA;KACnD,CAAA,CAAA;AACH,GAAA;EAEA,OAAO;AAACgC,IAAAA,QAAQ,EAARA,QAAQ;AAAEO,IAAAA,MAAM,EAANA,MAAM;AAAEE,IAAAA,YAAY,EAAZA,YAAY;AAAES,IAAAA,IAAI,EAAJA,IAAAA;GAAK,CAAA;AAC/C;;;;"}
1
+ {"version":3,"file":"use-intl.esm3.js","sources":["../../src/core/createFormatter.tsx"],"sourcesContent":["import DateTimeFormatOptions from './DateTimeFormatOptions';\nimport Formats from './Formats';\nimport IntlError, {IntlErrorCode} from './IntlError';\nimport NumberFormatOptions from './NumberFormatOptions';\nimport RelativeTimeFormatOptions from './RelativeTimeFormatOptions';\nimport TimeZone from './TimeZone';\nimport {defaultOnError} from './defaults';\n\nconst SECOND = 1;\nconst MINUTE = SECOND * 60;\nconst HOUR = MINUTE * 60;\nconst DAY = HOUR * 24;\nconst WEEK = DAY * 7;\nconst MONTH = DAY * (365 / 12); // Approximation\nconst QUARTER = MONTH * 3;\nconst YEAR = DAY * 365;\n\nconst UNIT_SECONDS: Record<Intl.RelativeTimeFormatUnit, number> = {\n second: SECOND,\n seconds: SECOND,\n minute: MINUTE,\n minutes: MINUTE,\n hour: HOUR,\n hours: HOUR,\n day: DAY,\n days: DAY,\n week: WEEK,\n weeks: WEEK,\n month: MONTH,\n months: MONTH,\n quarter: QUARTER,\n quarters: QUARTER,\n year: YEAR,\n years: YEAR\n} as const;\n\nfunction resolveRelativeTimeUnit(seconds: number) {\n const absValue = Math.abs(seconds);\n\n if (absValue < MINUTE) {\n return 'second';\n } else if (absValue < HOUR) {\n return 'minute';\n } else if (absValue < DAY) {\n return 'hour';\n } else if (absValue < WEEK) {\n return 'day';\n } else if (absValue < MONTH) {\n return 'week';\n } else if (absValue < YEAR) {\n return 'month';\n }\n return 'year';\n}\n\nfunction calculateRelativeTimeValue(\n seconds: number,\n unit: Intl.RelativeTimeFormatUnit\n) {\n // We have to round the resulting values, as `Intl.RelativeTimeFormat`\n // will include fractions like '2.1 hours ago'.\n return Math.round(seconds / UNIT_SECONDS[unit]);\n}\n\ntype Props = {\n locale: string;\n timeZone?: TimeZone;\n onError?(error: IntlError): void;\n formats?: Partial<Formats>;\n now?: Date;\n};\n\nexport default function createFormatter({\n formats,\n locale,\n now: globalNow,\n onError = defaultOnError,\n timeZone: globalTimeZone\n}: Props) {\n function resolveFormatOrOptions<Options>(\n typeFormats: Record<string, Options> | undefined,\n formatOrOptions?: string | Options\n ) {\n let options;\n if (typeof formatOrOptions === 'string') {\n const formatName = formatOrOptions;\n options = typeFormats?.[formatName];\n\n if (!options) {\n const error = new IntlError(\n IntlErrorCode.MISSING_FORMAT,\n process.env.NODE_ENV !== 'production'\n ? `Format \\`${formatName}\\` is not available. You can configure it on the provider or provide custom options.`\n : undefined\n );\n onError(error);\n throw error;\n }\n } else {\n options = formatOrOptions;\n }\n\n return options;\n }\n\n function getFormattedValue<Value, Options>(\n value: Value,\n formatOrOptions: string | Options | undefined,\n typeFormats: Record<string, Options> | undefined,\n formatter: (options?: Options) => string\n ) {\n let options;\n try {\n options = resolveFormatOrOptions(typeFormats, formatOrOptions);\n } catch (error) {\n return String(value);\n }\n\n try {\n return formatter(options);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(value);\n }\n }\n\n function dateTime(\n /** If a number is supplied, this is interpreted as a UTC timestamp. */\n value: Date | number,\n /** If a time zone is supplied, the `value` is converted to that time zone.\n * Otherwise the user time zone will be used. */\n formatOrOptions?: string | DateTimeFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.dateTime,\n (options) => {\n if (!options?.timeZone) {\n if (globalTimeZone) {\n options = {...options, timeZone: globalTimeZone};\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`timeZone\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#time-zone`\n : undefined\n )\n );\n }\n }\n\n return new Intl.DateTimeFormat(locale, options).format(value);\n }\n );\n }\n\n function number(\n value: number | bigint,\n formatOrOptions?: string | NumberFormatOptions\n ) {\n return getFormattedValue(\n value,\n formatOrOptions,\n formats?.number,\n (options) => new Intl.NumberFormat(locale, options).format(value)\n );\n }\n\n function getGlobalNow() {\n if (globalNow) {\n return globalNow;\n } else {\n onError(\n new IntlError(\n IntlErrorCode.ENVIRONMENT_FALLBACK,\n process.env.NODE_ENV !== 'production'\n ? `The \\`now\\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`\n : undefined\n )\n );\n return new Date();\n }\n }\n\n function extractNowDate(\n nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions\n ) {\n if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {\n return new Date(nowOrOptions);\n }\n if (nowOrOptions?.now !== undefined) {\n return new Date(nowOrOptions.now);\n }\n return getGlobalNow();\n }\n\n function relativeTime(\n /** The date time that needs to be formatted. */\n date: number | Date,\n /** The reference point in time to which `date` will be formatted in relation to. */\n nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions\n ) {\n try {\n const dateDate = new Date(date);\n const nowDate = extractNowDate(nowOrOptions);\n const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;\n\n const unit =\n typeof nowOrOptions === 'number' ||\n nowOrOptions instanceof Date ||\n nowOrOptions?.unit === undefined\n ? resolveRelativeTimeUnit(seconds)\n : nowOrOptions.unit;\n\n const value = calculateRelativeTimeValue(seconds, unit);\n\n return new Intl.RelativeTimeFormat(locale, {\n numeric: 'auto'\n }).format(value, unit);\n } catch (error) {\n onError(\n new IntlError(IntlErrorCode.FORMATTING_ERROR, (error as Error).message)\n );\n return String(date);\n }\n }\n\n function list(\n value: Iterable<string>,\n formatOrOptions?: string | Intl.ListFormatOptions\n ) {\n return getFormattedValue(value, formatOrOptions, formats?.list, (options) =>\n new Intl.ListFormat(locale, options).format(value)\n );\n }\n\n return {dateTime, number, relativeTime, list};\n}\n"],"names":["SECOND","MINUTE","HOUR","DAY","WEEK","MONTH","QUARTER","YEAR","UNIT_SECONDS","second","seconds","minute","minutes","hour","hours","day","days","week","weeks","month","months","quarter","quarters","year","years","resolveRelativeTimeUnit","absValue","Math","abs","calculateRelativeTimeValue","unit","round","createFormatter","_ref","formats","locale","globalNow","now","_ref$onError","onError","defaultOnError","globalTimeZone","timeZone","resolveFormatOrOptions","typeFormats","formatOrOptions","options","formatName","error","IntlError","IntlErrorCode","MISSING_FORMAT","process","env","NODE_ENV","undefined","getFormattedValue","value","formatter","String","FORMATTING_ERROR","message","dateTime","_options","_extends","ENVIRONMENT_FALLBACK","Intl","DateTimeFormat","format","number","NumberFormat","getGlobalNow","Date","extractNowDate","nowOrOptions","relativeTime","date","dateDate","nowDate","getTime","RelativeTimeFormat","numeric","list","ListFormat"],"mappings":";;;;AAQA,IAAMA,MAAM,GAAG,CAAC,CAAA;AAChB,IAAMC,MAAM,GAAGD,MAAM,GAAG,EAAE,CAAA;AAC1B,IAAME,IAAI,GAAGD,MAAM,GAAG,EAAE,CAAA;AACxB,IAAME,GAAG,GAAGD,IAAI,GAAG,EAAE,CAAA;AACrB,IAAME,IAAI,GAAGD,GAAG,GAAG,CAAC,CAAA;AACpB,IAAME,KAAK,GAAGF,GAAG,IAAI,GAAG,GAAG,EAAE,CAAC,CAAC;AAC/B,IAAMG,OAAO,GAAGD,KAAK,GAAG,CAAC,CAAA;AACzB,IAAME,IAAI,GAAGJ,GAAG,GAAG,GAAG,CAAA;AAEtB,IAAMK,YAAY,GAAgD;AAChEC,EAAAA,MAAM,EAAET,MAAM;A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,CAAC,CAACsB,MAAM,CAACX,KAAK,CAAC,CAAA;KACnD,CAAA,CAAA;AACH,GAAA;EAEA,OAAO;AAACK,IAAAA,QAAQ,EAARA,QAAQ;AAAEO,IAAAA,MAAM,EAANA,MAAM;AAAEM,IAAAA,YAAY,EAAZA,YAAY;AAAEO,IAAAA,IAAI,EAAJA,IAAAA;GAAK,CAAA;AAC/C;;;;"}
@@ -1,6 +1,6 @@
1
1
  export default function useFormatter(): {
2
2
  dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
3
  number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
4
- relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
4
+ relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
5
5
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
6
6
  };
@@ -2,5 +2,5 @@
2
2
  export default function useIntl(): {
3
3
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
4
4
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
5
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
6
6
  };
@@ -0,0 +1,5 @@
1
+ type RelativeTimeFormatOptions = {
2
+ now?: number | Date;
3
+ unit?: Intl.RelativeTimeFormatUnit;
4
+ };
5
+ export default RelativeTimeFormatOptions;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=RelativeTimeFormatOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RelativeTimeFormatOptions.js","sourceRoot":"","sources":["../../../src/core/RelativeTimeFormatOptions.tsx"],"names":[],"mappings":""}
@@ -2,6 +2,7 @@ import DateTimeFormatOptions from './DateTimeFormatOptions';
2
2
  import Formats from './Formats';
3
3
  import IntlError from './IntlError';
4
4
  import NumberFormatOptions from './NumberFormatOptions';
5
+ import RelativeTimeFormatOptions from './RelativeTimeFormatOptions';
5
6
  import TimeZone from './TimeZone';
6
7
  type Props = {
7
8
  locale: string;
@@ -13,7 +14,7 @@ type Props = {
13
14
  export default function createFormatter({ formats, locale, now: globalNow, onError, timeZone: globalTimeZone }: Props): {
14
15
  dateTime: (value: Date | number, formatOrOptions?: string | DateTimeFormatOptions) => string;
15
16
  number: (value: number | bigint, formatOrOptions?: string | NumberFormatOptions) => string;
16
- relativeTime: (date: number | Date, now?: number | Date) => string;
17
+ relativeTime: (date: number | Date, nowOrOptions?: RelativeTimeFormatOptions['now'] | RelativeTimeFormatOptions) => string;
17
18
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions) => string;
18
19
  };
19
20
  export {};
@@ -1,45 +1,57 @@
1
1
  import IntlError, { IntlErrorCode } from './IntlError';
2
2
  import { defaultOnError } from './defaults';
3
- const MINUTE = 60;
3
+ const SECOND = 1;
4
+ const MINUTE = SECOND * 60;
4
5
  const HOUR = MINUTE * 60;
5
6
  const DAY = HOUR * 24;
6
7
  const WEEK = DAY * 7;
7
8
  const MONTH = DAY * (365 / 12); // Approximation
9
+ const QUARTER = MONTH * 3;
8
10
  const YEAR = DAY * 365;
9
- function getRelativeTimeFormatConfig(seconds) {
11
+ const UNIT_SECONDS = {
12
+ second: SECOND,
13
+ seconds: SECOND,
14
+ minute: MINUTE,
15
+ minutes: MINUTE,
16
+ hour: HOUR,
17
+ hours: HOUR,
18
+ day: DAY,
19
+ days: DAY,
20
+ week: WEEK,
21
+ weeks: WEEK,
22
+ month: MONTH,
23
+ months: MONTH,
24
+ quarter: QUARTER,
25
+ quarters: QUARTER,
26
+ year: YEAR,
27
+ years: YEAR
28
+ };
29
+ function resolveRelativeTimeUnit(seconds) {
10
30
  const absValue = Math.abs(seconds);
11
- let value, unit;
12
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
13
- // will include fractions like '2.1 hours ago'.
14
31
  if (absValue < MINUTE) {
15
- unit = 'second';
16
- value = Math.round(seconds);
32
+ return 'second';
17
33
  }
18
34
  else if (absValue < HOUR) {
19
- unit = 'minute';
20
- value = Math.round(seconds / MINUTE);
35
+ return 'minute';
21
36
  }
22
37
  else if (absValue < DAY) {
23
- unit = 'hour';
24
- value = Math.round(seconds / HOUR);
38
+ return 'hour';
25
39
  }
26
40
  else if (absValue < WEEK) {
27
- unit = 'day';
28
- value = Math.round(seconds / DAY);
41
+ return 'day';
29
42
  }
30
43
  else if (absValue < MONTH) {
31
- unit = 'week';
32
- value = Math.round(seconds / WEEK);
44
+ return 'week';
33
45
  }
34
46
  else if (absValue < YEAR) {
35
- unit = 'month';
36
- value = Math.round(seconds / MONTH);
37
- }
38
- else {
39
- unit = 'year';
40
- value = Math.round(seconds / YEAR);
47
+ return 'month';
41
48
  }
42
- return { value, unit };
49
+ return 'year';
50
+ }
51
+ function calculateRelativeTimeValue(seconds, unit) {
52
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
53
+ // will include fractions like '2.1 hours ago'.
54
+ return Math.round(seconds / UNIT_SECONDS[unit]);
43
55
  }
44
56
  export default function createFormatter({ formats, locale, now: globalNow, onError = defaultOnError, timeZone: globalTimeZone }) {
45
57
  function resolveFormatOrOptions(typeFormats, formatOrOptions) {
@@ -99,29 +111,41 @@ export default function createFormatter({ formats, locale, now: globalNow, onErr
99
111
  function number(value, formatOrOptions) {
100
112
  return getFormattedValue(value, formatOrOptions, formats?.number, (options) => new Intl.NumberFormat(locale, options).format(value));
101
113
  }
114
+ function getGlobalNow() {
115
+ if (globalNow) {
116
+ return globalNow;
117
+ }
118
+ else {
119
+ onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
120
+ ? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
121
+ : undefined));
122
+ return new Date();
123
+ }
124
+ }
125
+ function extractNowDate(nowOrOptions) {
126
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
127
+ return new Date(nowOrOptions);
128
+ }
129
+ if (nowOrOptions?.now !== undefined) {
130
+ return new Date(nowOrOptions.now);
131
+ }
132
+ return getGlobalNow();
133
+ }
102
134
  function relativeTime(
103
135
  /** The date time that needs to be formatted. */
104
136
  date,
105
137
  /** The reference point in time to which `date` will be formatted in relation to. */
106
- now) {
138
+ nowOrOptions) {
107
139
  try {
108
- if (!now) {
109
- if (globalNow) {
110
- now = globalNow;
111
- }
112
- else {
113
- onError(new IntlError(IntlErrorCode.ENVIRONMENT_FALLBACK, process.env.NODE_ENV !== 'production'
114
- ? `The \`now\` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now`
115
- : undefined));
116
- }
117
- }
118
- const dateDate = date instanceof Date ? date : new Date(date);
119
- const nowDate = now instanceof Date
120
- ? now
121
- : // @ts-expect-error -- `undefined` is fine for the `Date` constructor
122
- new Date(now);
140
+ const dateDate = new Date(date);
141
+ const nowDate = extractNowDate(nowOrOptions);
123
142
  const seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
124
- const { unit, value } = getRelativeTimeFormatConfig(seconds);
143
+ const unit = typeof nowOrOptions === 'number' ||
144
+ nowOrOptions instanceof Date ||
145
+ nowOrOptions?.unit === undefined
146
+ ? resolveRelativeTimeUnit(seconds)
147
+ : nowOrOptions.unit;
148
+ const value = calculateRelativeTimeValue(seconds, unit);
125
149
  return new Intl.RelativeTimeFormat(locale, {
126
150
  numeric: 'auto'
127
151
  }).format(value, unit);
@@ -1 +1 @@
1
- {"version":3,"file":"createFormatter.js","sourceRoot":"","sources":["../../../src/core/createFormatter.tsx"],"names":[],"mapp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AAM,OAAO,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC;YAEhE,MAAM,IAAI,GACR,OAAO,YAAY,KAAK,QAAQ;gBAChC,YAAY,YAAY,IAAI;gBAC5B,YAAY,EAAE,IAAI,KAAK,SAAS;gBAC9B,CAAC,CAAC,uBAAuB,CAAC,OAAO,CAAC;gBAClC,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC;YAExB,MAAM,KAAK,GAAG,0BAA0B,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;YAExD,OAAO,IAAI,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE;gBACzC,OAAO,EAAE,MAAM;aAChB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;SACxB;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CACL,IAAI,SAAS,CAAC,aAAa,CAAC,gBAAgB,EAAG,KAAe,CAAC,OAAO,CAAC,CACxE,CAAC;YACF,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC;SACrB;IACH,CAAC;IAED,SAAS,IAAI,CACX,KAAuB,EACvB,eAAiD;QAEjD,OAAO,iBAAiB,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAC1E,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CACnD,CAAC;IACJ,CAAC;IAED,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAC,CAAC;AAChD,CAAC"}
@@ -3,5 +3,5 @@ import createFormatter from './createFormatter';
3
3
  export default function createIntl(...args: Parameters<typeof createFormatter>): {
4
4
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("./DateTimeFormatOptions").default | undefined) => string;
5
5
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
6
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
6
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("./RelativeTimeFormatOptions").default | undefined) => string;
7
7
  };
@@ -1,6 +1,6 @@
1
1
  export default function useFormatter(): {
2
2
  dateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
3
3
  number: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
4
- relativeTime: (date: number | Date, now?: number | Date | undefined) => string;
4
+ relativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
5
5
  list: (value: Iterable<string>, formatOrOptions?: string | Intl.ListFormatOptions | undefined) => string;
6
6
  };
@@ -2,5 +2,5 @@
2
2
  export default function useIntl(): {
3
3
  formatDateTime: (value: number | Date, formatOrOptions?: string | import("../core/DateTimeFormatOptions").default | undefined) => string;
4
4
  formatNumber: (value: number | bigint, formatOrOptions?: string | import("@formatjs/ecma402-abstract/types/number").NumberFormatOptions | undefined) => string;
5
- formatRelativeTime: (date: number | Date, now?: number | Date | undefined) => string;
5
+ formatRelativeTime: (date: number | Date, nowOrOptions?: number | Date | import("../core/RelativeTimeFormatOptions").default | undefined) => string;
6
6
  };
@@ -460,43 +460,53 @@ function createTranslator(_ref) {
460
460
  }), '!');
461
461
  }
462
462
 
463
- var MINUTE = 60;
463
+ var SECOND = 1;
464
+ var MINUTE = SECOND * 60;
464
465
  var HOUR = MINUTE * 60;
465
466
  var DAY = HOUR * 24;
466
467
  var WEEK = DAY * 7;
467
468
  var MONTH = DAY * (365 / 12); // Approximation
469
+ var QUARTER = MONTH * 3;
468
470
  var YEAR = DAY * 365;
469
- function getRelativeTimeFormatConfig(seconds) {
471
+ var UNIT_SECONDS = {
472
+ second: SECOND,
473
+ seconds: SECOND,
474
+ minute: MINUTE,
475
+ minutes: MINUTE,
476
+ hour: HOUR,
477
+ hours: HOUR,
478
+ day: DAY,
479
+ days: DAY,
480
+ week: WEEK,
481
+ weeks: WEEK,
482
+ month: MONTH,
483
+ months: MONTH,
484
+ quarter: QUARTER,
485
+ quarters: QUARTER,
486
+ year: YEAR,
487
+ years: YEAR
488
+ };
489
+ function resolveRelativeTimeUnit(seconds) {
470
490
  var absValue = Math.abs(seconds);
471
- var value, unit;
472
- // We have to round the resulting values, as `Intl.RelativeTimeFormat`
473
- // will include fractions like '2.1 hours ago'.
474
491
  if (absValue < MINUTE) {
475
- unit = 'second';
476
- value = Math.round(seconds);
492
+ return 'second';
477
493
  } else if (absValue < HOUR) {
478
- unit = 'minute';
479
- value = Math.round(seconds / MINUTE);
494
+ return 'minute';
480
495
  } else if (absValue < DAY) {
481
- unit = 'hour';
482
- value = Math.round(seconds / HOUR);
496
+ return 'hour';
483
497
  } else if (absValue < WEEK) {
484
- unit = 'day';
485
- value = Math.round(seconds / DAY);
498
+ return 'day';
486
499
  } else if (absValue < MONTH) {
487
- unit = 'week';
488
- value = Math.round(seconds / WEEK);
500
+ return 'week';
489
501
  } else if (absValue < YEAR) {
490
- unit = 'month';
491
- value = Math.round(seconds / MONTH);
492
- } else {
493
- unit = 'year';
494
- value = Math.round(seconds / YEAR);
502
+ return 'month';
495
503
  }
496
- return {
497
- value: value,
498
- unit: unit
499
- };
504
+ return 'year';
505
+ }
506
+ function calculateRelativeTimeValue(seconds, unit) {
507
+ // We have to round the resulting values, as `Intl.RelativeTimeFormat`
508
+ // will include fractions like '2.1 hours ago'.
509
+ return Math.round(seconds / UNIT_SECONDS[unit]);
500
510
  }
501
511
  function createFormatter(_ref) {
502
512
  var formats = _ref.formats,
@@ -558,25 +568,32 @@ function createFormatter(_ref) {
558
568
  return new Intl.NumberFormat(locale, options).format(value);
559
569
  });
560
570
  }
571
+ function getGlobalNow() {
572
+ if (globalNow) {
573
+ return globalNow;
574
+ } else {
575
+ onError(new IntlError(exports.IntlErrorCode.ENVIRONMENT_FALLBACK, "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" ));
576
+ return new Date();
577
+ }
578
+ }
579
+ function extractNowDate(nowOrOptions) {
580
+ if (nowOrOptions instanceof Date || typeof nowOrOptions === 'number') {
581
+ return new Date(nowOrOptions);
582
+ }
583
+ if ((nowOrOptions == null ? void 0 : nowOrOptions.now) !== undefined) {
584
+ return new Date(nowOrOptions.now);
585
+ }
586
+ return getGlobalNow();
587
+ }
561
588
  function relativeTime( /** The date time that needs to be formatted. */
562
589
  date, /** The reference point in time to which `date` will be formatted in relation to. */
563
- now) {
590
+ nowOrOptions) {
564
591
  try {
565
- if (!now) {
566
- if (globalNow) {
567
- now = globalNow;
568
- } else {
569
- onError(new IntlError(exports.IntlErrorCode.ENVIRONMENT_FALLBACK, "development" !== 'production' ? "The `now` parameter wasn't provided and there is no global default configured. Consider adding a global default to avoid markup mismatches caused by environment differences. Learn more: https://next-intl-docs.vercel.app/docs/configuration#now" : undefined));
570
- }
571
- }
572
- var dateDate = date instanceof Date ? date : new Date(date);
573
- var nowDate = now instanceof Date ? now :
574
- // @ts-expect-error -- `undefined` is fine for the `Date` constructor
575
- new Date(now);
592
+ var dateDate = new Date(date);
593
+ var nowDate = extractNowDate(nowOrOptions);
576
594
  var seconds = (dateDate.getTime() - nowDate.getTime()) / 1000;
577
- var _getRelativeTimeForma = getRelativeTimeFormatConfig(seconds),
578
- unit = _getRelativeTimeForma.unit,
579
- value = _getRelativeTimeForma.value;
595
+ var unit = typeof nowOrOptions === 'number' || nowOrOptions instanceof Date || (nowOrOptions == null ? void 0 : nowOrOptions.unit) === undefined ? resolveRelativeTimeUnit(seconds) : nowOrOptions.unit;
596
+ var value = calculateRelativeTimeValue(seconds, unit);
580
597
  return new Intl.RelativeTimeFormat(locale, {
581
598
  numeric: 'auto'
582
599
  }).format(value, unit);