HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/wp-recipe-maker/assets/js/shared/CSV.js
const CSV_BOM = '\ufeff';

const toCsvCell = ( value ) => {
    if ( null === value || typeof value === 'undefined' ) {
        return '';
    }

    const cellValue = String( value ).replace( /"/g, '""' );

    return `"${ cellValue }"`;
};

const sanitizeFileName = ( fileName ) => {
    const sanitized = String( fileName )
        .trim()
        .replace( /[^a-z0-9._-]+/gi, '-' )
        .replace( /-+/g, '-' )
        .replace( /^[-.]+|[-.]+$/g, '' );

    return sanitized || 'report';
};

export function downloadToCsv( fileName, headers, rows ) {
    const csvContent = [
        headers.map( ( header ) => toCsvCell( header ) ).join( ',' ),
        ...rows.map(
            ( row ) => row.map( ( value ) => toCsvCell( value ) ).join( ',' )
        ),
    ].join( '\r\n' );

    const blob = new Blob( [ CSV_BOM + csvContent ], { type: 'text/csv;charset=utf-8;' } );
    const csvFileName = `${ sanitizeFileName( fileName ) }.csv`;

    if ( window.navigator && window.navigator.msSaveOrOpenBlob ) {
        window.navigator.msSaveOrOpenBlob( blob, csvFileName );
        return;
    }

    const url = window.URL.createObjectURL( blob );
    const link = document.createElement( 'a' );

    link.setAttribute( 'href', url );
    link.setAttribute( 'download', csvFileName );
    link.style.display = 'none';

    document.body.appendChild( link );
    link.click();
    document.body.removeChild( link );

    window.URL.revokeObjectURL( url );
}