Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extensions on DateTime and List #881

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions pkgs/intl4x/lib/datetime_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
// 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 {
/// Format a date in a locale-dependent manner.
///
/// For more options, use [Intl.datetimeFormat] directly.
String toLocaleDateString([Locale? locale]) =>
Intl(locale: locale).datetimeFormat().format(this);
}
32 changes: 32 additions & 0 deletions pkgs/intl4x/lib/list_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@
// 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';
import 'src/list_format/list_format_options.dart';

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

extension ListFormatIntl4x on List<String> {
/// Join a list in a locale-dependent manner using `and`-based grouping.
///
/// Example: "A, B, and C". See also [Type.and].
///
/// For more options, use [Intl.listFormat] directly.
String joinAnd([Locale? locale]) => Intl(locale: locale)
.listFormat(const ListFormatOptions(type: Type.and))
.format(this);

/// Join a list in a locale-dependent manner using `or`-based grouping.
///
/// Example: "A, B, or C". See also [Type.or].
///
/// For more options, use [Intl.listFormat] directly.
String joinOr([Locale? locale]) => Intl(locale: locale)
.listFormat(const ListFormatOptions(type: Type.or))
.format(this);

/// Join a list in a locale-dependent manner using unit-based grouping.
///
/// Example: "A, B, C". See also [Type.unit].
///
/// For more options, use [Intl.listFormat] directly.
String joinUnit([Locale? locale]) => Intl(locale: locale)
.listFormat(const ListFormatOptions(type: Type.or))
mosuem marked this conversation as resolved.
Show resolved Hide resolved
.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'],
);
}
17 changes: 10 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,10 @@ void main() {
expect(formatter.format(list), 'A, B, C');
});
});

testWithFormatting('extension', () {
expect(list.joinAnd(enUS), 'A, B, and C');
expect(list.joinOr(enUS), 'A, B, or C');
expect(list.joinUnit(enUS), 'A, B, C');
});
}
Loading