Skip to content

Commit f476ad6

Browse files
committed
refactor: use enum for LengthStrategy
1 parent f506c6d commit f476ad6

5 files changed

Lines changed: 42 additions & 28 deletions

File tree

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@ export {
9292
generateMersenne32Randomizer,
9393
generateMersenne53Randomizer,
9494
} from './utils/mersenne';
95-
export type { Casing, LengthStrategy, NumberRange } from './utils/types';
95+
export { LengthStrategy } from './utils/types';
96+
export type { Casing, LengthStrategyType, NumberRange } from './utils/types';

src/modules/lorem/module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ModuleBase } from '../../internal/module-base';
2-
import type { LengthStrategy, NumberRange } from '../../utils/types';
2+
import type { LengthStrategyType, NumberRange } from '../../utils/types';
33
import { filterWordListByLength } from '../word/filter-word-list-by-length';
44

55
/**
@@ -46,7 +46,7 @@ export class LoremModule extends ModuleBase {
4646
*
4747
* @default 'any-length'
4848
*/
49-
strategy?: LengthStrategy;
49+
strategy?: LengthStrategyType;
5050
} = {}
5151
): string {
5252
if (typeof options === 'number') {

src/modules/word/filter-word-list-by-length.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FakerError } from '../../errors/faker-error';
22
import { groupBy } from '../../internal/group-by';
3-
import type { LengthStrategy, NumberRange } from '../../utils/types';
3+
import type { LengthStrategyType, NumberRange } from '../../utils/types';
44

55
/**
66
* The error handling strategies for the `filterWordListByLength` function.
@@ -54,7 +54,7 @@ const STRATEGIES = {
5454
export function filterWordListByLength(options: {
5555
wordList: ReadonlyArray<string>;
5656
length?: number | NumberRange;
57-
strategy?: LengthStrategy;
57+
strategy?: LengthStrategyType;
5858
}): string[] {
5959
const { wordList, length, strategy = 'fail' } = options;
6060

src/modules/word/module.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FakerError } from '../../errors/faker-error';
22
import { ModuleBase } from '../../internal/module-base';
3-
import type { LengthStrategy, NumberRange } from '../../utils/types';
3+
import type { LengthStrategyType, NumberRange } from '../../utils/types';
44
import { filterWordListByLength } from './filter-word-list-by-length';
55

66
/**
@@ -37,7 +37,7 @@ export class WordModule extends ModuleBase {
3737
*
3838
* @default 'fail'
3939
*/
40-
strategy?: LengthStrategy;
40+
strategy?: LengthStrategyType;
4141
} = {}
4242
): string {
4343
if (typeof options === 'number') {
@@ -82,7 +82,7 @@ export class WordModule extends ModuleBase {
8282
*
8383
* @default 'fail'
8484
*/
85-
strategy?: LengthStrategy;
85+
strategy?: LengthStrategyType;
8686
} = {}
8787
): string {
8888
if (typeof options === 'number') {
@@ -127,7 +127,7 @@ export class WordModule extends ModuleBase {
127127
*
128128
* @default 'fail'
129129
*/
130-
strategy?: LengthStrategy;
130+
strategy?: LengthStrategyType;
131131
} = {}
132132
): string {
133133
if (typeof options === 'number') {
@@ -172,7 +172,7 @@ export class WordModule extends ModuleBase {
172172
*
173173
* @default 'fail'
174174
*/
175-
strategy?: LengthStrategy;
175+
strategy?: LengthStrategyType;
176176
} = {}
177177
): string {
178178
if (typeof options === 'number') {
@@ -217,7 +217,7 @@ export class WordModule extends ModuleBase {
217217
*
218218
* @default 'fail'
219219
*/
220-
strategy?: LengthStrategy;
220+
strategy?: LengthStrategyType;
221221
} = {}
222222
): string {
223223
if (typeof options === 'number') {
@@ -262,7 +262,7 @@ export class WordModule extends ModuleBase {
262262
*
263263
* @default 'fail'
264264
*/
265-
strategy?: LengthStrategy;
265+
strategy?: LengthStrategyType;
266266
} = {}
267267
): string {
268268
if (typeof options === 'number') {
@@ -307,7 +307,7 @@ export class WordModule extends ModuleBase {
307307
*
308308
* @default 'fail'
309309
*/
310-
strategy?: LengthStrategy;
310+
strategy?: LengthStrategyType;
311311
} = {}
312312
): string {
313313
if (typeof options === 'number') {
@@ -350,7 +350,7 @@ export class WordModule extends ModuleBase {
350350
*
351351
* @default 'fail'
352352
*/
353-
strategy?: LengthStrategy;
353+
strategy?: LengthStrategyType;
354354
} = {}
355355
): string {
356356
const wordMethods = this.faker.helpers.shuffle([

src/utils/types.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,31 @@ export interface NumberRange {
2323

2424
/**
2525
* The strategy to apply when no words with a matching length are found.
26-
*
27-
* Available error handling strategies:
28-
*
29-
* - `fail`: Throws an error if no words with the given length are found.
30-
* - `shortest`: Returns any of the shortest words.
31-
* - `closest`: Returns any of the words closest to the given length.
32-
* - `longest`: Returns any of the longest words.
33-
* - `any-length`: Returns a word with any length.
3426
*/
35-
export type LengthStrategy =
36-
| 'fail'
37-
| 'closest'
38-
| 'shortest'
39-
| 'longest'
40-
| 'any-length';
27+
export enum LengthStrategy {
28+
/**
29+
* Throws an error if no words with the given length are found.
30+
*/
31+
Fail = 'fail',
32+
/**
33+
* Returns any of the words closest to the given length.
34+
*/
35+
Closest = 'closest',
36+
/**
37+
* Returns any of the shortest words.
38+
*/
39+
Shortest = 'shortest',
40+
/**
41+
* Returns any of the longest words.
42+
*/
43+
Longest = 'longest',
44+
/**
45+
* Returns a word with any length.
46+
*/
47+
AnyLength = 'any-length',
48+
}
49+
50+
/**
51+
* The strategy to apply when no words with a matching length are found.
52+
*/
53+
export type LengthStrategyType = `${LengthStrategy}`;

0 commit comments

Comments
 (0)