|
| 1 | +import type { LocaleDefinition } from './definitions'; |
| 2 | +import { FakerError } from './errors/faker-error'; |
| 3 | + |
| 4 | +/** |
| 5 | + * A proxy for LocaleDefinitions that marks all properties as required and throws an error when an entry is accessed that is not defined. |
| 6 | + */ |
| 7 | +export type LocaleProxy = Readonly<{ |
| 8 | + [key in keyof LocaleDefinition]-?: Readonly< |
| 9 | + Required<NonNullable<LocaleDefinition[key]>> |
| 10 | + >; |
| 11 | +}>; |
| 12 | + |
| 13 | +const throwReadOnlyError: () => never = () => { |
| 14 | + throw new FakerError('You cannot edit the locale data on the faker instance'); |
| 15 | +}; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a proxy for LocaleDefinition that throws an error if an undefined property is accessed. |
| 19 | + * |
| 20 | + * @param locale The locale definition to create the proxy for. |
| 21 | + */ |
| 22 | +export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy { |
| 23 | + const proxies = {} as LocaleDefinition; |
| 24 | + return new Proxy(locale, { |
| 25 | + has(): true { |
| 26 | + // Categories are always present (proxied), that's why we return true. |
| 27 | + return true; |
| 28 | + }, |
| 29 | + |
| 30 | + get( |
| 31 | + target: LocaleDefinition, |
| 32 | + categoryName: keyof LocaleDefinition |
| 33 | + ): LocaleDefinition[keyof LocaleDefinition] { |
| 34 | + if (categoryName in proxies) { |
| 35 | + return proxies[categoryName]; |
| 36 | + } |
| 37 | + |
| 38 | + return (proxies[categoryName] = createCategoryProxy( |
| 39 | + categoryName, |
| 40 | + target[categoryName] |
| 41 | + )); |
| 42 | + }, |
| 43 | + |
| 44 | + set: throwReadOnlyError, |
| 45 | + deleteProperty: throwReadOnlyError, |
| 46 | + }) as LocaleProxy; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Creates a proxy for a category that throws an error when accessing an undefined property. |
| 51 | + * |
| 52 | + * @param categoryName The name of the category. |
| 53 | + * @param categoryData The module to create the proxy for. |
| 54 | + */ |
| 55 | +function createCategoryProxy< |
| 56 | + CategoryData extends Record<string | symbol, unknown> |
| 57 | +>( |
| 58 | + categoryName: string, |
| 59 | + categoryData: CategoryData = {} as CategoryData |
| 60 | +): Required<CategoryData> { |
| 61 | + return new Proxy(categoryData, { |
| 62 | + has(target: CategoryData, entryName: keyof CategoryData): boolean { |
| 63 | + const value = target[entryName]; |
| 64 | + return value != null; |
| 65 | + }, |
| 66 | + |
| 67 | + get( |
| 68 | + target: CategoryData, |
| 69 | + entryName: keyof CategoryData |
| 70 | + ): CategoryData[keyof CategoryData] { |
| 71 | + const value = target[entryName]; |
| 72 | + if (value === null) { |
| 73 | + throw new FakerError( |
| 74 | + `The locale data for '${categoryName}.${entryName.toString()}' aren't applicable to this locale. |
| 75 | + If you think this is a bug, please report it at: https://github.com/faker-js/faker` |
| 76 | + ); |
| 77 | + } else if (value === undefined) { |
| 78 | + throw new FakerError( |
| 79 | + `The locale data for '${categoryName}.${entryName.toString()}' are missing in this locale. |
| 80 | + Please contribute the missing data to the project or use a locale/Faker instance that has these data. |
| 81 | + For more information see https://next.fakerjs.dev/guide/localization.html` |
| 82 | + ); |
| 83 | + } else { |
| 84 | + return value; |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | + set: throwReadOnlyError, |
| 89 | + deleteProperty: throwReadOnlyError, |
| 90 | + }) as Required<CategoryData>; |
| 91 | +} |
0 commit comments