fix: return empty string if null or undefined value is passed

This commit is contained in:
Daniel Karadachki 2017-04-18 17:06:56 +03:00
Родитель 6da228a588
Коммит bc4eba1212
5 изменённых файлов: 15 добавлений и 2 удалений

Просмотреть файл

@ -176,6 +176,9 @@ formatters.Q = formatQuarter;
export default function formatDate(date, format, locale = "en") {
if (!isDate(date)) {
if (date === undefined || date === null) {
return '';
}
return date;
}

Просмотреть файл

@ -13,7 +13,7 @@ export function toString(value, format, locale) {
}
}
return value !== undefined ? value : "";
return value !== undefined && value !== null ? value : "";
}
export function format(format, values, locale) {

Просмотреть файл

@ -29,6 +29,11 @@ describe('date formatting', () => {
expect(formatDate("foo")).toEqual("foo");
});
it('returns empty string if value is null or undefined', () => {
expect(formatDate(undefined)).toEqual("");
expect(formatDate(null)).toEqual("");
});
it('applies short date format if no format is set', () => {
expect(formatDate(date(2000, 1, 30))).toEqual("1/30/2000");
});

Просмотреть файл

@ -35,6 +35,10 @@ describe('toString', () => {
expect(toString(undefined, "c", "en")).toBe("");
});
it('returns empty string if the value is null', () => {
expect(toString(null, "c", "en")).toBe("");
});
it('returns the value if the format is not defined', () => {
expect(toString(10)).toBe(10);
});

Просмотреть файл

@ -47,8 +47,9 @@ describe('formatNumber', () => {
expect(formatNumber()).toEqual("");
});
it('should return empty string if null value is passed', () => {
it('should return empty string if null or undefined value is passed', () => {
expect(formatNumber(null)).toEqual("");
expect(formatNumber(undefined)).toEqual("");
});
it('should return value if not finite value is passed', () => {