Skip to content

Instantly share code, notes, and snippets.

@htammen
Created January 21, 2022 16:52
Show Gist options
  • Save htammen/f62bf2951e5ca6cf45254df40909670f to your computer and use it in GitHub Desktop.
Save htammen/f62bf2951e5ca6cf45254df40909670f to your computer and use it in GitHub Desktop.
Returns holidays for a year, country and state. Many countries are supported
import dateHolidays from 'https://cdn.skypack.dev/date-holidays';
import { ld } from 'https://x.nest.land/deno-lodash@1.0.0/mod.ts'
import { parse } from "https://deno.land/std/flags/mod.ts"
/**
* returns a command line argument by name from the args array
*/
const getArg = function(argName: string, args: any[]): string {
//@ts-ignore
const arg = ld.find(args, (arg: string[]) => arg[0] === argName)
return arg ? arg[1]: null
}
/**
* Helper method that logs a msg if debug flag is set
*/
const logDebug=function(msg: any) {
if(bDebug) {
console.log(msg)
}
}
// parse the args array into an array of 2-dimensionals array.
// Each array element is an array with 2 members: 0-argument, 1-argument value
const args = ld.toPairs(parse(Deno.args))
//console.log(args)
// save the command line parameters in variables.
const bHelp = getArg('help', args)
const bDebug = getArg('debug', args)
const year = getArg('year', args) || getArg('_', args)[0] || new Date().getFullYear()
const country = getArg('country', args) || getArg('_', args)[1] || "DE"
const state = getArg('state', args) || getArg('_', args)[2] || "NI"
const bCountries = getArg('countries', args)
const bStates = getArg('states', args)
const bQuery = getArg('query', args)
logDebug(args)
logDebug(year)
class HolidayViewer {
private hd: dateHolidays
constructor() {
this.hd = new dateHolidays(country, state, null, null);
}
processInput() {
if(bCountries) {
this.showCountries()
} else if(bStates) {
this.showStates()
} else if(bQuery) {
this.showQueryResults()
} else {
this.showHolidays()
}
}
showQueryResults() {
let queryResult = this.hd.query(country)
console.log(queryResult)
}
showCountries() {
let countries = this.hd.getCountries()
console.log(countries)
}
showStates() {
let states = this.hd.getStates(country)
console.log(states);
}
showHolidays() {
let holidays:Array<any> = this.hd.getHolidays(year, "de");
let publics:Array<{date:string, name:string}> = holidays.filter(holiday => {return holiday.type === "public"})
.map(entry => {return {date: entry.date, name: entry.name}});
console.log(JSON.stringify(publics));
// Deno.stdout.writeSync(new TextEncoder().encode(JSON.stringify(publics)));
}
}
if(bHelp) {
console.log('Usage: holidays <year country state> [options]')
console.log('Usage: hoidays [options]')
console.log(`
Returns the holidays for a year, country and state.
ex: holidays 2022 DE NI -> this returns the holidays for lower saxony in germany
Options
--year: year for which to show the holidays
--country: country for which you want to see the date-holidays. Get a list of countries with holidays --countries
--countries: get a list of supported countries
--states: get a list of available states for a country. Needs --country to be set. Get a list of states with e.g. holidays --states --country DE
--state: state of the country for which you want to see the holidays
--help: show this help screen
--debug: show debug information
`)
Deno.exit(0);
}
new HolidayViewer().processInput();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment