UNPKG

icalzone

Version:

iCalzone is a light-weight VTIMEZONE provider

60 lines (59 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getZoneString = exports.getZoneLines = void 0; const zones_1 = require("./zones"); // This module must be generated with `yarn run build-zones`. function renderZoneSub(data) { const { n, f, t, r, s } = data; return [ `TZNAME:${n}`, `TZOFFSETFROM:${f}`, `TZOFFSETTO:${t || f}`, `DTSTART:${s || zones_1.defaultStart}`, ...(r ? [`RRULE:FREQ=${r.f || 'YEARLY'};BYMONTH=${r.m};BYDAY=${r.d}`] : []), ]; } /** * Tries to resolve a given time zone to iCalendar time zone component * (`VTIMEZONE`) as string array (usually for further processing). * @param zoneName Time zone name (e.g. `America/Los_Angeles`) * @param includeWrapper Set to `false` to avoid including lines for * `BEGIN:VTIMEZONE` and `END:VTIMEZONE`. * @returns Lines of the iCalendar time zone component (`VTIMEZONE`), each line * individually as an array of strings. */ function getZoneLines(zoneName, includeWrapper = true) { const zoneData = zones_1.zonesMap.get(zoneName); if (zoneData) { const { s, d } = zoneData; const lines = [ ...(includeWrapper ? ['BEGIN:VTIMEZONE'] : []), `TZID:${zoneName}`, // `X-LIC-LOCATION:${zoneName}`, // Who uses this? 'BEGIN:STANDARD', ...renderZoneSub(s), 'END:STANDARD', ...(d ? [ 'BEGIN:DAYLIGHT', ...renderZoneSub(d), 'END:DAYLIGHT', ] : []), ...(includeWrapper ? ['END:VTIMEZONE'] : []), ]; return lines; } } exports.getZoneLines = getZoneLines; /** * Tries to resolve a given time zone to iCalendar time zone component * (`VTIMEZONE`) as string. * @param zoneName Time zone name (e.g. `America/Los_Angeles`) * @param includeWrapper Set to `false` to avoid including lines for * `BEGIN:VTIMEZONE` and `END:VTIMEZONE`. * @returns The iCalendar time zone component (`VTIMEZONE`) as string * with `\r\n` line breaks. */ function getZoneString(zoneName, includeWrapper = true) { const lines = getZoneLines(zoneName, includeWrapper); return lines === null || lines === void 0 ? void 0 : lines.join('\r\n'); } exports.getZoneString = getZoneString;