Skip to content

Commit 28c63da

Browse files
authored
Merge pull request #3 from tiagocastro070/main
feat(formatDate): adds new date helper function
2 parents 4a99fd5 + bee33bc commit 28c63da

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* Please refer to the terms of the license agreement in the root of the project
3+
*
4+
* (c) 2025 Feedzai
5+
*/
6+
import { formatDate } from "../../../../src/functions";
7+
8+
const DEFAULT_LOCALES = { locales: "en-US" };
9+
10+
describe("formatDate", () => {
11+
it("should format date string correctly", () => {
12+
expect(formatDate({ date: "2024-01-15", ...DEFAULT_LOCALES })).to.equal(
13+
"January 15"
14+
);
15+
expect(formatDate({ date: "2024-12-31", ...DEFAULT_LOCALES })).to.equal(
16+
"December 31"
17+
);
18+
});
19+
20+
it("should format Date object correctly", () => {
21+
const dateObject = new Date("2024-01-15");
22+
23+
expect(formatDate({ date: dateObject, ...DEFAULT_LOCALES })).to.equal(
24+
"January 15"
25+
);
26+
});
27+
28+
it("should format timestamp correctly", () => {
29+
const timestamp = new Date("2024-01-15").getTime();
30+
31+
expect(formatDate({ date: timestamp, ...DEFAULT_LOCALES })).to.equal(
32+
"January 15"
33+
);
34+
});
35+
36+
it("should handle different date string formats", () => {
37+
expect(formatDate({ date: "2024/01/15", ...DEFAULT_LOCALES })).to.equal(
38+
"January 15"
39+
);
40+
expect(formatDate({ date: "01-15-2024", ...DEFAULT_LOCALES })).to.equal(
41+
"January 15"
42+
);
43+
expect(formatDate({ date: "15 Jan 2024", ...DEFAULT_LOCALES })).to.equal(
44+
"January 15"
45+
);
46+
});
47+
48+
it("should include year when specified", () => {
49+
const customOptions = { year: "numeric" } as const;
50+
51+
expect(
52+
formatDate({
53+
date: "2024-01-15",
54+
options: customOptions,
55+
...DEFAULT_LOCALES,
56+
})
57+
).to.equal("January 15, 2024");
58+
});
59+
60+
it("should show short month format", () => {
61+
const customOptions = { month: "short" } as const;
62+
63+
expect(
64+
formatDate({
65+
date: "2024-01-15",
66+
options: customOptions,
67+
...DEFAULT_LOCALES,
68+
})
69+
).to.equal("Jan 15");
70+
});
71+
72+
it("should include weekday", () => {
73+
const customOptions = { weekday: "long" } as const;
74+
75+
expect(
76+
formatDate({
77+
date: "2024-01-15",
78+
options: customOptions,
79+
...DEFAULT_LOCALES,
80+
})
81+
).to.equal("Monday, January 15");
82+
});
83+
84+
it("should respect all provided options while maintaining defaults", () => {
85+
const customOptions = {
86+
weekday: "long",
87+
year: "numeric",
88+
month: "long",
89+
day: "numeric",
90+
} as const;
91+
92+
expect(
93+
formatDate({
94+
date: "2024-01-15",
95+
options: customOptions,
96+
...DEFAULT_LOCALES,
97+
})
98+
).to.equal("Monday, January 15, 2024");
99+
});
100+
101+
it("should handle invalid dates", () => {
102+
expect(() => formatDate({ date: "invalid-date" })).throw();
103+
expect(() => formatDate({ date: "2024-13-45" })).throw();
104+
});
105+
106+
it("should handle empty input", () => {
107+
expect(() => formatDate({ date: "" })).throw();
108+
});
109+
110+
it("should handle null and undefined", () => {
111+
expect(() => formatDate({ date: null })).throw();
112+
expect(() => formatDate()).throw();
113+
});
114+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: formatDate
3+
---
4+
5+
Formats a date value into a localized string representation using the `toLocaleDateString()` method.
6+
7+
### API
8+
9+
```typescript
10+
interface IFormatDateConfig {
11+
date: ConstructorParameters<typeof Date>[0];
12+
locales?: Intl.LocalesArgument;
13+
options?: Intl.DateTimeFormatOptions;
14+
}
15+
16+
function formatDate(config: IFormatDateConfig): string;
17+
```
18+
19+
### Usage
20+
21+
```typescript
22+
import { formatDate } from "@feedzai/js-utilities";
23+
24+
const formattedDate = formatDate({ date: "2023-12-25" });
25+
26+
console.log(formattedDate); // "December 25"
27+
```

src/functions/dates/format-date.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Please refer to the terms of the license agreement in the root of the project
3+
*
4+
* (c) 2025 Feedzai
5+
*/
6+
7+
interface IFormatDateConfig {
8+
date: ConstructorParameters<typeof Date>[0];
9+
locales?: Intl.LocalesArgument;
10+
options?: Intl.DateTimeFormatOptions;
11+
}
12+
13+
const DEFAULT_OPTIONS = {
14+
month: "long",
15+
day: "numeric",
16+
} as const;
17+
18+
/**
19+
* Formats a date value into a localized string representation.
20+
*
21+
* @param {IFormatDateConfig} config - Configuration object for date formatting.
22+
* @returns {string} A localized date string. By default, returns the date in "{Month} {Day}" format (e.g., "December 25")
23+
*
24+
* @example
25+
* formatDate({ date: "2023-12-25" })
26+
* // => "December 25"
27+
*/
28+
export const formatDate = ({
29+
date: dateValue,
30+
locales,
31+
options,
32+
}: IFormatDateConfig) => {
33+
const date = new Date(dateValue);
34+
35+
if (!dateValue || isNaN(date.getTime())) {
36+
throw new Error("Invalid date value");
37+
}
38+
39+
return date.toLocaleDateString(locales, {
40+
...DEFAULT_OPTIONS,
41+
...options,
42+
});
43+
};

src/functions/dates/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* (c) 2024 Feedzai
55
*/
66
export * from "./get-browser-timezone";
7+
export * from "./format-date";

0 commit comments

Comments
 (0)