Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(datePipe): numeric string support #9124

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/@angular/common/src/pipes/date_pipe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Pipe, PipeTransform} from '@angular/core';
import {isDate, isNumber, isString, DateWrapper, isBlank,} from '../facade/lang';
import {DateFormatter} from '../facade/intl';
import {StringMapWrapper} from '../facade/collection';
import {DateFormatter} from '../facade/intl';
import {DateWrapper, NumberWrapper, isBlank, isDate, isString} from '../facade/lang';
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';

// TODO: move to a global configurable location along with other i18n components.
Expand Down Expand Up @@ -98,8 +98,8 @@ export class DatePipe implements PipeTransform {
throw new InvalidPipeArgumentException(DatePipe, value);
}

if (isNumber(value)) {
value = DateWrapper.fromMillis(value);
if (NumberWrapper.isNumeric(value)) {
value = DateWrapper.fromMillis(NumberWrapper.parseInt(value, 10));
} else if (isString(value)) {
value = DateWrapper.fromISOString(value);
}
Expand All @@ -110,7 +110,7 @@ export class DatePipe implements PipeTransform {
}

private supports(obj: any): boolean {
if (isDate(obj) || isNumber(obj)) {
if (isDate(obj) || NumberWrapper.isNumeric(obj)) {
return true;
}
if (isString(obj) && isDate(DateWrapper.fromISOString(obj))) {
Expand Down
3 changes: 3 additions & 0 deletions modules/@angular/common/test/pipes/date_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function main() {
describe('supports', () => {
it('should support date', () => { expect(() => pipe.transform(date)).not.toThrow(); });
it('should support int', () => { expect(() => pipe.transform(123456789)).not.toThrow(); });
it('should support numeric strings',
() => { expect(() => pipe.transform('123456789')).not.toThrow(); });

it('should support ISO string',
() => { expect(() => pipe.transform('2015-06-15T21:43:11Z')).not.toThrow(); });

Expand Down
7 changes: 7 additions & 0 deletions modules/@angular/facade/src/lang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ class NumberWrapper {

static double get NaN => double.NAN;

static bool isNumeric(value) {
if(value == null) {
return false;
}
return double.parse(value, (e) => null) != null;
}

static bool isNaN(num value) => value.isNaN;

static bool isInteger(value) => value is int;
Expand Down
2 changes: 2 additions & 0 deletions modules/@angular/facade/src/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ export class NumberWrapper {

static get NaN(): number { return NaN; }

static isNumeric(value: any): boolean { return !isNaN(value - parseFloat(value)); }

static isNaN(value: any): boolean { return isNaN(value); }

static isInteger(value: any): boolean { return Number.isInteger(value); }
Expand Down
25 changes: 23 additions & 2 deletions modules/@angular/facade/test/lang_spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing';

import {RegExpMatcherWrapper, RegExpWrapper, StringWrapper, hasConstructor, isPresent, resolveEnumToken} from '../src/lang';
import {NumberWrapper, RegExpMatcherWrapper, RegExpWrapper, StringWrapper, hasConstructor, isPresent, resolveEnumToken} from '../src/lang';

enum UsefulEnum {
MyToken,
Expand Down Expand Up @@ -51,6 +50,28 @@ export function main() {
});
});

describe('Number', () => {
describe('isNumeric', () => {
it('should return true when passing correct numeric string',
() => { expect(NumberWrapper.isNumeric('2')).toBe(true); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.123, 1e5, negative values


it('should return true when passing correct double string',
() => { expect(NumberWrapper.isNumeric('1.123')).toBe(true); });

it('should return true when passing correct negative string',
() => { expect(NumberWrapper.isNumeric('-2')).toBe(true); });

it('should return true when passing correct scientific notation string',
() => { expect(NumberWrapper.isNumeric('1e5')).toBe(true); });

it('should return false when passing incorrect numeric',
() => { expect(NumberWrapper.isNumeric('a')).toBe(false); });

it('should return false when passing parseable but non numeric',
() => { expect(NumberWrapper.isNumeric('2a')).toBe(false); });
});
});

describe('String', () => {
var s: any /** TODO #9100 */;

Expand Down