number_pipe.js
8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Inject, LOCALE_ID, Pipe } from '@angular/core';
import { NumberWrapper } from '../facade/lang';
import { NumberFormatStyle, NumberFormatter } from './intl';
import { InvalidPipeArgumentError } from './invalid_pipe_argument_error';
var /** @type {?} */ _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
/**
* @param {?} pipe
* @param {?} locale
* @param {?} value
* @param {?} style
* @param {?} digits
* @param {?=} currency
* @param {?=} currencyAsSymbol
* @return {?}
*/
function formatNumber(pipe, locale, value, style, digits, currency, currencyAsSymbol) {
if (currency === void 0) { currency = null; }
if (currencyAsSymbol === void 0) { currencyAsSymbol = false; }
if (value == null)
return null;
// Convert strings to numbers
value = typeof value === 'string' && NumberWrapper.isNumeric(value) ? +value : value;
if (typeof value !== 'number') {
throw new InvalidPipeArgumentError(pipe, value);
}
var /** @type {?} */ minInt;
var /** @type {?} */ minFraction;
var /** @type {?} */ maxFraction;
if (style !== NumberFormatStyle.Currency) {
// rely on Intl default for currency
minInt = 1;
minFraction = 0;
maxFraction = 3;
}
if (digits) {
var /** @type {?} */ parts = digits.match(_NUMBER_FORMAT_REGEXP);
if (parts === null) {
throw new Error(digits + " is not a valid digit info for number pipes");
}
if (parts[1] != null) {
minInt = NumberWrapper.parseIntAutoRadix(parts[1]);
}
if (parts[3] != null) {
minFraction = NumberWrapper.parseIntAutoRadix(parts[3]);
}
if (parts[5] != null) {
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
}
}
return NumberFormatter.format(/** @type {?} */ (value), locale, style, {
minimumIntegerDigits: minInt,
minimumFractionDigits: minFraction,
maximumFractionDigits: maxFraction,
currency: currency,
currencyAsSymbol: currencyAsSymbol,
});
}
/**
* \@ngModule CommonModule
* \@whatItDoes Formats a number according to locale rules.
* \@howToUse `number_expression | number[:digitInfo]`
*
* Formats a number as text. Group sizing and separator and other locale-specific
* configurations are based on the active locale.
*
* where `expression` is a number:
* - `digitInfo` is a `string` which has a following format: <br>
* <code>{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}</code>
* - `minIntegerDigits` is the minimum number of integer digits to use. Defaults to `1`.
* - `minFractionDigits` is the minimum number of digits after fraction. Defaults to `0`.
* - `maxFractionDigits` is the maximum number of digits after fraction. Defaults to `3`.
*
* For more information on the acceptable range for each of these numbers and other
* details see your native internationalization library.
*
* WARNING: this pipe uses the Internationalization API which is not yet available in all browsers
* and may require a polyfill. See {\@linkDocs guide/browser-support} for details.
*
* ### Example
*
* {\@example common/pipes/ts/number_pipe.ts region='NumberPipe'}
*
* \@stable
*/
export var DecimalPipe = (function () {
/**
* @param {?} _locale
*/
function DecimalPipe(_locale) {
this._locale = _locale;
}
/**
* @param {?} value
* @param {?=} digits
* @return {?}
*/
DecimalPipe.prototype.transform = function (value, digits) {
if (digits === void 0) { digits = null; }
return formatNumber(DecimalPipe, this._locale, value, NumberFormatStyle.Decimal, digits);
};
DecimalPipe.decorators = [
{ type: Pipe, args: [{ name: 'number' },] },
];
/** @nocollapse */
DecimalPipe.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [LOCALE_ID,] },] },
]; };
return DecimalPipe;
}());
function DecimalPipe_tsickle_Closure_declarations() {
/** @type {?} */
DecimalPipe.decorators;
/**
* @nocollapse
* @type {?}
*/
DecimalPipe.ctorParameters;
/** @type {?} */
DecimalPipe.prototype._locale;
}
/**
* \@ngModule CommonModule
* \@whatItDoes Formats a number as a percentage according to locale rules.
* \@howToUse `number_expression | percent[:digitInfo]`
*
* \@description
*
* Formats a number as percentage.
*
* - `digitInfo` See {\@link DecimalPipe} for detailed description.
*
* WARNING: this pipe uses the Internationalization API which is not yet available in all browsers
* and may require a polyfill. See {\@linkDocs guide/browser-support} for details.
*
* ### Example
*
* {\@example common/pipes/ts/number_pipe.ts region='PercentPipe'}
*
* \@stable
*/
export var PercentPipe = (function () {
/**
* @param {?} _locale
*/
function PercentPipe(_locale) {
this._locale = _locale;
}
/**
* @param {?} value
* @param {?=} digits
* @return {?}
*/
PercentPipe.prototype.transform = function (value, digits) {
if (digits === void 0) { digits = null; }
return formatNumber(PercentPipe, this._locale, value, NumberFormatStyle.Percent, digits);
};
PercentPipe.decorators = [
{ type: Pipe, args: [{ name: 'percent' },] },
];
/** @nocollapse */
PercentPipe.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [LOCALE_ID,] },] },
]; };
return PercentPipe;
}());
function PercentPipe_tsickle_Closure_declarations() {
/** @type {?} */
PercentPipe.decorators;
/**
* @nocollapse
* @type {?}
*/
PercentPipe.ctorParameters;
/** @type {?} */
PercentPipe.prototype._locale;
}
/**
* \@ngModule CommonModule
* \@whatItDoes Formats a number as currency using locale rules.
* \@howToUse `number_expression | currency[:currencyCode[:symbolDisplay[:digitInfo]]]`
* \@description
*
* Use `currency` to format a number as currency.
*
* - `currencyCode` is the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) currency code, such
* as `USD` for the US dollar and `EUR` for the euro.
* - `symbolDisplay` is a boolean indicating whether to use the currency symbol or code.
* - `true`: use symbol (e.g. `$`).
* - `false`(default): use code (e.g. `USD`).
* - `digitInfo` See {\@link DecimalPipe} for detailed description.
*
* WARNING: this pipe uses the Internationalization API which is not yet available in all browsers
* and may require a polyfill. See {\@linkDocs guide/browser-support} for details.
*
* ### Example
*
* {\@example common/pipes/ts/number_pipe.ts region='CurrencyPipe'}
*
* \@stable
*/
export var CurrencyPipe = (function () {
/**
* @param {?} _locale
*/
function CurrencyPipe(_locale) {
this._locale = _locale;
}
/**
* @param {?} value
* @param {?=} currencyCode
* @param {?=} symbolDisplay
* @param {?=} digits
* @return {?}
*/
CurrencyPipe.prototype.transform = function (value, currencyCode, symbolDisplay, digits) {
if (currencyCode === void 0) { currencyCode = 'USD'; }
if (symbolDisplay === void 0) { symbolDisplay = false; }
if (digits === void 0) { digits = null; }
return formatNumber(CurrencyPipe, this._locale, value, NumberFormatStyle.Currency, digits, currencyCode, symbolDisplay);
};
CurrencyPipe.decorators = [
{ type: Pipe, args: [{ name: 'currency' },] },
];
/** @nocollapse */
CurrencyPipe.ctorParameters = function () { return [
{ type: undefined, decorators: [{ type: Inject, args: [LOCALE_ID,] },] },
]; };
return CurrencyPipe;
}());
function CurrencyPipe_tsickle_Closure_declarations() {
/** @type {?} */
CurrencyPipe.decorators;
/**
* @nocollapse
* @type {?}
*/
CurrencyPipe.ctorParameters;
/** @type {?} */
CurrencyPipe.prototype._locale;
}
//# sourceMappingURL=number_pipe.js.map