{"size": 1081, "ext": "js", "max_stars_repo_path": "outputs/outStream.js", "max_stars_repo_name": "TotalTechGeek/deejay-file-query", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "outputs/outStream.js", "max_issues_repo_name": "TotalTechGeek/deejay-file-query", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "outputs/outStream.js", "max_forks_repo_name": "TotalTechGeek/deejay-file-query", "max_forks_repo_forks_event_min_datetime": "2022-02-19T07:34:32.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-19T07:34:32.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 30.0277777778, "max_line_length": 109, "alphanum_fraction": 0.6614246068}
// @ts-check
import path from 'path'
import fs from 'fs'

/**
 * Creates an output stream for the tool.
 * If no file is specified, it will write to stdout.
 * 
 * @param {string} [file] The file to write to. 
 * @param {string} format The extension that the file should have.
 * @returns A write stream to export data to.
 */
export function createOutStream (file, format = 'json') {
    if (file) {
        // remove all nonalphanumeric, space, or underscore characters, periods or slashes from the file name
        file = file.replace(/[^a-zA-Z0-9_\s./]/g, '')
        return fs.createWriteStream(correctExtension(file, format))
    }
    return process.stdout
}


/**
 * Corrects the extension on the file name.
 * @param {string} file The name of the file.
 * @param {string} format The extension that is expected
 * @returns 
 */
export function correctExtension (file, format = 'json') {
    // if the extension does not match the format, add it
    if(path.extname(file).substring(1).toLowerCase() !== format) {
        return `${file}.${format}`
    }
    return file
} 
