Skip to content

Commit bc4eba1

Browse files
fix: return empty string if null or undefined value is passed
1 parent 6da228a commit bc4eba1

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

src/dates/format-date.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ formatters.Q = formatQuarter;
176176

177177
export default function formatDate(date, format, locale = "en") {
178178
if (!isDate(date)) {
179+
if (date === undefined || date === null) {
180+
return '';
181+
}
179182
return date;
180183
}
181184

src/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function toString(value, format, locale) {
1313
}
1414
}
1515

16-
return value !== undefined ? value : "";
16+
return value !== undefined && value !== null ? value : "";
1717
}
1818

1919
export function format(format, values, locale) {

test/dates.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ describe('date formatting', () => {
2929
expect(formatDate("foo")).toEqual("foo");
3030
});
3131

32+
it('returns empty string if value is null or undefined', () => {
33+
expect(formatDate(undefined)).toEqual("");
34+
expect(formatDate(null)).toEqual("");
35+
});
36+
3237
it('applies short date format if no format is set', () => {
3338
expect(formatDate(date(2000, 1, 30))).toEqual("1/30/2000");
3439
});

test/format.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ describe('toString', () => {
3535
expect(toString(undefined, "c", "en")).toBe("");
3636
});
3737

38+
it('returns empty string if the value is null', () => {
39+
expect(toString(null, "c", "en")).toBe("");
40+
});
41+
3842
it('returns the value if the format is not defined', () => {
3943
expect(toString(10)).toBe(10);
4044
});

test/numbers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ describe('formatNumber', () => {
4747
expect(formatNumber()).toEqual("");
4848
});
4949

50-
it('should return empty string if null value is passed', () => {
50+
it('should return empty string if null or undefined value is passed', () => {
5151
expect(formatNumber(null)).toEqual("");
52+
expect(formatNumber(undefined)).toEqual("");
5253
});
5354

5455
it('should return value if not finite value is passed', () => {

0 commit comments

Comments
 (0)