Skip to content

Commit

Permalink
Fix: Prevent Array Duplication in Repeated Translations (#80)
Browse files Browse the repository at this point in the history
* Fix: Array Duplication in Repeated Translations

* Fix not updating for --

* Remove console logs
  • Loading branch information
fadkeabhi authored Oct 26, 2024
1 parent dee28ad commit 12d0a68
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/core/json_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export async function objectTranslator(
);

// Insert old translations that we removed into the generalObject
generalObject[indexAsNum] = mergeKeys(newTranslations, oldTranslations[to[indexAsNum]])
generalObject[indexAsNum] = mergeKeys(oldTranslations[to[indexAsNum]], newTranslations)

})
);

Expand Down
16 changes: 8 additions & 8 deletions src/utils/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ export const messages = {

export function removeKeys(fromDict: any, toDict: any): any {
if (Array.isArray(fromDict) && Array.isArray(toDict)) {
return fromDict.map((item, index) => {
if (index < toDict.length && typeof item === 'object' && item !== null) {
return removeKeys(item, toDict[index]);
}
return item;
});
if (fromDict.length === toDict.length) {
return null;
}
return fromDict;
}

if (typeof fromDict !== 'object' || fromDict === null) {
Expand Down Expand Up @@ -143,7 +141,7 @@ export function mergeKeys(base: any, insert: any): any {

// Handle arrays
if (Array.isArray(base) && Array.isArray(insert)) {
return [...base, ...insert.filter(item => !base.includes(item))];
return insert;
}

// Handle objects
Expand All @@ -157,8 +155,10 @@ export function mergeKeys(base: any, insert: any): any {
} else if (!(key in result)) {
// Add new key-value pair from insert if it doesn't exist in base
result[key] = insert[key];
} else if (key in result && typeof result[key] === 'string' && typeof insert[key] === 'string') {
// If key value is string take insert value
result[key] = insert[key];
}
// If the key exists in both and is not an object, keep the base value
}
}

Expand Down

0 comments on commit 12d0a68

Please sign in to comment.