Skip to content

Commit

Permalink
Removes double escaping (#264)
Browse files Browse the repository at this point in the history
 Removes double escaping

#259
  • Loading branch information
tookam authored and ngaruko committed Nov 13, 2019
1 parent a2c6f7d commit 187ac59
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -44,6 +44,7 @@
"pouchdb-adapter-http": "^7.1.1",
"pouchdb-core": "^7.1.1",
"pouchdb-mapreduce": "^7.1.1",
"properties": "^1.2.1",
"readline-sync": "^1.4.10",
"redact-basic-auth": "^1.0.0",
"request": "^2.88.0",
Expand Down
41 changes: 21 additions & 20 deletions src/fn/upload-custom-translations.js
Expand Up @@ -6,6 +6,7 @@ const pouch = require('../lib/db');
const getApiVersion = require('../lib/get-api-version');
const iso639 = require('iso-639-1');
const { warn } = require('../lib/log');
const properties = require('properties');

const FILE_MATCHER = /messages-.*\.properties/;

Expand Down Expand Up @@ -38,18 +39,19 @@ module.exports = () => {
}
}

var translations = propertiesAsObject(`${dir}/${fileName}`);

return db.get(id)
.catch(e => {
if(e.status === 404) {
return newDocFor(fileName, db, languageName, languageCode);
}

throw e;
})
.then(doc => overwriteProperties(doc, translations))
.then(doc => db.put(doc));
return parse(`${dir}/${fileName}`, { path: true })
.then(parsed =>
db.get(id)
.catch(e => {
if(e.status === 404) {
return newDocFor(fileName, db, languageName, languageCode);
}

throw e;
})
.then(doc => overwriteProperties(doc, parsed))
.then(doc => db.put(doc))
);
}));
});

Expand All @@ -61,14 +63,13 @@ function isLanguageCodeValid(code) {
return regex.test(code);
}

function propertiesAsObject(path) {
const vals = {};
fs.read(path)
.split('\n')
.filter(line => line.includes('='))
.map(line => line.split(/=(.*)/, 2).map(it => it.trim()))
.map(([k, v]) => vals[k] = v);
return vals;
function parse(filePath, options) {
return new Promise((resolve, reject) => {
properties.parse(filePath, options, (err, parsed) => {
if (err) return reject(err);
resolve(parsed);
});
});
}

function overwriteProperties(doc, props) {
Expand Down
@@ -0,0 +1,2 @@
one.escaped.exclamation=one equals one\!
two.escaped.exclamation=one equals one\!\!
15 changes: 15 additions & 0 deletions test/fn/upload-custom-translations.spec.js
Expand Up @@ -433,6 +433,21 @@ describe('upload-custom-translations', () => {
});
});

it('should properly upload translations containing escaped exclamation marks', () => {
mockTestDir(`escaped-exclamation`);
return uploadCustomTranslations()
.then(() => expectTranslationDocs('en'))
.then(() => getTranslationDoc('en'))
.then(messagesEn => {
assert.deepEqual(messagesEn.custom, {
'one.escaped.exclamation':'one equals one!',
'two.escaped.exclamation':'one equals one!!',
});
assert.deepEqual(messagesEn.generic, {});
assert(!messagesEn.values);
});
});

});


Expand Down

0 comments on commit 187ac59

Please sign in to comment.