// TODO

  • markdown to html vise versa

Hierarchy (View Summary)

Constructors

  • Initializes a new instance of the AioFileConverter class.

    The constructor takes an optional filepath parameter, which specifies the path to the file to be converted. If filepath is not specified, the object is initialized without a file path.

    Parameters

    • Optionalfilepath: string

      The path to the file to be converted.

    Returns AioFileConverter

Properties

_root: string
ext: string = ''
filename: string = ''
filepath: undefined | string = ''
outputPath: string = ''

Accessors

  • get content(): any
  • Returns any

  • set content(value: any): void
  • Parameters

    • value: any

    Returns void

Methods

  • Extracts the file extension from a given filepath and returns it in lowercase.

    Parameters

    • filepath: string

      The path of the file.

    Returns string

    The file extension in lowercase.


    _getExtension('/path/to/my/file.txt') // returns '.txt'
  • Returns the filename without the extension from a given filepath.

    Parameters

    • filepath: string

      The filepath to extract the filename from.

    Returns string

    The filename without the extension.


    _getFilename('/path/to/my/file.txt') // returns 'file'
  • Reads and parses a CSV file into a JavaScript array of objects.

    This asynchronous method reads the content of a CSV file specified by filepath. It determines the file extension, reads the file content, and then uses the csv2json method from the json-2-csv library to convert the CSV content into an array of JavaScript objects. Each object in the array represents a row from the CSV file, with keys corresponding to the CSV headers. The resulting array is stored in the content property of the AioFileConverter instance.

    Parameters

    • filepath: string

      The path to the CSV file.

    Returns Promise<AioFileConverter>

    A promise that resolves to the current AioFileConverter instance, allowing for method chaining.


    const converter = new AioFileConverter();
    // Reads 'data.csv' and parses its content.
    await converter.csv('data.csv');
    // Outputs an array of objects representing the CSV data.
    console.log(converter.content);
  • Parameters

    • filepath: string

    Returns void

  • Checks if a directory exists. If it doesn't, it creates the directory and any necessary parent directories.

    Parameters

    • dir: string

      The path of the directory to check or create.

    Returns Promise<void>

    • A promise that resolves when the directory exists or has been created.
    • Throws an error if there's a problem creating the directory.

    const fileOp = new AioFileOperation();
    await fileOp.isDirectoryExists('/path/to/new/directory');
    // If '/path/to/new/directory' doesn't exist, it will be created.
  • Reads and parses a JSON file into a JavaScript object.

    This asynchronous method reads the content of a JSON file specified by filepath. It determines the file extension, reads the file content, and then uses JSON.parse to convert the JSON content into a JavaScript object. The resulting object is stored in the content property of the AioFileConverter instance.

    Parameters

    • filepath: string

      The path to the JSON file.

    Returns Promise<AioFileConverter>

    A promise that resolves to the current AioFileConverter instance, allowing for method chaining.


    const converter = new AioFileConverter();
    // Reads 'data.json' and parses its content.
    await converter.json('data.json');
    // Outputs the parsed JSON content as a JavaScript object.
    console.log(converter.content);
  • Returns void

  • Opens a file at the specified path and returns a file handle.

    Parameters

    • filePath: string

      The path of the file to open.

    Returns Promise<FileHandle>

    • A promise that resolves to a FileHandle object for the opened file.
    • Throws an error if the file cannot be opened.

    const fileOp = new AioFileOperation();
    const fileHandle = await fileOp.open('/path/to/file.txt');
  • Reads a file synchronously and returns its content as a string.

    Parameters

    • path: string

      The path to the file to be read.

    Returns Promise<string>

    • A promise that resolves to the content of the file as a string. *

    const fileOp = new AioFileOperation();
    const lines = await fileOp.read('/path/to/large/file.txt');
    console.log(lines); // Output: ['Line 1', 'Line 2', 'Line 3', ...]
  • Removes a file if it exists.

    Parameters

    • filePath: string

      The path to the file to be removed.

    Returns Promise<void>

    • A promise that resolves when the file has been removed. *

    const fileOp = new AioFileOperation();
    await fileOp.remove('/path/to/large/file.txt');
  • Reads a file line by line and returns an array of strings, where each string is a line from the file.

    This method opens a file, reads it line by line, and stores each line as a separate string in an array. It uses an asynchronous iterator to efficiently handle large files without loading the entire content into memory at once.

    Parameters

    • filePath: string

      The path to the file to be scanned.

    Returns Promise<string[]>

    • A promise that resolves to an array of strings, where each string is a line from the file.

    const fileOp = new AioFileOperation();
    const lines = await fileOp.scan('/path/to/large/file.txt');
    console.log(lines); // Output: ['Line 1', 'Line 2', 'Line 3', ...]
  • Searches for files within a specified directory, optionally filtering by file extension.

    This method recursively scans a directory and its subdirectories for files. It can filter the results based on a specified file extension or an array of extensions. If no extension is provided, it returns all files. It also has an option to include directories in the results.

    Parameters

    • directory: string

      The path to the directory to search.

    • ext: undefined | string | string[]

      A single file extension or an array of file extensions to filter by. If undefined, no extension filtering is applied.

    • OptionalshowDir: boolean = false

      If true, directories will be included in the results. Defaults to false.

    Returns Promise<undefined | Dirent[]>

    • A promise that resolves to an array of Dirent objects representing the found files.
    • Throws an error if there is a problem accessing the directory, except for 'ENOENT' errors.

    const fileOp = new AioFileOperation();
    // Search for all .txt files in the 'mydir' directory
    const txtFiles = await fileOp.search('mydir', '.txt');
    // Search for all .txt and .md files in the 'mydir' directory
    const txtAndMdFiles = await fileOp.search('mydir', ['.txt', '.md']);
    // Search for all files and directories in the 'mydir' directory
    const allFilesAndDirs = await fileOp.search('mydir', undefined, true);
  • Converts the current file content to CSV format.

    This method handles the conversion from JSON or YAML to CSV.

    • If the current file extension is .json, it directly converts the content (which should be a JSON object or array) to CSV using json2csv.
    • If the current file extension is .yaml, it first converts the content (which should be a YAML object) to a JSON object, then converts it to CSV.

    Returns this

    Returns the current instance of AioFileConverter with the content property updated to a CSV string.


    const converter = new AioFileConverter();
    // Assuming data.json contains JSON data
    await converter.json('data.json');
    // Converts the JSON data to a CSV string
    converter.toCsv();
    // Output: CSV string representation of the JSON data
    console.log(converter.content);
    // Assuming data.yaml contains YAML data
    await converter.yaml('data.yaml');
    // Converts the YAML data to a CSV string
    converter.toCsv();
    // Output: CSV string representation of the YAML data
    console.log(converter.content);
  • Returns void

  • Converts the current file content to JSON format.

    This method handles the conversion from CSV or YAML to JSON.

    • If the current file extension is .csv, it directly stringifies the content (which should be an array of objects from csv2json).
    • If the current file extension is .yaml, it directly stringifies the content (which should be an object from YAML.parse).

    Returns this

    Returns the current instance of AioFileConverter with the content property updated to a JSON string.


    const converter = new AioFileConverter();
    // Assuming data.csv contains CSV data
    await converter.csv('data.csv');
    // Converts the CSV data to a JSON string
    converter.toJson();
    // Output: JSON string representation of the CSV data
    console.log(converter.content);
  • Returns void

  • Converts the current file content to YAML format.

    This method handles the conversion from JSON or CSV to YAML.

    • If the current file extension is .json, it directly converts the content (which should be a JSON object or array) to YAML using YAML.stringify.
    • If the current file extension is .csv, it first converts the content (which should be an array of objects from csv2json) to a JSON string, then parses it back to a JSON object, and finally converts it to YAML.

    Returns this

    Returns the current instance of AioFileConverter with the content property updated to a YAML string.


    const converter = new AioFileConverter();
    // Assuming data.json contains JSON data
    await converter.json('data.json');
    // Converts the JSON data to a YAML string
    converter.toYaml();
    // Output: YAML string representation of the JSON data
    console.log(converter.content);
    // Assuming data.csv contains CSV data
    await converter.csv('data.csv');
    // Converts the CSV data to a YAML string
    converter.toYaml();
    // Output: YAML string representation of the CSV data
    console.log(converter.content);
  • Writes an array of strings to a file, overwriting any existing content.

    Parameters

    • outputFilePath: string

      The path to the file to be written.

    • content: string[]

      The array of strings to be written to the file. *

    Returns Promise<void>


    const fileOp = new AioFileOperation();
    const dataText = ['Line 1', 'Line 2', 'Line 3', ...]
    const lines = await fileOp.write('/path/to/large/file.txt', dataText);
  • Reads and parses a YAML file into a JavaScript object.

    This asynchronous method reads the content of a YAML file specified by filepath. It determines the file extension, reads the file content, and then uses the YAML.parse method from the yamljs library to convert the YAML content into a JavaScript object. The resulting object is stored in the content property of the AioFileConverter instance.

    Parameters

    • filepath: string

      The path to the YAML file.

    Returns Promise<AioFileConverter>

    A promise that resolves to the current AioFileConverter instance, allowing for method chaining.

       const converter = new AioFileConverter();
    // Reads 'config.yaml' and parses its content.
    await converter.yaml('config.yaml');
    // Outputs the parsed YAML content as a JavaScript object.
    console.log(converter.content);