|
| 1 | +import { |
| 2 | + isNumber, |
| 3 | + isPresent, |
| 4 | + isBlank, |
| 5 | + StringWrapper, |
| 6 | + NumberWrapper, |
| 7 | + RegExpWrapper, |
| 8 | + BaseException, |
| 9 | + CONST, |
| 10 | + FunctionWrapper |
| 11 | +} from 'angular2/src/facade/lang'; |
| 12 | +import {NumberFormatter, NumberFormatStyle} from 'angular2/src/facade/intl'; |
| 13 | +import {ListWrapper} from 'angular2/src/facade/collection'; |
| 14 | +import {Pipe, BasePipe, PipeFactory} from './pipe'; |
| 15 | +import {ChangeDetectorRef} from '../change_detector_ref'; |
| 16 | + |
| 17 | +var defaultLocale: string = 'en-US'; |
| 18 | +var _re = RegExpWrapper.create('^(\\d+)?\\.((\\d+)(\\-(\\d+))?)?$'); |
| 19 | + |
| 20 | +@CONST() |
| 21 | +export class NumberPipe extends BasePipe implements PipeFactory { |
| 22 | + static _format(value: number, style: NumberFormatStyle, digits: string, currency: string = null, |
| 23 | + currencyAsSymbol: boolean = false): string { |
| 24 | + var minInt = 1, minFraction = 0, maxFraction = 3; |
| 25 | + if (isPresent(digits)) { |
| 26 | + var parts = RegExpWrapper.firstMatch(_re, digits); |
| 27 | + if (isBlank(parts)) { |
| 28 | + throw new BaseException(`${digits} is not a valid digit info for number pipes`); |
| 29 | + } |
| 30 | + if (isPresent(parts[1])) { // min integer digits |
| 31 | + minInt = NumberWrapper.parseIntAutoRadix(parts[1]); |
| 32 | + } |
| 33 | + if (isPresent(parts[3])) { // min fraction digits |
| 34 | + minFraction = NumberWrapper.parseIntAutoRadix(parts[3]); |
| 35 | + } |
| 36 | + if (isPresent(parts[5])) { // max fraction digits |
| 37 | + maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]); |
| 38 | + } |
| 39 | + } |
| 40 | + return NumberFormatter.format(value, defaultLocale, style, { |
| 41 | + minimumIntegerDigits: minInt, |
| 42 | + minimumFractionDigits: minFraction, |
| 43 | + maximumFractionDigits: maxFraction, |
| 44 | + currency: currency, |
| 45 | + currencyAsSymbol: currencyAsSymbol |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + supports(obj): boolean { return isNumber(obj); } |
| 50 | + |
| 51 | + create(cdRef: ChangeDetectorRef): Pipe { return this } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Formats a number as local text. i.e. group sizing and seperator and other locale-specific |
| 56 | + * configurations are based on the active locale. |
| 57 | + * |
| 58 | + * # Usage |
| 59 | + * |
| 60 | + * expression | number[:digitInfo] |
| 61 | + * |
| 62 | + * where `expression` is a number and `digitInfo` has the following format: |
| 63 | + * |
| 64 | + * {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits} |
| 65 | + * |
| 66 | + * - minIntegerDigits is the minimum number of integer digits to use. Defaults to 1. |
| 67 | + * - minFractionDigits is the minimum number of digits after fraction. Defaults to 0. |
| 68 | + * - maxFractionDigits is the maximum number of digits after fraction. Defaults to 3. |
| 69 | + * |
| 70 | + * For more information on the acceptable range for each of these numbers and other |
| 71 | + * details see your native internationalization library. |
| 72 | + * |
| 73 | + * # Examples |
| 74 | + * |
| 75 | + * {{ 123 | number }} // output is 123 |
| 76 | + * {{ 123.1 | number: '.2-3' }} // output is 123.10 |
| 77 | + * {{ 1 | number: '2.2' }} // output is 01.00 |
| 78 | + * |
| 79 | + * @exportedAs angular2/pipes |
| 80 | + */ |
| 81 | +@CONST() |
| 82 | +export class DecimalPipe extends NumberPipe { |
| 83 | + transform(value, args: any[]): string { |
| 84 | + var digits: string = ListWrapper.first(args); |
| 85 | + return NumberPipe._format(value, NumberFormatStyle.DECIMAL, digits); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Formats a number as local percent. |
| 91 | + * |
| 92 | + * # Usage |
| 93 | + * |
| 94 | + * expression | percent[:digitInfo] |
| 95 | + * |
| 96 | + * For more information about `digitInfo` see {@link DecimalPipe} |
| 97 | + * |
| 98 | + * @exportedAs angular2/pipes |
| 99 | + */ |
| 100 | +@CONST() |
| 101 | +export class PercentPipe extends NumberPipe { |
| 102 | + transform(value, args: any[]): string { |
| 103 | + var digits: string = ListWrapper.first(args); |
| 104 | + return NumberPipe._format(value, NumberFormatStyle.PERCENT, digits); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Formats a number as local currency. |
| 110 | + * |
| 111 | + * # Usage |
| 112 | + * |
| 113 | + * expression | currency[:currencyCode[:symbolDisplay[:digitInfo]]] |
| 114 | + * |
| 115 | + * where `currencyCode` is the ISO 4217 currency code, such as "USD" for the US dollar and |
| 116 | + * "EUR" for the euro. `symbolDisplay` is a boolean indicating whether to use the currency |
| 117 | + * symbol (e.g. $) or the currency code (e.g. USD) in the output. The default for this value |
| 118 | + * is `false`. |
| 119 | + * For more information about `digitInfo` see {@link DecimalPipe} |
| 120 | + * |
| 121 | + * @exportedAs angular2/pipes |
| 122 | + */ |
| 123 | +@CONST() |
| 124 | +export class CurrencyPipe extends NumberPipe { |
| 125 | + transform(value, args: any[]): string { |
| 126 | + var currencyCode: string = isPresent(args) && args.length > 0 ? args[0] : 'USD'; |
| 127 | + var symbolDisplay: boolean = isPresent(args) && args.length > 1 ? args[1] : false; |
| 128 | + var digits: string = isPresent(args) && args.length > 2 ? args[2] : null; |
| 129 | + return NumberPipe._format(value, NumberFormatStyle.CURRENCY, digits, currencyCode, |
| 130 | + symbolDisplay); |
| 131 | + } |
| 132 | +} |
0 commit comments