Skip to content

Commit c5a785a

Browse files
committed
revert: transform object with numeric keys (#466)
This reverts commit 9ef4da1.
1 parent ba58229 commit c5a785a

5 files changed

Lines changed: 7 additions & 194 deletions

File tree

.changeset/beige-lies-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
revert

packages/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"dev": "tsup --watch",
3838
"build": "tsc --noEmit && tsup",
3939
"test": "vitest run",
40-
"test:watch": "vitest",
4140
"clean": "rm -rf build"
4241
},
4342
"keywords": [],

packages/cli/src/cli/loaders/flat.spec.ts

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,24 @@
11
import { flatten, unflatten } from "flat";
22
import { ILoader } from "./_types";
33
import { createLoader } from "./_utils";
4-
import _ from "lodash";
5-
6-
export const OBJECT_NUMERIC_KEY_PREFIX = "__lingodotdev__obj__";
74

85
export default function createFlatLoader(): ILoader<Record<string, any>, Record<string, string>> {
9-
let denormalizedKeysMap: Record<string, string>;
10-
116
return createLoader({
127
pull: async (locale, input) => {
13-
const denormalized = denormalizeObjectKeys(input || {});
14-
const flattened: Record<string, string> = flatten(denormalized, {
8+
return flatten(input || {}, {
159
delimiter: "/",
1610
transformKey(key) {
1711
return encodeURIComponent(String(key));
1812
},
1913
});
20-
denormalizedKeysMap = buildDenormalizedKeysMap(flattened);
21-
const normalized = normalizeObjectKeys(flattened);
22-
return normalized;
2314
},
2415
push: async (locale, data) => {
25-
const denormalized = mapDeormalizedKeys(data, denormalizedKeysMap);
26-
const unflattened: Record<string, any> = unflatten(denormalized || {}, {
16+
return unflatten(data || {}, {
2717
delimiter: "/",
2818
transformKey(key) {
2919
return decodeURIComponent(String(key));
3020
},
3121
});
32-
const normalized = normalizeObjectKeys(unflattened);
33-
return normalized;
3422
},
3523
});
3624
}
37-
38-
export function buildDenormalizedKeysMap(obj: Record<string, string>) {
39-
return Object.keys(obj).reduce(
40-
(acc, key) => {
41-
const normalizedKey = `${key}`.replace(OBJECT_NUMERIC_KEY_PREFIX, "");
42-
acc[normalizedKey] = key;
43-
return acc;
44-
},
45-
{} as Record<string, string>,
46-
);
47-
}
48-
49-
export function mapDeormalizedKeys(obj: Record<string, any>, denormalizedKeysMap: Record<string, string>) {
50-
return Object.keys(obj).reduce(
51-
(acc, key) => {
52-
const denormalizedKey = denormalizedKeysMap[key];
53-
acc[denormalizedKey] = obj[key];
54-
return acc;
55-
},
56-
{} as Record<string, string>,
57-
);
58-
}
59-
60-
export function denormalizeObjectKeys(obj: Record<string, any>): Record<string, any> {
61-
if (_.isObject(obj) && !_.isArray(obj)) {
62-
return _.transform(
63-
obj,
64-
(result, value, key) => {
65-
const newKey = !isNaN(Number(key)) ? `${OBJECT_NUMERIC_KEY_PREFIX}${key}` : key;
66-
result[newKey] = _.isObject(value) ? denormalizeObjectKeys(value) : value;
67-
},
68-
{} as Record<string, any>,
69-
);
70-
} else {
71-
return obj;
72-
}
73-
}
74-
75-
export function normalizeObjectKeys(obj: Record<string, any>): Record<string, any> {
76-
if (_.isObject(obj) && !_.isArray(obj)) {
77-
return _.transform(
78-
obj,
79-
(result, value, key) => {
80-
const newKey = `${key}`.replace(OBJECT_NUMERIC_KEY_PREFIX, "");
81-
result[newKey] = _.isObject(value) ? normalizeObjectKeys(value) : value;
82-
},
83-
{} as Record<string, any>,
84-
);
85-
} else {
86-
return obj;
87-
}
88-
}

packages/cli/src/cli/loaders/index.spec.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,42 +332,6 @@ describe("bucket loaders", () => {
332332

333333
expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.json", expectedOutput, { encoding: "utf-8", flag: "w" });
334334
});
335-
336-
it("should save json data with numeric keys", async () => {
337-
setupFileMocks();
338-
339-
const input = { messages: { "1": "foo", "2": "bar", "3": "bar" } };
340-
const payload = { "messages/1": "foo", "messages/2": "bar", "messages/3": "bar" };
341-
const expectedOutput = JSON.stringify(input, null, 2);
342-
343-
mockFileOperations(JSON.stringify(input));
344-
345-
const jsonLoader = createBucketLoader("json", "i18n/[locale].json");
346-
jsonLoader.setDefaultLocale("en");
347-
await jsonLoader.pull("en");
348-
349-
await jsonLoader.push("es", payload);
350-
351-
expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.json", expectedOutput, { encoding: "utf-8", flag: "w" });
352-
});
353-
354-
it("should save json data with array", async () => {
355-
setupFileMocks();
356-
357-
const input = { messages: ["foo", "bar"] };
358-
const payload = { "messages/0": "foo", "messages/1": "bar" };
359-
const expectedOutput = `{\n "messages\": [\"foo\", \"bar\"]\n}`;
360-
361-
mockFileOperations(JSON.stringify(input));
362-
363-
const jsonLoader = createBucketLoader("json", "i18n/[locale].json");
364-
jsonLoader.setDefaultLocale("en");
365-
await jsonLoader.pull("en");
366-
367-
await jsonLoader.push("es", payload);
368-
369-
expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.json", expectedOutput, { encoding: "utf-8", flag: "w" });
370-
});
371335
});
372336

373337
describe("markdown bucket loader", () => {

0 commit comments

Comments
 (0)