From ace69b9e62ff1003ed7c3cb84258bb7e9d37f21e Mon Sep 17 00:00:00 2001 From: veeralsharma Date: Sat, 3 Oct 2020 20:13:24 +0530 Subject: [PATCH 1/6] added csv to json for javascript --- csv_to_vanilla-i18n/csv_to_json.js | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 csv_to_vanilla-i18n/csv_to_json.js diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js new file mode 100644 index 0000000..b955569 --- /dev/null +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -0,0 +1,33 @@ +const fs = require('fs'); +const path= "" // enter your data file path here +const mycsv = fs.readFileSync(path,'utf-8'); +console.log(mycsv); + +const myjson = csvJSON(mycsv) +console.log(myjson); + +function csvJSON(csv){ + + var lines=csv.split("\n"); + + var result = []; + + var headers=lines[0].split(","); + + for(var i=1;i Date: Sun, 4 Oct 2020 03:09:42 +0530 Subject: [PATCH 2/6] added option to take path input --- csv_to_vanilla-i18n/csv_to_json.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js index b955569..83fc909 100644 --- a/csv_to_vanilla-i18n/csv_to_json.js +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -1,5 +1,15 @@ const fs = require('fs'); -const path= "" // enter your data file path here +var path= "" // enter your data file path here +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +rl.question("Enter path for the csv file ",(input) => { + path=input; +}) + const mycsv = fs.readFileSync(path,'utf-8'); console.log(mycsv); From 0aa88d3cc607b9030a8eff3d3e82db975af952c4 Mon Sep 17 00:00:00 2001 From: Veeral Sharma <43784167+veeralsharma@users.noreply.github.com> Date: Sun, 4 Oct 2020 03:10:51 +0530 Subject: [PATCH 3/6] Update csv_to_json.js --- csv_to_vanilla-i18n/csv_to_json.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js index 83fc909..048035b 100644 --- a/csv_to_vanilla-i18n/csv_to_json.js +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -11,7 +11,6 @@ rl.question("Enter path for the csv file ",(input) => { }) const mycsv = fs.readFileSync(path,'utf-8'); -console.log(mycsv); const myjson = csvJSON(mycsv) console.log(myjson); @@ -36,8 +35,7 @@ function csvJSON(csv){ result.push(obj); } - - //return result; //JavaScript object - return JSON.stringify(result); //JSON + + return JSON.stringify(result); } From 8e2c37493b4ff7154d71032f953bd3a88b1c30ef Mon Sep 17 00:00:00 2001 From: veeralsharma Date: Sun, 11 Oct 2020 22:25:37 +0530 Subject: [PATCH 4/6] all fixes done --- csv_to_vanilla-i18n/csv_to_json.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js index 048035b..675841b 100644 --- a/csv_to_vanilla-i18n/csv_to_json.js +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -1,19 +1,28 @@ +const { dir } = require('console'); const fs = require('fs'); var path= "" // enter your data file path here const readline = require("readline"); + const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); + rl.question("Enter path for the csv file ",(input) => { path=input; -}) +const mycsv = fs.readFileSync(__dirname+path,'utf-8'); -const mycsv = fs.readFileSync(path,'utf-8'); const myjson = csvJSON(mycsv) console.log(myjson); +try { + fs.writeFileSync("./newfile.json", myjson) +} catch (err) { + console.error(err) +} +}) + function csvJSON(csv){ @@ -36,6 +45,6 @@ function csvJSON(csv){ } - return JSON.stringify(result); + return JSON.stringify(result[0]); } From e99c882292e954c9cce4a176fcb07d9b8eeb2f00 Mon Sep 17 00:00:00 2001 From: veeralsharma Date: Sun, 11 Oct 2020 22:28:23 +0530 Subject: [PATCH 5/6] final commit --- csv_to_vanilla-i18n/csv_to_json.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js index 675841b..b1e090c 100644 --- a/csv_to_vanilla-i18n/csv_to_json.js +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -15,9 +15,8 @@ const mycsv = fs.readFileSync(__dirname+path,'utf-8'); const myjson = csvJSON(mycsv) -console.log(myjson); try { - fs.writeFileSync("./newfile.json", myjson) + fs.writeFileSync("./vanilla-i18n/new.json", myjson) } catch (err) { console.error(err) } From 230add8e2eaa74945be3638c62793aea2752af68 Mon Sep 17 00:00:00 2001 From: veeralsharma Date: Mon, 12 Oct 2020 18:31:03 +0530 Subject: [PATCH 6/6] corrected output --- csv_to_vanilla-i18n/csv_to_json.js | 105 ++++++++++++------ .../vanilla-i18n/espa\303\261ola.json" | 2 +- .../vanilla-i18n/fran\303\247ais.json" | 2 +- ...\340\245\215\340\244\246\340\245\200.json" | 2 +- 4 files changed, 71 insertions(+), 40 deletions(-) diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js index b1e090c..114bbb5 100644 --- a/csv_to_vanilla-i18n/csv_to_json.js +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -1,49 +1,80 @@ -const { dir } = require('console'); const fs = require('fs'); -var path= "" // enter your data file path here -const readline = require("readline"); - -const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout +const path = require('path'); +const readline = require('readline'); +const r1 = readline.createInterface({ + input: process.stdin, + output: process.stdout, }); +const dirPath = path.join(__dirname, '/vanilla-i18n'); + +r1.question('Enter path of file: ', (csv) => { + if (fs.existsSync(csv)) { + const csvFile = fs.readFileSync(csv, { encoding: 'utf8' }); + const languages = csvFile + .split('\r\n') + .splice(0, 1) + .join('') + .split(',') + .splice(1); + const csvArr = csvFile.split('\r\n').splice(1); + let csvTemp = []; + for (let k = 0; k < csvArr.length; k++) { + csvTemp[k] = csvArr[k].split(','); + } -rl.question("Enter path for the csv file ",(input) => { - path=input; -const mycsv = fs.readFileSync(__dirname+path,'utf-8'); + const transpose = csvTemp[0].map((_, colIndex) => + csvTemp.map((row) => row[colIndex]) + ); + const csvTransposed = transpose.join('\n').split('\n').splice(1); + const header = transpose.join('\n').split('\n').splice(0, 1); -const myjson = csvJSON(mycsv) -try { - fs.writeFileSync("./vanilla-i18n/new.json", myjson) -} catch (err) { - console.error(err) -} -}) + const constructObj = (str, parentObj, data) => { + if (str.split('.').length === 1) { + parentObj[str] = data; + return parentObj; + } + let curKey = str.split('.')[0]; + if (!parentObj[curKey]) parentObj[curKey] = {}; + parentObj[curKey] = constructObj( + str.split('.').slice(1).join('.'), + parentObj[curKey], + data + ); + return parentObj; + }; -function csvJSON(csv){ + const csvFinal = csvTransposed.map((row) => { + let obj = {}; + let rowData = row.split(','); + header[0].split(',').forEach(function (val, idx) { + obj = constructObj(val, obj, rowData[idx]); + }); + return obj; + }); - var lines=csv.split("\n"); - - var result = []; - - var headers=lines[0].split(","); - - for(var i=1;i { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath); + create(csvFinal, dirPath); + } else { + for (let i = 0; i < languages.length; i++) { + fs.writeFileSync( + `${dirPath}/${languages[i]}.json`, + JSON.stringify(csvFinal.splice(0, 1)).replace(/^\[|]$/g, ''), + 'UTF-8' + ); } - - result.push(obj); - - } + } + }; - return JSON.stringify(result[0]); - } + create(csvFinal, dirPath); + r1.close(); + } else { + console.log('Invalid File CSV file'); + r1.close(); + } +}); \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" "b/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" index 353ed78..2df6dc8 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" @@ -1 +1 @@ -{"language":"idioma","form":{"name":"nombre","age":"a\u00f1os","resp":{"yes":"si"}}} \ No newline at end of file +{"language":"idioma","form":{"name":"nombre","age":"años","resp":{"yes":"si"}}} \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" "b/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" index b69905b..0f04079 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" @@ -1 +1 @@ -{"language":"Langue","form":{"name":"Nom","age":"\u00e2ge","resp":{"yes":"Oui"}}} \ No newline at end of file +{"language":"Langue","form":{"name":"Nom","age":"âge","resp":{"yes":"Oui"}}} \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" "b/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" index 0e8a84f..a426c76 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" @@ -1 +1 @@ -{"language":"\u092d\u093e\u0937\u093e","form":{"name":"\u0928\u093e\u092e","age":"\u0909\u092e\u094d\u0930","resp":{"yes":"\u0939\u093e\u0901"}}} \ No newline at end of file +{"language":"भाषा","form":{"name":"नाम","age":"उम्र","resp":{"yes":"हाँ"}}} \ No newline at end of file