-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor start and year end and show daily NS dates
- Loading branch information
Showing
3 changed files
with
8,866 additions
and
619 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,49 +1,65 @@ | ||
const n = require('nanakshahi') | ||
const ics = require('ics', './dist') | ||
const { | ||
writeFileSync | ||
} = require('fs') | ||
const ics = require('ics') | ||
const { writeFileSync } = require('fs') | ||
const path = require('path') | ||
|
||
const newDateFormat = (a) => { | ||
return a.toISOString().split('T')[0].split('-').map(Number) | ||
} | ||
|
||
const getGurpurab = (a) => { | ||
return n.getGurpurabsForDay(a) | ||
const formatNanakshahiDate = (gregorianDate) => { | ||
const nDate = n.getNanakshahiDate(gregorianDate) | ||
if (!nDate || !nDate.englishDate || !nDate.punjabiDate) return null | ||
|
||
return { | ||
en: `${nDate.englishDate.date} ${nDate.englishDate.monthName}`, | ||
pa: `${nDate.punjabiDate.date} ${nDate.punjabiDate.monthName}`, | ||
year: nDate.englishDate.year, | ||
day: { | ||
en: nDate.englishDate.day, | ||
pa: nDate.punjabiDate.day | ||
} | ||
} | ||
} | ||
|
||
const calEvents = [] | ||
let gurpurab = {} | ||
const yearStart = new Date(new Date().getFullYear(), 0, 1) | ||
const yearEnd = new Date(new Date().getFullYear(), 12, 0) | ||
const day = yearStart | ||
while (day.setDate(day.getDate() + 1) <= yearEnd) { | ||
gurpurab = getGurpurab(day) | ||
|
||
const currentYear = new Date().getFullYear() | ||
const yearStart = new Date(currentYear, 0, 1) | ||
const yearEnd = new Date(currentYear + 1, 11, 31) | ||
const day = new Date(yearStart) | ||
|
||
while (day <= yearEnd) { | ||
Check failure on line 32 in main.js
|
||
const currentDay = new Date(day) | ||
const gurpurab = n.getGurpurabsForDay(currentDay) | ||
const nDate = formatNanakshahiDate(currentDay) | ||
|
||
for (const g in gurpurab) { | ||
calEvents.push({ | ||
start: newDateFormat(day), | ||
start: newDateFormat(currentDay), | ||
title: gurpurab[g].en, | ||
description: gurpurab[g].pa, | ||
description: gurpurab[g].pa + (nDate ? `\n\nNanakshahi Date:\n${nDate.pa} ${nDate.year} NS` : ''), | ||
categories: [gurpurab[g].type] | ||
}) | ||
} | ||
|
||
if (nDate) { | ||
calEvents.push({ | ||
start: newDateFormat(currentDay), | ||
duration: { days: 1 }, | ||
title: `${nDate.en} ${nDate.year} NS (${nDate.day.en})`, | ||
description: `Nanakshahi Date:\n${nDate.en} ${nDate.year} NS (${nDate.day.en})\n${nDate.pa} ${nDate.year} NS (${nDate.day.pa})`, | ||
categories: ['daily'] | ||
}) | ||
} | ||
|
||
day.setDate(day.getDate() + 1) | ||
} | ||
|
||
const { | ||
error, | ||
value | ||
} = ics.createEvents(calEvents) | ||
|
||
if (error) { | ||
console.log(error) | ||
} | ||
console.log(value) | ||
const { error, value } = ics.createEvents(calEvents) | ||
if (error) throw error | ||
|
||
const filePath = path.join(__dirname, '/nanakshahi.ics') | ||
writeFileSync(filePath, value) | ||
|
||
module.exports = { | ||
newDateFormat, | ||
getGurpurab | ||
} | ||
module.exports = { newDateFormat, formatNanakshahiDate } | ||
Oops, something went wrong.