parent
979946c062
commit
f3dd91e1d7
|
@ -7,9 +7,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Pipe, PipeTransform} from '@angular/core';
|
import {Pipe, PipeTransform} from '@angular/core';
|
||||||
|
|
||||||
import {BaseException} from '../facade/exceptions';
|
import {BaseException} from '../facade/exceptions';
|
||||||
import {NumberFormatStyle, NumberFormatter} from '../facade/intl';
|
import {NumberFormatStyle, NumberFormatter} from '../facade/intl';
|
||||||
import {NumberWrapper, RegExpWrapper, Type, isBlank, isNumber, isPresent} from '../facade/lang';
|
import {NumberWrapper, RegExpWrapper, StringWrapper, Type, isBlank, isNumber, isPresent, isString} from '../facade/lang';
|
||||||
|
|
||||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||||
|
|
||||||
var defaultLocale: string = 'en-US';
|
var defaultLocale: string = 'en-US';
|
||||||
|
@ -19,13 +21,17 @@ const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(\-(\d+))?)?$/g;
|
||||||
* Internal function to format numbers used by Decimal, Percent and Date pipes.
|
* Internal function to format numbers used by Decimal, Percent and Date pipes.
|
||||||
*/
|
*/
|
||||||
function formatNumber(
|
function formatNumber(
|
||||||
pipe: Type, value: number, style: NumberFormatStyle, digits: string, currency: string = null,
|
pipe: Type, value: number | string, style: NumberFormatStyle, digits: string,
|
||||||
currencyAsSymbol: boolean = false): string {
|
currency: string = null, currencyAsSymbol: boolean = false): string {
|
||||||
if (isBlank(value)) return null;
|
if (isBlank(value)) return null;
|
||||||
|
// Convert strings to numbers
|
||||||
|
value = isString(value) && NumberWrapper.isNumeric(value) ? +value : value;
|
||||||
if (!isNumber(value)) {
|
if (!isNumber(value)) {
|
||||||
throw new InvalidPipeArgumentException(pipe, value);
|
throw new InvalidPipeArgumentException(pipe, value);
|
||||||
}
|
}
|
||||||
var minInt = 1, minFraction = 0, maxFraction = 3;
|
let minInt = 1;
|
||||||
|
let minFraction = 0;
|
||||||
|
let maxFraction = 3;
|
||||||
if (isPresent(digits)) {
|
if (isPresent(digits)) {
|
||||||
var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits);
|
var parts = RegExpWrapper.firstMatch(_NUMBER_FORMAT_REGEXP, digits);
|
||||||
if (isBlank(parts)) {
|
if (isBlank(parts)) {
|
||||||
|
@ -41,7 +47,7 @@ function formatNumber(
|
||||||
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
|
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NumberFormatter.format(value, defaultLocale, style, {
|
return NumberFormatter.format(value as number, defaultLocale, style, {
|
||||||
minimumIntegerDigits: minInt,
|
minimumIntegerDigits: minInt,
|
||||||
minimumFractionDigits: minFraction,
|
minimumFractionDigits: minFraction,
|
||||||
maximumFractionDigits: maxFraction,
|
maximumFractionDigits: maxFraction,
|
||||||
|
|
|
@ -32,8 +32,19 @@ export function main() {
|
||||||
expect(pipe.transform(1.1234)).toEqual('1.123');
|
expect(pipe.transform(1.1234)).toEqual('1.123');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not support other objects',
|
it('should support strings', () => {
|
||||||
() => { expect(() => pipe.transform(new Object())).toThrowError(); });
|
expect(pipe.transform('12345')).toEqual('12,345');
|
||||||
|
expect(pipe.transform('123', '.2')).toEqual('123.00');
|
||||||
|
expect(pipe.transform('1', '3.')).toEqual('001');
|
||||||
|
expect(pipe.transform('1.1', '3.4-5')).toEqual('001.1000');
|
||||||
|
expect(pipe.transform('1.123456', '3.4-5')).toEqual('001.12346');
|
||||||
|
expect(pipe.transform('1.1234')).toEqual('1.123');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not support other objects', () => {
|
||||||
|
expect(() => pipe.transform(new Object())).toThrowError();
|
||||||
|
expect(() => pipe.transform('123abc')).toThrowError();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue