-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
481/add native date to result (#482)
* feat(#481): add aditional arg * test(#481): date instead of string * test(#481): consolidate tests * refactor(#481): improve api * docs(#481): include returnNativeDate option * refactor(#481): naming * test(#481): add more cases * docs(#481): remove unnecessary stuff
- Loading branch information
1 parent
b4104e7
commit dc02e50
Showing
7 changed files
with
136 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
module.exports = { | ||
coverageThreshold: { | ||
global: { | ||
branches: 100, | ||
functions: 100, | ||
lines: 100, | ||
statements: 100, | ||
}, | ||
}, | ||
collectCoverageFrom: ['src/**/*'], | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
collectCoverageFrom: ["src/**/*"], | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,52 @@ | ||
import getHoliday from "./helpers"; | ||
import holidays from "./holidays"; | ||
import { ColombianHoliday } from "./types"; | ||
import { ColombianHoliday, ColombianHolidayWithNativeDate } from "./types"; | ||
|
||
// pascua package year limits | ||
export const FIRST_HOLIDAY_YEAR = 1583; | ||
export const LAST_HOLIDAY_YEAR = 4099; | ||
|
||
function colombianHolidays( | ||
year: number = new Date().getFullYear() | ||
): ColombianHoliday[] { | ||
year?: number, | ||
options?: undefined | { returnNativeDate?: false | undefined } | ||
): ColombianHoliday[]; | ||
function colombianHolidays( | ||
year?: number, | ||
options?: { returnNativeDate?: true } | ||
): ColombianHolidayWithNativeDate[]; | ||
function colombianHolidays( | ||
year?: number, | ||
options?: { returnNativeDate?: boolean } | ||
): (ColombianHoliday | ColombianHolidayWithNativeDate)[]; | ||
function colombianHolidays( | ||
year: number = new Date().getFullYear(), | ||
{ returnNativeDate = false }: { returnNativeDate?: boolean } = {} | ||
): unknown[] { | ||
if (year < FIRST_HOLIDAY_YEAR || year > LAST_HOLIDAY_YEAR) { | ||
throw new Error( | ||
`The year should be between ${FIRST_HOLIDAY_YEAR} and ${LAST_HOLIDAY_YEAR}` | ||
); | ||
} | ||
|
||
return holidays | ||
.map((holiday) => getHoliday(holiday, year)) | ||
.sort((a, b) => a.celebrationDate.localeCompare(b.celebrationDate)); | ||
.map((holiday) => getHoliday(holiday, year, { returnNativeDate })) | ||
.sort((a, b) => { | ||
if ( | ||
a.celebrationDate instanceof Date && | ||
b.celebrationDate instanceof Date | ||
) { | ||
return a.celebrationDate.getTime() - b.celebrationDate.getTime(); | ||
} | ||
|
||
if ( | ||
typeof a.celebrationDate === "string" && | ||
typeof b.celebrationDate === "string" | ||
) { | ||
return a.celebrationDate.localeCompare(b.celebrationDate); | ||
} | ||
|
||
throw new Error("Invariant violation: this state is not possible."); | ||
}); | ||
} | ||
|
||
export default colombianHolidays; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters