BREAKING_CHANGE Ts2Dart issue: https://github.com/angular/ts2dart/issues/270 TypeScript convention: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines DartConvertion: https://www.dartlang.org/articles/style-guide/ Rename: - NumberFormatStyle.DECIMAL => NumberFormatStyle.Decimal - NumberFormatStyle.PERCENT => NumberFormatStyle.Percent - NumberFormatStyle.CURRENCY => NumberFormatStyle.Currency - RequestMethods.GET => RequestMethods.Get - RequestMethods.POST => RequestMethods.Post - RequestMethods.PUT => RequestMethods.Put - RequestMethods.DELETE => RequestMethods.Delete - RequestMethods.HEAD => RequestMethods.Head - RequestMethods.PATCH => RequestMethods.Patch - ReadyStates.UNSENT => ReadyStates.Unsent - ReadyStates.OPEN => ReadyStates.Open - ReadyStates.HEADERS_RECEIVED => ReadyStates.HeadersReceived - ReadyStates.LOADING => ReadyStates.Loading - ReadyStates.DONE => ReadyStates.Done - ReadyStates.CANCELLED => ReadyStates.Canceled
58 lines
1.9 KiB
Dart
58 lines
1.9 KiB
Dart
library facade.intl;
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
String _normalizeLocale(String locale) => locale.replaceAll('-', '_');
|
|
|
|
enum NumberFormatStyle { Decimal, Percent, Currency }
|
|
|
|
class NumberFormatter {
|
|
static String format(num number, String locale, NumberFormatStyle style,
|
|
{int minimumIntegerDigits: 1,
|
|
int minimumFractionDigits: 0,
|
|
int maximumFractionDigits: 3,
|
|
String currency,
|
|
bool currencyAsSymbol: false}) {
|
|
locale = _normalizeLocale(locale);
|
|
NumberFormat formatter;
|
|
switch (style) {
|
|
case NumberFormatStyle.Decimal:
|
|
formatter = new NumberFormat.decimalPattern(locale);
|
|
break;
|
|
case NumberFormatStyle.Percent:
|
|
formatter = new NumberFormat.percentPattern(locale);
|
|
break;
|
|
case NumberFormatStyle.Currency:
|
|
if (currencyAsSymbol) {
|
|
// See https://github.com/dart-lang/intl/issues/59.
|
|
throw new Exception(
|
|
'Displaying currency as symbol is not supported.');
|
|
}
|
|
formatter = new NumberFormat.currencyPattern(locale, currency);
|
|
break;
|
|
}
|
|
formatter.minimumIntegerDigits = minimumIntegerDigits;
|
|
formatter.minimumFractionDigits = minimumFractionDigits;
|
|
formatter.maximumFractionDigits = maximumFractionDigits;
|
|
return formatter.format(number);
|
|
}
|
|
}
|
|
|
|
class DateFormatter {
|
|
static RegExp _multiPartRegExp = new RegExp(r'^([yMdE]+)([Hjms]+)$');
|
|
|
|
static String format(DateTime date, String locale, String pattern) {
|
|
locale = _normalizeLocale(locale);
|
|
var formatter = new DateFormat(null, locale);
|
|
var matches = _multiPartRegExp.firstMatch(pattern);
|
|
if (matches != null) {
|
|
// Support for patterns which have known date and time components.
|
|
formatter.addPattern(matches[1]);
|
|
formatter.addPattern(matches[2], ', ');
|
|
} else {
|
|
formatter.addPattern(pattern);
|
|
}
|
|
return formatter.format(date);
|
|
}
|
|
}
|