feat(common): DatePipe supports ISO string

Closes #7794
This commit is contained in:
laco0416 2016-03-28 23:14:32 +09:00 committed by Misko Hevery
parent 7d853dd9ad
commit abc266fa35
2 changed files with 18 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import {PipeTransform, Pipe, Injectable} from '@angular/core';
import { import {
isDate, isDate,
isNumber, isNumber,
isString,
DateWrapper, DateWrapper,
isBlank, isBlank,
} from '../../src/facade/lang'; } from '../../src/facade/lang';
@ -29,8 +30,9 @@ var defaultLocale: string = 'en-US';
* *
* expression | date[:format] * expression | date[:format]
* *
* where `expression` is a date object or a number (milliseconds since UTC epoch) and * where `expression` is a date object or a number (milliseconds since UTC epoch) or an ISO string
* `format` indicates which date/time components to include: * (https://www.w3.org/TR/NOTE-datetime) and `format` indicates which date/time components to
* include:
* *
* | Component | Symbol | Short Form | Long Form | Numeric | 2-digit | * | Component | Symbol | Short Form | Long Form | Numeric | 2-digit |
* |-----------|:------:|--------------|-------------------|-----------|-----------| * |-----------|:------:|--------------|-------------------|-----------|-----------|
@ -105,6 +107,8 @@ export class DatePipe implements PipeTransform {
if (isNumber(value)) { if (isNumber(value)) {
value = DateWrapper.fromMillis(value); value = DateWrapper.fromMillis(value);
} else if (isString(value)) {
value = DateWrapper.fromISOString(value);
} }
if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) { if (StringMapWrapper.contains(DatePipe._ALIASES, pattern)) {
pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern); pattern = <string>StringMapWrapper.get(DatePipe._ALIASES, pattern);
@ -112,5 +116,13 @@ export class DatePipe implements PipeTransform {
return DateFormatter.format(value, defaultLocale, pattern); return DateFormatter.format(value, defaultLocale, pattern);
} }
supports(obj: any): boolean { return isDate(obj) || isNumber(obj); } supports(obj: any): boolean {
if (isDate(obj) || isNumber(obj)) {
return true;
}
if (isString(obj) && isDate(DateWrapper.fromISOString(obj))) {
return true;
}
return false;
}
} }

View File

@ -30,10 +30,13 @@ export function main() {
describe("supports", () => { describe("supports", () => {
it("should support date", () => { expect(pipe.supports(date)).toBe(true); }); it("should support date", () => { expect(pipe.supports(date)).toBe(true); });
it("should support int", () => { expect(pipe.supports(123456789)).toBe(true); }); it("should support int", () => { expect(pipe.supports(123456789)).toBe(true); });
it("should support ISO string",
() => { expect(pipe.supports("2015-06-15T21:43:11Z")).toBe(true); });
it("should not support other objects", () => { it("should not support other objects", () => {
expect(pipe.supports(new Object())).toBe(false); expect(pipe.supports(new Object())).toBe(false);
expect(pipe.supports(null)).toBe(false); expect(pipe.supports(null)).toBe(false);
expect(pipe.supports("")).toBe(false);
}); });
}); });