Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | 1x 1x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 205x 1x 1x 205x 205x 157x 157x 157x 157x 157x 157x 157x 157x 157x 24x 24x 24x 24x 157x 157x 10x 10x 157x 157x 22x 22x 22x 22x 22x 22x 157x 157x 125x 125x 157x 157x 157x 157x 48x 37x 37x 18x 19x 37x 37x 31x 6x 37x 11x 7x 1x 6x 7x 7x 3x 4x 7x 4x 4x 4x 4x 4x 1x 1x 3x 1x 1x 2x 2x 2x 2x 4x 4x 3x 1x 4x 205x 1x 1x 1x 1x 1x 1x 1x 88x 88x 88x 88x 19x 19x 19x 19x 19x 19x 88x 88x 88x 1x 1x 1x 1x 1x 186x 44x 186x 186x 1x 1x 1x 1x 1x 50x 847x 847x 847x 19786x 19786x 19786x 19786x 2x 2x 19786x 19786x 19786x 19786x 19786x 27x 27x 27x 19786x 19786x 19786x 847x 50x 50x 1x 1x 1x 1x 1x 102x 36x 7x 7x 2x 2x 7x 1x 1x 7x 7x 36x 36x 66x 6x 5x 1x 1x 5x 2x 2x 2x 2x 6x 6x 60x 9x 1x 1x 9x 9x 8x 51x 51x 12x 12x 51x 51x 102x 1x 1x 134x 134x 134x 134x 134x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 16x 16x 7x 7x 7x 7x 7x 9x 6x 6x 6x 6x 6x 16x 14x 12x 12x 12x 12x 12x 12x 12x 30x 30x 3x 3x 3x 3x 3x 27x 2x 2x 2x 2x 2x 30x 30x 1x 1x 30x 30x 30x 1x 1x 1x 1x 1x 1x 166x 166x 166x 166x 26x 26x 166x 166x 166x 1x 1x 1x 1x 1x 1x 561x 561x 561x 561x 552x 561x 15x 15x 561x 410x 410x 561x 561x 561x 3x 3x 561x 561x 561x 109x 109x 561x 561x 561x 1x 1x 124x 94x 94x 124x 124x 3x 3x 124x 124x 124x 1x 1x 206x 206x 206x 206x 1x 22x 22x 1x 24x 8x 3x 3x 3x 24x 1x 180x 180x 1x 1x 17x 17x 1x 1x 163x 163x 1x 1x 139x 72x 72x 139x 6x 6x 139x 139x 139x 1x 1x 37x 37x 37x 37x 23x 23x 23x 37x 37x 37x 37x 37x 5x 5x 5x 37x 5x 5x 37x 37x 32x 32x 37x 4x 4x 4x 37x 37x 37x 26x 26x 26x 37x 37x 37x 3x 3x 29x 1x 1x 37x 37x 37x | 'use strict'; import { type ICalDateTimeValue, type ICalDayJsStub, type ICalLuxonDateTimeStub, type ICalMomentDurationStub, type ICalMomentStub, type ICalMomentTimezoneStub, type ICalOrganizer, type ICalRRuleStub } from './types.ts'; /** * Converts a valid date/time object supported by this library to a string. */ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateonly?: boolean, floating?: boolean): string { if(timezone?.startsWith('/')) { timezone = timezone.substr(1); } if(typeof d === 'string' || d instanceof Date) { const m = new Date(d); // (!dateonly && !floating) || !timezone => utc let s = m.getUTCFullYear() + String(m.getUTCMonth() + 1).padStart(2, '0') + m.getUTCDate().toString().padStart(2, '0'); // (dateonly || floating) && timezone => tz if(timezone) { s = m.getFullYear() + String(m.getMonth() + 1).padStart(2, '0') + m.getDate().toString().padStart(2, '0'); } if(dateonly) { return s; } if(timezone) { s += 'T' + m.getHours().toString().padStart(2, '0') + m.getMinutes().toString().padStart(2, '0') + m.getSeconds().toString().padStart(2, '0'); return s; } s += 'T' + m.getUTCHours().toString().padStart(2, '0') + m.getUTCMinutes().toString().padStart(2, '0') + m.getUTCSeconds().toString().padStart(2, '0') + (floating ? '' : 'Z'); return s; } else if(isMoment(d)) { // @see https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/ const m = timezone ? (isMomentTZ(d) && !d.tz() ? d.clone().tz(timezone) : d) : (floating || (dateonly && isMomentTZ(d) && d.tz()) ? d : d.utc()); return m.format('YYYYMMDD') + (!dateonly ? ( 'T' + m.format('HHmmss') + (floating || timezone ? '' : 'Z') ) : ''); } else if(isLuxonDate(d)) { const m = timezone ? d.setZone(timezone) : (floating || (dateonly && d.zone.type !== 'system') ? d : d.setZone('utc')); return m.toFormat('yyyyLLdd') + (!dateonly ? ( 'T' + m.toFormat('HHmmss') + (floating || timezone ? '' : 'Z') ) : ''); } else { // @see https://day.js.org/docs/en/plugin/utc let m = d; if(timezone) { m = typeof d.tz === 'function' ? d.tz(timezone) : d; } else if(floating) { // m = d; } else if (typeof d.utc === 'function') { m = d.utc(); } else { throw new Error('Unable to convert dayjs object to UTC value: UTC plugin is not available!'); } return m.format('YYYYMMDD') + (!dateonly ? ( 'T' + m.format('HHmmss') + (floating || timezone ? '' : 'Z') ) : ''); } } /** * Converts a valid date/time object supported by this library to a string. * For information about this format, see RFC 5545, section 3.3.5 * https://tools.ietf.org/html/rfc5545#section-3.3.5 */ export function formatDateTZ (timezone: string | null, property: string, date: ICalDateTimeValue | Date | string, eventData?: {floating?: boolean | null, timezone?: string | null}): string { let tzParam = ''; let floating = eventData?.floating || false; if (eventData?.timezone) { tzParam = ';TZID=' + eventData.timezone; // This isn't a 'floating' event because it has a timezone; // but we use it to omit the 'Z' UTC specifier in formatDate() floating = true; } return property + tzParam + ':' + formatDate(timezone, date, false, floating); } /** * Escapes special characters in the given string */ export function escape (str: string | unknown, inQuotes: boolean): string { return String(str).replace(inQuotes ? /[\\"]/g : /[\\;,]/g, function (match) { return '\\' + match; }).replace(/(?:\r\n|\r|\n)/g, '\\n'); } /** * Trim line length of given string */ export function foldLines (input: string): string { return input.split('\r\n').map(function (line) { let result = ''; let c = 0; for (let i = 0; i < line.length; i++) { let ch = line.charAt(i); // surrogate pair, see https://mathiasbynens.be/notes/javascript-encoding#surrogate-pairs if (ch >= '\ud800' && ch <= '\udbff') { ch += line.charAt(++i); } // TextEncoder is available in browsers and node.js >= 11.0.0 const charsize = new TextEncoder().encode(ch).length; c += charsize; if (c > 74) { result += '\r\n '; c = charsize; } result += ch; } return result; }).join('\r\n'); } export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray: ({key: string, value: string})[] | [string, string][] | Record<string, string>): void; export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray: string, value: string): void; export function addOrGetCustomAttributes (data: {x: [string, string][]}): ({key: string, value: string})[]; export function addOrGetCustomAttributes (data: {x: [string, string][]}, keyOrArray?: ({key: string, value: string})[] | [string, string][] | Record<string, string> | string | undefined, value?: string | undefined): void | ({key: string, value: string})[] { if (Array.isArray(keyOrArray)) { data.x = keyOrArray.map((o: {key: string, value: string} | [string, string]) => { if(Array.isArray(o)) { return o; } if (typeof o.key !== 'string' || typeof o.value !== 'string') { throw new Error('Either key or value is not a string!'); } if (o.key.substr(0, 2) !== 'X-') { throw new Error('Key has to start with `X-`!'); } return [o.key, o.value] as [string, string]; }); } else if (typeof keyOrArray === 'object') { data.x = Object.entries(keyOrArray).map(([key, value]) => { if (typeof key !== 'string' || typeof value !== 'string') { throw new Error('Either key or value is not a string!'); } if (key.substr(0, 2) !== 'X-') { throw new Error('Key has to start with `X-`!'); } return [key, value]; }); } else if (typeof keyOrArray === 'string' && typeof value === 'string') { if (keyOrArray.substr(0, 2) !== 'X-') { throw new Error('Key has to start with `X-`!'); } data.x.push([keyOrArray, value]); } else { return data.x.map(a => ({ key: a[0], value: a[1] })); } } export function generateCustomAttributes (data: {x: [string, string][]}): string { const str = data.x .map(([key, value]) => key.toUpperCase() + ':' + escape(value, false)) .join('\r\n'); return str.length ? str + '\r\n' : ''; } /** * Check the given string or ICalOrganizer. Parses * the string for name and email address if possible. * * @param attribute Attribute name for error messages * @param value Value to parse name/email from */ export function checkNameAndMail (attribute: string, value: string | ICalOrganizer): ICalOrganizer { let result: ICalOrganizer | null = null; if (typeof value === 'string') { const match = value.match(/^(.+) ?<([^>]+)>$/); if (match) { result = { name: match[1].trim(), email: match[2].trim() }; } else if(value.includes('@')) { result = { name: value.trim(), email: value.trim() }; } } else if (typeof value === 'object') { result = { name: value.name, email: value.email, mailto: value.mailto, sentBy: value.sentBy }; } if (!result && typeof value === 'string') { throw new Error( '`' + attribute + '` isn\'t formated correctly. See https://sebbo2002.github.io/ical-generator/develop/'+ 'reference/interfaces/ICalOrganizer.html' ); } else if (!result) { throw new Error( '`' + attribute + '` needs to be a valid formed string or an object. See https://sebbo2002.github.io/'+ 'ical-generator/develop/reference/interfaces/ICalOrganizer.html' ); } if (!result.name) { throw new Error('`' + attribute + '.name` is empty!'); } return result; } /** * Checks if the given string `value` is a * valid one for the type `type` */ export function checkEnum(type: Record<string, string>, value: unknown): unknown { const allowedValues = Object.values(type); const valueStr = String(value).toUpperCase(); if (!valueStr || !allowedValues.includes(valueStr)) { throw new Error(`Input must be one of the following: ${allowedValues.join(', ')}`); } return valueStr; } /** * Checks if the given input is a valid date and * returns the internal representation (= moment object) */ export function checkDate(value: ICalDateTimeValue, attribute: string): ICalDateTimeValue { // Date & String if( (value instanceof Date && isNaN(value.getTime())) || (typeof value === 'string' && isNaN(new Date(value).getTime())) ) { throw new Error(`\`${attribute}\` has to be a valid date!`); } if(value instanceof Date || typeof value === 'string') { return value; } // Luxon if(isLuxonDate(value) && value.isValid === true) { return value; } // Moment / Moment Timezone if((isMoment(value) || isDayjs(value)) && value.isValid()) { return value; } throw new Error(`\`${attribute}\` has to be a valid date!`); } export function toDate(value: ICalDateTimeValue): Date { if(typeof value === 'string' || value instanceof Date) { return new Date(value); } if(isLuxonDate(value)) { return value.toJSDate(); } return value.toDate(); } export function isMoment(value: ICalDateTimeValue): value is ICalMomentStub { // @ts-expect-error _isAMomentObject is a private property return value != null && value._isAMomentObject != null; } export function isMomentTZ(value: ICalDateTimeValue): value is ICalMomentTimezoneStub { return isMoment(value) && 'tz' in value && typeof value.tz === 'function'; } export function isDayjs(value: ICalDateTimeValue): value is ICalDayJsStub { return typeof value === 'object' && value !== null && !(value instanceof Date) && !isMoment(value) && !isLuxonDate(value); } export function isLuxonDate(value: ICalDateTimeValue): value is ICalLuxonDateTimeStub { return typeof value === 'object' && value !== null && 'toJSDate' in value && typeof value.toJSDate === 'function'; } export function isMomentDuration(value: unknown): value is ICalMomentDurationStub { return value !== null && typeof value === 'object' && 'asSeconds' in value && typeof value.asSeconds === 'function'; } export function isRRule(value: unknown): value is ICalRRuleStub { return value !== null && typeof value === 'object' && 'between' in value && typeof value.between === 'function' && typeof value.toString === 'function'; } export function toJSON(value: ICalDateTimeValue | null | undefined): string | null | undefined { if(!value) { return null; } if(typeof value === 'string') { return value; } return value.toJSON(); } export function toDurationString(seconds: number): string { let string = ''; // < 0 if(seconds < 0) { string = '-'; seconds *= -1; } string += 'P'; // DAYS if(seconds >= 86400) { string += Math.floor(seconds / 86400) + 'D'; seconds %= 86400; } if(!seconds && string.length > 1) { return string; } string += 'T'; // HOURS if(seconds >= 3600) { string += Math.floor(seconds / 3600) + 'H'; seconds %= 3600; } // MINUTES if(seconds >= 60) { string += Math.floor(seconds / 60) + 'M'; seconds %= 60; } // SECONDS if(seconds > 0) { string += seconds + 'S'; } else if(string.length <= 2) { string += '0S'; } return string; } |