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.
Optional
filepath: stringThe path to the file to be converted.
Protected
_rootProtected
_getProtected
_getReads 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.
The path to the CSV file.
A promise that resolves to the current AioFileConverter
instance, allowing for method chaining.
Protected
isChecks if a directory exists. If it doesn't, it creates the directory and any necessary parent directories.
The path of the directory to check or create.
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.
The path to the JSON file.
A promise that resolves to the current AioFileConverter
instance, allowing for method chaining.
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.
The path to the file to be scanned.
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.
The path to the directory to search.
A single file extension or an array of file extensions to filter by. If undefined, no extension filtering is applied.
Optional
showDir: boolean = falseIf true, directories will be included in the results. Defaults to false.
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.
.json
, it directly converts the content
(which should be a JSON object or array) to CSV using json2csv
..yaml
, it first converts the content
(which should be a YAML object) to a JSON object, then converts it to CSV.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);
Converts the current file content to JSON format.
This method handles the conversion from CSV or YAML to JSON.
.csv
, it directly stringifies the content
(which should be an array of objects from csv2json)..yaml
, it directly stringifies the content
(which should be an object from YAML.parse).Returns the current instance of AioFileConverter
with the content
property updated to a JSON string.
Converts the current file content to YAML format.
This method handles the conversion from JSON or CSV to YAML.
.json
, it directly converts the content
(which should be a JSON object or array) to YAML using YAML.stringify
..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 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);
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.
The path to the YAML file.
A promise that resolves to the current AioFileConverter
instance, allowing for method chaining.
// TODO