Skip to content

Commit

Permalink
feat: Export formatDate and some other tool functions
Browse files Browse the repository at this point in the history
Closes #248
  • Loading branch information
sebbo2002 committed Apr 30, 2021
1 parent d5215b1 commit 6142e11
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ export {
ICalWeekday,
ICalTimezone
} from './types';

export {
formatDate,
formatDateTZ,
escape,
foldLines
} from './tools';
17 changes: 15 additions & 2 deletions src/tools.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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 = '';
Expand Down
21 changes: 20 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import ical, {
ICalEventBusyStatus,
ICalEventTransparency,
ICalEventRepeatingFreq,
ICalWeekday
ICalWeekday,
formatDate,
formatDateTZ,
escape,
foldLines
} from '../src';

describe('ical-generator Index', function() {
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 6142e11

Please sign in to comment.