Skip to content

Commit 42a8257

Browse files
committed
Sort translated strings keys by original input
1 parent a99a94d commit 42a8257

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/structure/kv.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { addedDiff, deletedDiff } from 'deep-object-diff';
55
type KVStructure = Record<string, string>;
66

77
export default class implements Structure<KVStructure> {
8+
private keys: string[] = [];
9+
810
split(data: KVStructure, enc: Tiktoken, chunkSize: number): KVStructure[] {
911
let left: KVStructure = {};
1012
const result: KVStructure[] = [];
@@ -48,13 +50,8 @@ export default class implements Structure<KVStructure> {
4850
dst = {};
4951
}
5052

51-
const patch: KVStructure = {};
52-
const addedDiffs = addedDiff(dst, src) as KVStructure;
53-
54-
for (const [key, value] of Object.entries(addedDiffs)) {
55-
patch[key] = value;
56-
}
57-
53+
this.keys = Object.keys(src);
54+
const patch = addedDiff(dst, src) as KVStructure;
5855
const deletedDiffs = deletedDiff(dst, src) as KVStructure;
5956

6057
for (const key of Object.keys(deletedDiffs)) {
@@ -65,7 +62,13 @@ export default class implements Structure<KVStructure> {
6562
}
6663

6764
merge(src: KVStructure, patch: KVStructure): KVStructure {
65+
const result: KVStructure = {};
6866
Object.assign(src, patch);
69-
return src;
67+
68+
for (const key of this.keys) {
69+
result[key] = src[key];
70+
}
71+
72+
return result;
7073
}
7174
}

src/structure/tree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export default class implements Structure<TreeStructure> {
99
private kv = new kv();
1010

1111
split(data: TreeStructure, enc: Tiktoken, chunkSize: number): TreeStructure[] {
12-
return this.kv.split(flatten(data), enc, chunkSize);
12+
return this.kv.split(data, enc, chunkSize);
1313
}
1414

1515
join(chunks: TreeStructure[]): TreeStructure {
1616
return this.kv.join(chunks);
1717
}
1818

1919
diff(src: TreeStructure, dst: TreeStructure | null): [TreeStructure, TreeStructure] {
20-
return this.kv.diff(src, dst);
20+
return this.kv.diff(flatten(src), flatten(dst || {}));
2121
}
2222

2323
merge(src: TreeStructure, patch: TreeStructure): TreeStructure {

0 commit comments

Comments
 (0)