@mahdi.golzar/dynamicconvertdata
v1.0.0
Published
DynamicDataConverter is a versatile tool for converting data between various formats. It supports dynamic format handling and allows for easy extension to new formats and conversions.
Downloads
3
Readme
DynamicDataConverter
DynamicDataConverter is a versatile tool for converting data between various formats. It supports dynamic format handling and allows for easy extension to new formats and conversions.
Features
- Dynamic Format Handling: Supports conversion between various data formats dynamically.
- Custom Format Registration: Register and use custom formats and converters.
- File Conversion: Convert data files between formats.
- Easy to Use: Simple methods to handle data and files.
Installation
No installation is required for this code as it is self-contained and can be added directly to your project. Ensure you have the following packages:
npm install json2csv csv-parse
Usage
- Create a DynamicDataConverter Instance
const converter = new DynamicDataConverter();
- Convert Data Between Formats
const jsonArray = [
{ id: 1, name: 'John Doe', email: '[email protected]' },
{ id: 2, name: 'Jane Doe', email: '[email protected]' }
];
// Convert JSON to CSV
const csvData = converter.convert(jsonArray, 'json', 'csv');
console.log('CSV Data:\n', csvData);
// Convert CSV to JSON
const csvString = `id,name,email\n1,John Doe,[email protected]\n2,Jane Doe,[email protected]`;
const jsonData = converter.convert(csvString, 'csv', 'json');
console.log('JSON Data:\n', jsonData);
- Save to and Read from Files
Save Data to a File
Read Data from a Fileconverter.saveToFile(csvData, './data.csv');
const fileContent = converter.readFromFile('./data.csv'); console.log('File Content:\n', fileContent);
- Convert File Formats
converter.convertFromFile('./data.csv', 'csv', 'json');