From 6142e11f631011aff9be8df129980dfac411d4f2 Mon Sep 17 00:00:00 2001 From: Sebastian Pekarek Date: Fri, 30 Apr 2021 15:59:17 +0200 Subject: [PATCH] feat: Export `formatDate` and some other tool functions Closes #248 --- src/index.ts | 7 +++++++ src/tools.ts | 17 +++++++++++++++-- test/index.ts | 21 ++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index ab1f31e92..39b1bdbf3 100755 --- a/src/index.ts +++ b/src/index.ts @@ -95,3 +95,10 @@ export { ICalWeekday, ICalTimezone } from './types'; + +export { + formatDate, + formatDateTZ, + escape, + foldLines +} from './tools'; diff --git a/src/tools.ts b/src/tools.ts index 92cb2e418..9a1473971 100755 --- a/src/tools.ts +++ b/src/tools.ts @@ -1,5 +1,6 @@ 'use strict'; + import type {Moment, Duration} from 'moment'; import type {Moment as MomentTZ} from 'moment-timezone'; import type {Dayjs} from 'dayjs'; @@ -8,6 +9,9 @@ import type { RRule } from 'rrule'; import {ICalDateTimeValue, ICalOrganizer} from './types'; +/** + * 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); @@ -88,8 +92,11 @@ export function formatDate (timezone: string | null, d: ICalDateTimeValue, dateo } } -// For information about this format, see RFC 5545, section 3.3.5 -// https://tools.ietf.org/html/rfc5545#section-3.3.5 +/** + * 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; @@ -105,12 +112,18 @@ export function formatDateTZ (timezone: string | null, property: string, date: I return property + tzParam + ':' + formatDate(timezone, date, false, floating); } +/** + * Escapes special characters in the given string + */ export function escape (str: string | unknown): string { return String(str).replace(/[\\;,"]/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 = ''; diff --git a/test/index.ts b/test/index.ts index c7618f0fb..7d7f17298 100644 --- a/test/index.ts +++ b/test/index.ts @@ -16,7 +16,11 @@ import ical, { ICalEventBusyStatus, ICalEventTransparency, ICalEventRepeatingFreq, - ICalWeekday + ICalWeekday, + formatDate, + formatDateTZ, + escape, + foldLines } from '../src'; describe('ical-generator Index', function() { @@ -92,4 +96,19 @@ describe('ical-generator Index', function() { assert.ok(ICalWeekday); }); }); + + describe('Tools', function () { + it('should export formatDate', function () { + assert.ok(typeof formatDate === 'function'); + }); + it('should export formatDateTZ', function () { + assert.ok(typeof formatDateTZ === 'function'); + }); + it('should export escape', function () { + assert.ok(typeof escape === 'function'); + }); + it('should export foldLines', function () { + assert.ok(typeof foldLines === 'function'); + }); + }); });