|
1 | 1 | import { flatten, unflatten } from "flat"; |
2 | 2 | import { ILoader } from "./_types"; |
3 | 3 | import { createLoader } from "./_utils"; |
4 | | -import _ from "lodash"; |
5 | | - |
6 | | -export const OBJECT_NUMERIC_KEY_PREFIX = "__lingodotdev__obj__"; |
7 | 4 |
|
8 | 5 | export default function createFlatLoader(): ILoader<Record<string, any>, Record<string, string>> { |
9 | | - let denormalizedKeysMap: Record<string, string>; |
10 | | - |
11 | 6 | return createLoader({ |
12 | 7 | pull: async (locale, input) => { |
13 | | - const denormalized = denormalizeObjectKeys(input || {}); |
14 | | - const flattened: Record<string, string> = flatten(denormalized, { |
| 8 | + return flatten(input || {}, { |
15 | 9 | delimiter: "/", |
16 | 10 | transformKey(key) { |
17 | 11 | return encodeURIComponent(String(key)); |
18 | 12 | }, |
19 | 13 | }); |
20 | | - denormalizedKeysMap = buildDenormalizedKeysMap(flattened); |
21 | | - const normalized = normalizeObjectKeys(flattened); |
22 | | - return normalized; |
23 | 14 | }, |
24 | 15 | push: async (locale, data) => { |
25 | | - const denormalized = mapDeormalizedKeys(data, denormalizedKeysMap); |
26 | | - const unflattened: Record<string, any> = unflatten(denormalized || {}, { |
| 16 | + return unflatten(data || {}, { |
27 | 17 | delimiter: "/", |
28 | 18 | transformKey(key) { |
29 | 19 | return decodeURIComponent(String(key)); |
30 | 20 | }, |
31 | 21 | }); |
32 | | - const normalized = normalizeObjectKeys(unflattened); |
33 | | - return normalized; |
34 | 22 | }, |
35 | 23 | }); |
36 | 24 | } |
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 | | -} |
0 commit comments