feat(pipes): add number (decimal, percent, currency) pipes
This commit is contained in:

committed by
Tobias Bosch

parent
b54e7214f0
commit
3143d188ae
42
modules/angular2/src/facade/intl.dart
Normal file
42
modules/angular2/src/facade/intl.dart
Normal file
@ -0,0 +1,42 @@
|
||||
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);
|
||||
}
|
||||
}
|
73
modules/angular2/src/facade/intl.ts
Normal file
73
modules/angular2/src/facade/intl.ts
Normal file
@ -0,0 +1,73 @@
|
||||
|
||||
// Modified version of internal Typescript intl.d.ts.
|
||||
// TODO(piloopin): remove when https://github.com/Microsoft/TypeScript/issues/3521 is shipped.
|
||||
declare module Intl {
|
||||
interface NumberFormatOptions {
|
||||
localeMatcher?: string;
|
||||
style?: string;
|
||||
currency?: string;
|
||||
currencyDisplay?: string;
|
||||
useGrouping?: boolean;
|
||||
}
|
||||
|
||||
interface NumberFormat {
|
||||
format(value: number): string;
|
||||
}
|
||||
|
||||
var NumberFormat: {
|
||||
new (locale?: string, options?: NumberFormatOptions): NumberFormat;
|
||||
}
|
||||
|
||||
interface DateTimeFormatOptions {
|
||||
localeMatcher?: string;
|
||||
weekday?: string;
|
||||
era?: string;
|
||||
year?: string;
|
||||
month?: string;
|
||||
day?: string;
|
||||
hour?: string;
|
||||
minute?: string;
|
||||
second?: string;
|
||||
timeZoneName?: string;
|
||||
formatMatcher?: string;
|
||||
hour12?: boolean;
|
||||
}
|
||||
|
||||
interface DateTimeFormat {
|
||||
format(date?: Date | number): string;
|
||||
}
|
||||
|
||||
var DateTimeFormat: {
|
||||
new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat;
|
||||
}
|
||||
}
|
||||
|
||||
export enum NumberFormatStyle {
|
||||
DECIMAL,
|
||||
PERCENT,
|
||||
CURRENCY
|
||||
}
|
||||
|
||||
export class NumberFormatter {
|
||||
static format(number: number, locale: string, style: NumberFormatStyle,
|
||||
{minimumIntegerDigits = 1, minimumFractionDigits = 0, maximumFractionDigits = 3,
|
||||
currency, currencyAsSymbol = false}: {
|
||||
minimumIntegerDigits?: int,
|
||||
minimumFractionDigits?: int,
|
||||
maximumFractionDigits?: int,
|
||||
currency?: string,
|
||||
currencyAsSymbol?: boolean
|
||||
} = {}): string {
|
||||
var intlOptions: Intl.NumberFormatOptions = {
|
||||
minimumIntegerDigits: minimumIntegerDigits,
|
||||
minimumFractionDigits: minimumFractionDigits,
|
||||
maximumFractionDigits: maximumFractionDigits
|
||||
};
|
||||
intlOptions.style = NumberFormatStyle[style].toLowerCase();
|
||||
if (style == NumberFormatStyle.CURRENCY) {
|
||||
intlOptions.currency = currency;
|
||||
intlOptions.currencyDisplay = currencyAsSymbol ? 'symbol' : 'code';
|
||||
}
|
||||
return new Intl.NumberFormat(locale, intlOptions).format(number);
|
||||
}
|
||||
}
|
@ -32,6 +32,7 @@ bool isType(obj) => obj is Type;
|
||||
bool isStringMap(obj) => obj is Map;
|
||||
bool isArray(obj) => obj is List;
|
||||
bool isPromise(obj) => obj is Future;
|
||||
bool isNumber(obj) => obj is num;
|
||||
|
||||
String stringify(obj) => obj.toString();
|
||||
|
||||
|
@ -89,6 +89,10 @@ export function isArray(obj): boolean {
|
||||
return Array.isArray(obj);
|
||||
}
|
||||
|
||||
export function isNumber(obj): boolean {
|
||||
return typeof obj === 'number';
|
||||
}
|
||||
|
||||
export function stringify(token): string {
|
||||
if (typeof token === 'string') {
|
||||
return token;
|
||||
|
Reference in New Issue
Block a user