Skip to content

Commit

Permalink
fix(types): locale proxy (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Apr 28, 2023
1 parent 8395e69 commit 971f363
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/locale-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import { FakerError } from './errors/faker-error';
* A proxy for LocaleDefinitions that marks all properties as required and throws an error when an entry is accessed that is not defined.
*/
export type LocaleProxy = Readonly<{
[key in keyof LocaleDefinition]-?: Readonly<
Required<NonNullable<LocaleDefinition[key]>>
>;
[key in keyof LocaleDefinition]-?: LocaleProxyCategory<LocaleDefinition[key]>;
}>;

type LocaleProxyCategory<T> = Readonly<{
[key in keyof T]-?: LocaleProxyEntry<T[key]>;
}>;

type LocaleProxyEntry<T> = unknown extends T ? T : Readonly<NonNullable<T>>;

const throwReadOnlyError: () => never = () => {
throw new FakerError('You cannot edit the locale data on the faker instance');
};
Expand Down
11 changes: 5 additions & 6 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ export class HelpersModule {
*
* @since 8.0.0
*/
fake(patterns: string[]): string;
fake(patterns: ReadonlyArray<string>): string;
/**
* Generator for combining faker methods based on a static string input or an array of static string inputs.
*
Expand Down Expand Up @@ -1135,11 +1135,10 @@ export class HelpersModule {
*
* @since 7.4.0
*/
fake(pattern: string | string[]): string;
fake(pattern: string | string[]): string {
if (Array.isArray(pattern)) {
pattern = this.arrayElement(pattern);
}
fake(pattern: string | ReadonlyArray<string>): string;
fake(pattern: string | ReadonlyArray<string>): string {
pattern =
typeof pattern === 'string' ? pattern : this.arrayElement(pattern);

// find first matching {{ and }}
const start = pattern.search(/{{[a-z]/);
Expand Down
10 changes: 5 additions & 5 deletions src/modules/word/filterWordListByLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const STRATEGIES = {
throw new FakerError('No words found that match the given length.');
},
closest: (
wordList: string[],
wordList: ReadonlyArray<string>,
length: { min: number; max: number }
): string[] => {
const wordsByLength = wordList.reduce((data, word) => {
Expand All @@ -30,15 +30,15 @@ const STRATEGIES = {
word.length === length.max + closestOffset
);
},
shortest: (wordList: string[]): string[] => {
shortest: (wordList: ReadonlyArray<string>): string[] => {
const minLength = Math.min(...wordList.map((word) => word.length));
return wordList.filter((word) => word.length === minLength);
},
longest: (wordList: string[]): string[] => {
longest: (wordList: ReadonlyArray<string>): string[] => {
const maxLength = Math.max(...wordList.map((word) => word.length));
return wordList.filter((word) => word.length === maxLength);
},
'any-length': (wordList: string[]): string[] => {
'any-length': (wordList: ReadonlyArray<string>): string[] => {
return [...wordList];
},
} as const; /*
Expand Down Expand Up @@ -67,7 +67,7 @@ string, // Parameters<filterWordListByLength>[0]['strategy']
* - `any-length`: Returns a copy of the original word list.
*/
export function filterWordListByLength(options: {
wordList: string[];
wordList: ReadonlyArray<string>;
length?: number | { min: number; max: number };
strategy?: 'fail' | 'closest' | 'shortest' | 'longest' | 'any-length';
}): string[] {
Expand Down

0 comments on commit 971f363

Please sign in to comment.