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

Feature/delete extra keys #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "er-translations-fix",
"version": "1.0.1",
"version": "1.0.7",
"description": "",
"main": "./index.js",
"bin": {
Expand Down
29 changes: 26 additions & 3 deletions src/translations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const sync = require('glob').sync;
const fs = require('fs');
const debug = require('debug')('er-translations-fixer');

module.exports = {
processTranslationFiles,
Expand All @@ -12,12 +13,19 @@ function processTranslationFiles({baseFileName = '', filePath = ''}) {
const readFile = filename => JSON.parse(fs.readFileSync(filename, 'utf8'));
const fileMap = files.map(filename => [filename, readFile(filename)]);
const baseFile = readFile(files.find(filename => filename.indexOf(baseFileName) > -1));
const processJson = content => getSortedObject(fillMissingKeys(baseFile, content));
const getFinalStringContent = content => JSON.stringify(processJson(content), null, 2) + '\n';
const getFinalStringContent = content => JSON.stringify(mergeKeys(baseFile, content), null, 2) + '\n';

fileMap.forEach(([filename, content]) => fs.writeFileSync(filename, getFinalStringContent(content), 'utf8'));
}

function mergeKeys(baseObject, object) {
let newObject = object;
newObject = fillMissingKeys(baseObject, newObject);
newObject = deleteExtraKeys(baseObject, newObject);
newObject = getSortedObject(newObject);
return newObject;
}

function getSortedObject(object) {
const sortedObject = {};

Expand All @@ -35,9 +43,24 @@ function getSortedObject(object) {

function fillMissingKeys(baseObject, object) {
Object.keys(baseObject).forEach(baseKey => {
if (!object[baseKey]) {
if (typeof object[baseKey] === 'undefined') {
object[baseKey] = '';
debug(`ADDED KEY: ${baseKey}`);
}
});
return object;
}

function deleteExtraKeys(baseObject, object) {
const baseKeys = Object.keys(baseObject) || [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.keys always returns array. Not sure about baseObject always being an object though.

const objectKeys = Object.keys(object) || [];

objectKeys.forEach(prop => {
if (!baseKeys.find(k => k === prop)) {
delete object[prop];
debug(`DELETED KEY: ${prop}`);
}
});

return object;
}