import * as FileSaver from 'file-saver' ; import data from './data' ; import time from './time' ; import safariFileSave from './safariFileSave' ; import CalendarRecurrence from '../types/CalendarRecurrence' ; import { FORMAT } from '../constants' ; param { string } str returns { string } const formatText = ( str = '' ) : string => { return str . replace ( /\\/g , '\\\\' ) . replace ( /\n/g , '\\n' ) . replace ( /[,;]/g , '\\$&' ) ; } ; param { string } icsData returns { Blob } const getBlob = ( icsData ) : Blob => { return new Blob ( [ icsData ] , { type : 'application/octet-stream' } ) ; } ; param { string } title returns { string } const getFileName = ( title ) : string => { if ( ! title ) { return 'event.ics' ; } return `${ title . replace ( /[^\w ]/g , '' ) }.ics` ; } ; returns { string } const getUid = ( ) : string => { return Math . random ( ) . toString ( 36 ) . substr ( 2 ) ; } ; returns { string } const getProdId = ( ) : string => { return typeof window !== 'undefined' ? window . location . host : 'datebook' ; } ; param { CalendarRecurrence } recurrence returns { string } const getRrule = ( recurrence ) : string => { const rrule = { FREQ : recurrence . frequency , INTERVAL : recurrence . interval ?. toString ( ) , COUNT : recurrence . count ?. toString ( ) , WKST : recurrence . weekstart , BYDAY : recurrence . weekdays , BYMONTHDAY : recurrence . monthdays } ; if ( recurrence . end ) { rrule . UNTIL = time . formatDate ( recurrence . end , FORMAT . FULL ) ; } return data . toIcsParamString ( rrule ) ; } ; returns { boolean } const isSafari = ( ) : boolean => { return window . hasOwnProperty ( 'safari' ) || ( /^((?!chrome|android).)*safari/i . test ( navigator . userAgent ) && ! /(cr|fx)ios[^a-z]/i . test ( navigator . userAgent ) ) ; } ; param { string } fileName param { string } data const download = ( fileName , data ) : void => { if ( isSafari ( ) ) { safariFileSave ( data , fileName ) ; } else { const blob = getBlob ( data ) ; FileSaver . saveAs ( blob , fileName ) ; } } ; export default { formatText , getBlob , getFileName , getUid , getProdId , getRrule , download } ;