Skip to content
Open
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
27 changes: 17 additions & 10 deletions csv-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ var Config = require(path.resolve(process.cwd(), process.argv[2]));

module.exports = {
processCsv: processCsv
}
};

function processCsv(fileContents) {
if (!fileContents) {
return {};
}
var results = [];
var results;
var regex = new RegExp('([^' + Config.csvEol + ']*)' + Config.csvEol, 'g');

for (var i = 0; i < Config.csvEolAlt.length; i++) {
fileContents = fileContents.split(Config.csvEolAlt[i]).join(Config.csvEol);
}
Expand All @@ -23,26 +23,33 @@ function processCsv(fileContents) {
for (var j = 0; j < languages.length; j++) {
to[languages[j]] = {};
}

while ((results = regex.exec(fileContents)) !== null) {
for (var str in Config.jsonReplace) {
if (Config.jsonReplace.hasOwnProperty(str)) {
results[1] = results[1].replace(new RegExp(str, 'g'), Config.jsonReplace[str]);
}
}

var translations = results[1].split(Config.csvFieldSeparator);
var nss = translations[0].slice(0, translations[0].lastIndexOf('.')).split('.');
var id = translations[0].slice(translations[0].lastIndexOf('.') + 1);
var id = translations[0];
var dotIndex = id.lastIndexOf('.');
var nss;
if (dotIndex === -1) {
nss = [];
} else {
nss = id.slice(0, dotIndex).split('.');
id = id.slice(dotIndex + 1);
}
translations.shift();
for (var languageIndex = 0; languageIndex < languages.length; languageIndex++) {
var currentPointer = to[languages[languageIndex]];

for (var nsIndex = 0; nsIndex < nss.length; nsIndex++) {
if (currentPointer[nss[nsIndex]] === undefined) {
currentPointer[nss[nsIndex]] = {};
}

currentPointer = currentPointer[nss[nsIndex]];
}

Expand All @@ -51,6 +58,6 @@ function processCsv(fileContents) {
}
}
}

return to;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-translate-csv",
"version": "0.8.3",
"version": "0.8.4",
"description": "A CLI build tool to generate JSON files required by Angular Translate from a single CSV file",
"main": "app.js",
"scripts": {
Expand Down