Skip to content

Commit

Permalink
Revert "feat(@formatjs/intl): remove default hour/minute option set i…
Browse files Browse the repository at this point in the history
…n formatTime"

This reverts commit 0140cea.
  • Loading branch information
longlho committed Dec 16, 2020
1 parent e37f131 commit 044c906
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
12 changes: 6 additions & 6 deletions packages/intl-datetimeformat/BUILD
Expand Up @@ -144,9 +144,9 @@ ts_node(
],
data = CLDR_DEPS,
output_dir = True,
visibility = [
"//packages/intl:__subpackages__",
],
visibility=[
"//packages/intl:__subpackages__"
]
)

[generate_src_file(
Expand Down Expand Up @@ -274,9 +274,9 @@ ts_node(
"@npm//tslib",
"@tzdata//:zdumps",
],
visibility = [
"//packages/intl:__subpackages__",
],
visibility=[
"//packages/intl:__subpackages__"
]
)

# add-golden-tz
Expand Down
2 changes: 0 additions & 2 deletions packages/intl/BUILD
Expand Up @@ -56,8 +56,6 @@ ts_compile(
jest_test(
name = "unit",
srcs = SRCS + TESTS + [
"//packages/intl-datetimeformat:locale-data",
"//packages/intl-datetimeformat:add-all-tz",
"//packages/intl-numberformat:locale-data",
"//packages/intl-displaynames:polyfill-locales.js",
],
Expand Down
10 changes: 10 additions & 0 deletions packages/intl/src/dateTime.ts
Expand Up @@ -60,6 +60,16 @@ export function getFormatter(
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);
}

Expand Down
File renamed without changes.
24 changes: 6 additions & 18 deletions packages/intl/tests/formatTime.test.ts
@@ -1,19 +1,13 @@
/* eslint-disable @typescript-eslint/camelcase */
import {DateTimeFormat} from '@formatjs/ecma402-abstract';
import {formatTime as formatTimeFn} from '../src/dateTime';
import {OptionalIntlConfig, IntlFormatters, Formatters} from '../src/types';
import '@formatjs/intl-datetimeformat/polyfill';
import '@formatjs/intl-datetimeformat/locale-data/en';
import '@formatjs/intl-datetimeformat/add-all-tz';
import {OptionalIntlConfig, IntlFormatters} from '../src/types';

describe('format API', () => {
const {NODE_ENV} = process.env;

let config: OptionalIntlConfig<any>;

let getDateTimeFormat: Formatters['getDateTimeFormat'] = (
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
) => new Intl.DateTimeFormat(...args) as DateTimeFormat;
let getDateTimeFormat: any;
beforeEach(() => {
config = {
locale: 'en',
Expand All @@ -35,6 +29,10 @@ describe('format API', () => {

onError: jest.fn(),
};

getDateTimeFormat = jest
.fn()
.mockImplementation((...args) => new Intl.DateTimeFormat(...args));
});

afterEach(() => {
Expand All @@ -52,23 +50,13 @@ describe('format API', () => {
});
// @ts-ignore
formatTime = formatTimeFn.bind(null, config, getDateTimeFormat);
(config.onError as jest.Mock).mockClear();
});

it('render now if no value is provided', () => {
// @ts-ignore
expect(formatTime()).toBe(df.format());
});

it('should not inject additional hour/minute when dateStyle/timeStyle are used', function () {
expect(config.onError).not.toHaveBeenCalled();
formatTimeFn(config as any, getDateTimeFormat, new Date(), {
dateStyle: 'short',
});

expect(config.onError).not.toHaveBeenCalled();
});

it('falls back and warns when a non-finite value is provided', () => {
expect(formatTime(NaN)).toBe('NaN');
// @ts-ignore
Expand Down

0 comments on commit 044c906

Please sign in to comment.