mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-08 17:46:45 +05:30
feat: check leap year (service file)
This commit is contained in:
parent
65574872ae
commit
1d2d714143
1 changed files with 25 additions and 0 deletions
25
src/pages/tools/time/check-leap-years/service.ts
Normal file
25
src/pages/tools/time/check-leap-years/service.ts
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function isLeapYear(year: number): boolean {
|
||||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
||||
}
|
||||
|
||||
export function checkLeapYear(input: string): string {
|
||||
if (!input) return '';
|
||||
|
||||
const years = input
|
||||
.split('\n')
|
||||
.map((year) => year.trim())
|
||||
.filter((year) => year !== '');
|
||||
|
||||
const results = years.map((yearStr) => {
|
||||
if (!/^\d{1,4}$/.test(yearStr)) {
|
||||
return `${yearStr}: Invalid year`;
|
||||
}
|
||||
|
||||
const year = Number(yearStr);
|
||||
return `${year} ${
|
||||
isLeapYear(year) ? 'is a leap year.' : 'is not a leap year.'
|
||||
}`;
|
||||
});
|
||||
|
||||
return results.join('\n');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue