Created
June 6, 2023 06:06
-
-
Save bryanrsebastian/3a0b9dc9f02a9c5936ec0ab5e4095c85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const fbiPath = 'Z:/'; | |
new Promise( ( resolve, reject ) => { | |
/* Read filenames in specified directory */ | |
fs.readdir( fbiPath, function( error, filenames ) { | |
if ( error ) return reject( error ); | |
/* Filter out only csv files with old to new date */ | |
return resolve( | |
filenames | |
.map( filename => ( { | |
name: filename, | |
time: fs.statSync( fbiPath + '/' + filename ).mtime.getTime() | |
} ) ) // get file and date | |
.sort( ( a, b ) => a.time - b.time ) // sort old to new | |
.filter( file => /\.csv$/i.test( file.name ) ) // filter csv file only | |
); | |
} ); | |
} ) | |
.then( files => { | |
const csvs = files.map( file => { | |
/* Read the content of CSV file */ | |
return fs.readFileSync( fbiPath + '/' + file.name, 'utf8' ).split( '\n' ); | |
} ); | |
/* Content arrays */ | |
return Promise.resolve( csvs ); | |
} ) | |
.then( csvs => { | |
/** | |
* Mergge all CSV to one | |
* .reduce() is for summation of all elements in array | |
*/ | |
const csv = csvs.reduce( ( previousValue, currentValue ) => { | |
/* Check if the first column is really a column name */ | |
if( currentValue[0].includes( 'membership_id' ) ) | |
previousValue['column'] = currentValue.shift(); // Push the column name in the top of the array | |
/* Push each item to column row */ | |
currentValue.forEach( ( value, key ) => { | |
/* Get the column id */ | |
var id = String( value.split( ',' )[0] ); | |
/* If the id is not empty Insert value */ | |
if( id != '' ) | |
previousValue[id] = value; // Can be overriden the existing value with the updated one | |
} ); | |
return previousValue; | |
}, {} ); // Set the initial previousValue as empty object | |
return csv | |
} ) | |
.then( csv => { | |
/** | |
* Convert JSON Object to CSV Format | |
*/ | |
var csvFormat = []; | |
/* Create flag for array key counting */ | |
var i = 0; | |
/* Initialize the column */ | |
csvFormat[i] = csv.column.replace( /(\r\n|\n|\r|")/gm, '' ); // Replace unnecessarry characters | |
/* Remove columnd data */ | |
delete csv.column; | |
/* Move now the object data in array data */ | |
for( var id in csv ) { | |
i++; | |
csvFormat[i] = csv[id].replace( /(\r\n|\n|\r|")/gm, '' ); | |
} | |
/* Convert array to string in CSV Format */ | |
const data = csvFormat.join( '\n' ); | |
/* Write to csv file */ | |
return Promise.resolve( fs.writeFileSync( __dirname + '/fbi.csv', data ) ); | |
} ) | |
.catch( error => console.error( error.message ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment