Skip to content

Commit

Permalink
fix(#496): use valueAsDate instead of returnNativeDates (#497)
Browse files Browse the repository at this point in the history
closes #496
  • Loading branch information
MauricioRobayo authored Feb 27, 2023
1 parent 3945fa1 commit ee4acad
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ Returns:
];
```

You can opt-in to have the function return native JavaScript dates instead of strings for the `date` and `celebrationDate` properties by using the `returnNativeDate` option:
You can opt-in to have the function return native JavaScript dates instead of strings for the `date` and `celebrationDate` properties by using the `valueAsDate` option:

```js
const colombianHolidays2015 = colombianHolidays({
year: 2015,
returnNativeDate: true,
valueAsDate: true,
});
```

Expand All @@ -232,7 +232,7 @@ If the year is omitted, by default the function will return the holidays for the
```js
const currentYearHolidaysAsStrings = colombianHolidays();
const currentYearHolidaysAsDates = colombianHolidays({
returnNativeDate: true,
valueAsDate: true,
});
```

Expand Down
16 changes: 7 additions & 9 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ function generateUtcStringFromDate(date: Date): string {

function getHoliday(
holiday: Holiday,
options?: { year?: number; returnNativeDate: false | undefined }
options?: { year?: number; valueAsDate: false | undefined }
): ColombianHoliday;
function getHoliday(
holiday: Holiday,
options: { year?: number; returnNativeDate: true }
options: { year?: number; valueAsDate: true }
): ColombianHolidayWithNativeDate;
function getHoliday(
holiday: Holiday,
options?: { year?: number; returnNativeDate?: boolean }
options?: { year?: number; valueAsDate?: boolean }
): ColombianHoliday | ColombianHolidayWithNativeDate;
function getHoliday(
holiday: Holiday,
{
year = new Date().getUTCFullYear(),
returnNativeDate = false,
}: { year?: number; returnNativeDate?: boolean } = {}
valueAsDate = false,
}: { year?: number; valueAsDate?: boolean } = {}
): unknown {
const holidayDate = getHolidayDate(holiday, year);
const celebrationDate =
Expand All @@ -85,10 +85,8 @@ function getHoliday(
: holidayDate;

return {
date: returnNativeDate
? holidayDate
: generateUtcStringFromDate(holidayDate),
celebrationDate: returnNativeDate
date: valueAsDate ? holidayDate : generateUtcStringFromDate(holidayDate),
celebrationDate: valueAsDate
? celebrationDate
: generateUtcStringFromDate(celebrationDate),
name: holiday.name,
Expand Down
10 changes: 5 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ describe.each(years)("Gets all holidays for %p", (year) => {
const holidays = colombianHolidays({
year,
month,
returnNativeDate: true,
valueAsDate: true,
});
const expected = holidaysYears[year]
.filter(
Expand All @@ -501,20 +501,20 @@ describe.each(years)("Gets all holidays for %p", (year) => {
);

it.each(timezones)(
"Should return holidays formatted as string for %p if returnNativeDate is set to false",
"Should return holidays formatted as string for %p if valueAsDate is set to false",
(timezone) => {
timezone_mock.register(timezone);
expect(colombianHolidays({ year, returnNativeDate: false })).toEqual(
expect(colombianHolidays({ year, valueAsDate: false })).toEqual(
holidaysYears[year]
);
}
);

it.each(timezones)(
"Should return holidays with native JS date for %p if returnNativeDate is set to true",
"Should return holidays with native JS date for %p if valueAsDate is set to true",
(timezone) => {
timezone_mock.register(timezone);
expect(colombianHolidays({ year, returnNativeDate: true })).toEqual(
expect(colombianHolidays({ year, valueAsDate: true })).toEqual(
holidaysYears[year].map(transformStringsToDates)
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ export type Month = MonthNumbers | Omit<number, MonthNumbers>;
export function colombianHolidays(
options?:
| undefined
| { year?: number; month?: Month; returnNativeDate: false | undefined }
| { year?: number; month?: Month; valueAsDate: false | undefined }
): ColombianHoliday[];
export function colombianHolidays(options?: {
year?: number;
month?: Month;
returnNativeDate: true;
valueAsDate: true;
}): ColombianHolidayWithNativeDate[];
export function colombianHolidays(options?: {
year?: number;
month?: Month;
returnNativeDate?: boolean;
valueAsDate?: boolean;
}): ColombianHoliday[] | ColombianHolidayWithNativeDate[];
export function colombianHolidays({
year = new Date().getUTCFullYear(),
month,
returnNativeDate = false,
valueAsDate = false,
}: {
year?: number;
month?: Month;
returnNativeDate?: boolean;
valueAsDate?: boolean;
} = {}): unknown {
if (year < FIRST_HOLIDAY_YEAR || year > LAST_HOLIDAY_YEAR) {
throw new Error(
Expand All @@ -40,7 +40,7 @@ export function colombianHolidays({
}

return holidays
.map((holiday) => getHoliday(holiday, { year, returnNativeDate }))
.map((holiday) => getHoliday(holiday, { year, valueAsDate }))
.filter((holiday) => {
if (month === undefined) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/holidaysWithinInterval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("holidaysWithinInterval", () => {
const result = holidaysWithinInterval({
start,
end,
returnNativeDate: true,
valueAsDate: true,
});
expect(result.length).toBe(18);
expect(result).toEqual(
Expand Down
12 changes: 6 additions & 6 deletions src/utils/holidaysWithinInterval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import { ColombianHoliday, ColombianHolidayWithNativeDate } from "../types";
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
returnNativeDate: false | undefined;
valueAsDate: false | undefined;
}): ColombianHoliday[];
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
returnNativeDate: true;
valueAsDate: true;
}): ColombianHolidayWithNativeDate[];
export function holidaysWithinInterval(options: {
start: Date;
end: Date;
returnNativeDate?: boolean;
valueAsDate?: boolean;
}): ColombianHoliday[] | ColombianHolidayWithNativeDate[];
export function holidaysWithinInterval({
start,
end,
returnNativeDate = false,
valueAsDate = false,
}: {
start: Date;
end: Date;
returnNativeDate?: boolean;
valueAsDate?: boolean;
}): unknown {
if (start >= end) {
throw new Error("end date should be greater than start date");
Expand All @@ -32,7 +32,7 @@ export function holidaysWithinInterval({
const yearStart = start.getUTCFullYear();

const holidays = Array.from({ length: yearEnd - yearStart + 1 }, (_, i) =>
colombianHolidays({ year: i + yearStart, returnNativeDate })
colombianHolidays({ year: i + yearStart, valueAsDate })
).flat();

return holidays.filter(({ celebrationDate }) => {
Expand Down

0 comments on commit ee4acad

Please sign in to comment.