Skip to content

Commit

Permalink
Add extensions on DateTime and List
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Aug 29, 2024
1 parent 4796ea1 commit 33dfbec
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
4 changes: 4 additions & 0 deletions pkgs/intl4x/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.2-wip

- Add examples of extensions to `List` and `DateTime` formatting.

## 0.10.1

- Upgrade to new artifacts.
Expand Down
7 changes: 7 additions & 0 deletions pkgs/intl4x/lib/datetime_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'intl4x.dart';

export 'src/datetime_format/datetime_format.dart';
export 'src/datetime_format/datetime_format_options.dart';
export 'src/options.dart';

extension DatetimeFormatIntl4x on DateTime {
String toLocaleDateString([Locale? locale]) =>
Intl(locale: locale).datetimeFormat().format(this);
}
7 changes: 7 additions & 0 deletions pkgs/intl4x/lib/list_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'intl4x.dart';

export 'src/list_format/list_format.dart';
export 'src/list_format/list_format_options.dart';
export 'src/options.dart';

extension DatetimeFormatIntl4x on List<String> {
String toLocaleFormat([Locale? locale]) =>
Intl(locale: locale).listFormat().format(this);
}
2 changes: 1 addition & 1 deletion pkgs/intl4x/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: intl4x
description: >-
A lightweight modular library for internationalization (i18n) functionality.
version: 0.10.1
version: 0.10.2-wip
repository: https://github.com/dart-lang/i18n/tree/main/pkgs/intl4x
platforms:
web:
Expand Down
10 changes: 10 additions & 0 deletions pkgs/intl4x/test/datetime_format_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,14 @@ void main() {
},
tags: ['icu4xUnimplemented'],
);
testWithFormatting(
'Extension',
() {
expect(
DateTime.utc(2012, 12, 20, 3, 0, 0)
.toLocaleDateString(const Locale(language: 'en', region: 'US')),
'12/20/2012');
},
tags: ['icu4xUnimplemented'],
);
}
15 changes: 8 additions & 7 deletions pkgs/intl4x/test/list_format_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:intl4x/intl4x.dart';
import 'package:intl4x/src/list_format/list_format_options.dart';
import 'package:intl4x/list_format.dart';
import 'package:test/test.dart';

import 'utils.dart';

void main() {
final list = ['A', 'B', 'C'];
const enUS = Locale(language: 'en', region: 'US');
final intl = Intl(locale: enUS);
group('List style options', () {
final list = ['A', 'B', 'C'];
final intl = Intl(locale: const Locale(language: 'en', region: 'US'));
testWithFormatting('long', () {
final listFormat =
intl.listFormat(const ListFormatOptions(style: ListStyle.long));
Expand All @@ -30,8 +31,6 @@ void main() {
});

group('List type options', () {
final list = ['A', 'B', 'C'];
final intl = Intl(locale: const Locale(language: 'en', region: 'US'));
testWithFormatting('long', () {
final listFormat =
intl.listFormat(const ListFormatOptions(type: Type.and));
Expand All @@ -50,8 +49,6 @@ void main() {
});

group('List style and type combinations', () {
final list = ['A', 'B', 'C'];
final intl = Intl(locale: const Locale(language: 'en', region: 'US'));
testWithFormatting('long', () {
final formatter = intl.listFormat(
const ListFormatOptions(style: ListStyle.narrow, type: Type.and));
Expand All @@ -63,4 +60,8 @@ void main() {
expect(formatter.format(list), 'A, B, C');
});
});

testWithFormatting('extension', () {
expect(list.toLocaleFormat(enUS), 'A, B, and C');
});
}

0 comments on commit 33dfbec

Please sign in to comment.