Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1x 1x 18544x 18544x 78966x 78966x 18428x 60538x 18544x | import _ from "lodash";
export interface NestedTranslations {
[key: string]: string | NestedTranslations;
}
export class JsonService {
/**
* Flattens a nested translations object into a flat key-value structure
* @param obj The nested translations object to flatten
* @param prefix Optional prefix for nested keys
* @returns A flattened object with dot-notation keys
*/
flatten(obj: NestedTranslations, prefix = ""): Record<string, string> {
const result: Record<string, string> = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === "object" && value !== null) {
Object.assign(result, this.flatten(value as NestedTranslations, newKey));
} else {
result[newKey] = value === "" ? "" : (value as string);
}
}
return result;
}
/**
* Sets a value at a specific path in the translations object
* @param obj The translations object to modify
* @param key The dot-notation path where to set the value
* @param value The translation string to set
* @returns The modified translations object
*/
set(obj: NestedTranslations, key: string, value: string): NestedTranslations {
_.set(obj, key, value);
return obj;
}
/**
* Removes a value at a specific path in the translations object
* @param obj The translations object to modify
* @param key The dot-notation path to remove
* @returns The modified translations object
*/
unset(obj: NestedTranslations, key: string): NestedTranslations {
_.unset(obj, key);
return obj;
}
}
|