Skip to content

Commit

Permalink
Add support for Temporal objects (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer authored Aug 14, 2024
1 parent 53a8574 commit bf12d97
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-ties-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/intl': major
---

Added support for Temporal objects to the date and time formatting functions. The [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) package is now a required peer dependency.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ Creating instances of `Intl.*` formatters is an [expensive operation](https://bl

`@sumup/intl` works in [modern browsers](https://caniuse.com/mdn-javascript_builtins_intl_numberformat_numberformat,mdn-javascript_builtins_intl_datetimeformat_datetimeformat) as well as server runtimes with support for the `Intl` APIs (such as Node 10+[^1]). When the `Intl` APIs aren't (fully) available, `@sumup/intl` tries to gracefully degrade. Please refer to the [API reference](#api-reference) to learn how certain functions behave when the runtime doesn't support the necessary APIs. If you need to support legacy browsers, consider including [polyfills](https://formatjs.io/docs/polyfills/).

`@sumup/intl` integrates [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) to support formatting [`Temporal`](https://github.com/tc39/proposal-temporal) date-time objects.

[^1]: [Node](https://nodejs.org/en/) supports the `Intl` APIs since v8, however, it includes only the English localizations up to v12. Node v13 and above support all locales. If you're unable to use Node v13+, you can either include [polyfills](https://formatjs.io/docs/polyfills/) or use a [custom Node build](https://nodejs.org/docs/latest-v8.x/api/intl.html#intl_options_for_building_node_js).

## Installation

[`@sumup/intl`](https://www.npmjs.com/package/@sumup/intl) can be installed as a dependency via the [npm](https://www.npmjs.com) or [Yarn](https://classic.yarnpkg.com) package managers. Depending on your preference, run one of the following:
[`@sumup/intl`](https://www.npmjs.com/package/@sumup/intl) can be installed as a dependency via the [npm](https://www.npmjs.com) package manager. The [`temporal-polyfill`](https://www.npmjs.com/package/temporal-polyfill) package is a required peer dependency.

```sh
# With npm
npm install @sumup/intl

# With Yarn v1
yarn add @sumup/intl
npm install @sumup/intl temporal-polyfill
```

`@sumup/intl` requires Node v10+.
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@
"eslint-plugin-prettier": "^5.1.3",
"jest-extended": "^4.0.2",
"license-checker": "^25.0.1",
"temporal-polyfill": "^0.2.5",
"typedoc": "^0.26.3",
"typedoc-github-wiki-theme": "^2.0.0",
"typedoc-plugin-markdown": "^4.0.3",
"typescript": "^5.4.5",
"vitest": "^2.0.5"
},
"peerDependencies": {
"temporal-polyfill": "0.2.x"
}
}
5 changes: 3 additions & 2 deletions src/lib/date-time-format/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

import { Intl as IntlWithTemporal } from 'temporal-polyfill';
import memoizeFormatConstructor from 'intl-format-cache';

import { Locale } from '../../types';
Expand Down Expand Up @@ -61,8 +62,8 @@ export const isDateTimeStyleSupported = (() => {
})();

export const getDateTimeFormat = memoizeFormatConstructor(
Intl.DateTimeFormat,
IntlWithTemporal.DateTimeFormat,
) as (
locales?: Locale | Locale[],
options?: Intl.DateTimeFormatOptions,
) => Intl.DateTimeFormat;
) => IntlWithTemporal.DateTimeFormat;
1 change: 1 addition & 0 deletions src/lib/date-time-format/tests/format-to-parts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import { describe, it, expect } from 'vitest';
import { Intl } from 'temporal-polyfill';

import { formatDateTimeToParts } from '..';

Expand Down
1 change: 1 addition & 0 deletions src/lib/date-time-format/tests/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import { describe, it, expect } from 'vitest';
import { Intl } from 'temporal-polyfill';

import { formatDateTime, formatDate, formatTime } from '..';

Expand Down
1 change: 1 addition & 0 deletions src/lib/date-time-format/tests/resolve-format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import { describe, it, expect } from 'vitest';
import { Intl } from 'temporal-polyfill';

import { resolveDateTimeFormat } from '..';

Expand Down
1 change: 1 addition & 0 deletions src/lib/date-time-format/tests/unsupported-styles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import { vi, describe, it, expect } from 'vitest';
import { Intl } from 'temporal-polyfill';

import { formatDateTime } from '..';

Expand Down
3 changes: 2 additions & 1 deletion vitest.setup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { vi, expect } from 'vitest';
import * as matchers from 'jest-extended';
import { Intl as IntlWithTemporal } from 'temporal-polyfill';

expect.extend(matchers);

vi.spyOn(Intl, 'NumberFormat');
vi.spyOn(Intl, 'DateTimeFormat');
vi.spyOn(IntlWithTemporal, 'DateTimeFormat');

// Apparently, Node.js doesn't implement these APIs.
// The mocked return value is based on the test value `1001001001.11111`
Expand Down

0 comments on commit bf12d97

Please sign in to comment.