Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] added csv to json for javascript #4

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions csv_to_vanilla-i18n/csv_to_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const fs = require('fs');
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(',');
}

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 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;
};

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;
});

const create = (csvFinal, dirPath) => {
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'
);
}
}
};

create(csvFinal, dirPath);

r1.close();
} else {
console.log('Invalid File CSV file');
r1.close();
}
});
2 changes: 1 addition & 1 deletion csv_to_vanilla-i18n/vanilla-i18n/española.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"language":"idioma","form":{"name":"nombre","age":"a\u00f1os","resp":{"yes":"si"}}}
{"language":"idioma","form":{"name":"nombre","age":"años","resp":{"yes":"si"}}}
2 changes: 1 addition & 1 deletion csv_to_vanilla-i18n/vanilla-i18n/français.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"language":"Langue","form":{"name":"Nom","age":"\u00e2ge","resp":{"yes":"Oui"}}}
{"language":"Langue","form":{"name":"Nom","age":"âge","resp":{"yes":"Oui"}}}
2 changes: 1 addition & 1 deletion csv_to_vanilla-i18n/vanilla-i18n/हिन्दी.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"language":"\u092d\u093e\u0937\u093e","form":{"name":"\u0928\u093e\u092e","age":"\u0909\u092e\u094d\u0930","resp":{"yes":"\u0939\u093e\u0901"}}}
{"language":"भाषा","form":{"name":"नाम","age":"उम्र","resp":{"yes":"हाँ"}}}