All files / src flat-locales.ts

100% Statements 10/10
100% Branches 4/4
100% Functions 1/1
100% Lines 10/10

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              2x 37x 44x 35x 44x 9x 9x 44x 37x 37x  
import { type Locale } from "./interface";
 
export type FlatLocale = Record<string, string>;
 
/**
 * Turns a nested locale object into a flat locale object.
 */
export const flattenLocale = (locale: Locale, flatLocale: FlatLocale = {}, prefix = ""): FlatLocale => {
  for (const [key, value] of Object.entries(locale)) {
    if (typeof value === "string") {
      flatLocale[prefix + key] = value;
    } else {
      flattenLocale(value, flatLocale, prefix + key + ".");
    }
  }
  return flatLocale;
};