@@ -96,6 +96,49 @@ export class StringModule {
96
96
}
97
97
}
98
98
99
+ /**
100
+ * Generates a string from the given characters.
101
+ *
102
+ * @param characters The characters to use for the string. Can be a string or an array of characters.
103
+ * If it is an array, then each element is treated as a single character even if it is a string with multiple characters.
104
+ * @param length The length of the string to generate. Defaults to `1`.
105
+ * @param length.min The minimum length of the string to generate.
106
+ * @param length.max The maximum length of the string to generate.
107
+ *
108
+ * @example
109
+ * faker.string.fromCharacters('abc') // 'c'
110
+ * faker.string.fromCharacters(['a', 'b', 'c']) // 'a'
111
+ * faker.string.fromCharacters('abc', 10) // 'cbbbacbacb'
112
+ * faker.string.fromCharacters('abc', { min: 5, max: 10 }) // 'abcaaaba'
113
+ *
114
+ * @since 8.0.0
115
+ */
116
+ fromCharacters (
117
+ characters : string | ReadonlyArray < string > ,
118
+ length : number | { min : number ; max : number } = 1
119
+ ) : string {
120
+ length = this . faker . helpers . rangeToNumber ( length ) ;
121
+ if ( length <= 0 ) {
122
+ return '' ;
123
+ }
124
+
125
+ if ( typeof characters === 'string' ) {
126
+ characters = characters . split ( '' ) ;
127
+ }
128
+
129
+ if ( characters . length === 0 ) {
130
+ throw new FakerError (
131
+ 'Unable to generate string: No characters to select from.'
132
+ ) ;
133
+ }
134
+
135
+ return this . faker . helpers
136
+ . multiple ( ( ) => this . faker . helpers . arrayElement ( characters as string [ ] ) , {
137
+ count : length ,
138
+ } )
139
+ . join ( '' ) ;
140
+ }
141
+
99
142
/**
100
143
* Generating a string consisting of letters in the English alphabet.
101
144
*
@@ -157,15 +200,7 @@ export class StringModule {
157
200
158
201
charsArray = charsArray . filter ( ( elem ) => ! exclude . includes ( elem ) ) ;
159
202
160
- if ( charsArray . length === 0 ) {
161
- throw new FakerError (
162
- 'Unable to generate string, because all possible characters are excluded.'
163
- ) ;
164
- }
165
-
166
- return Array . from ( { length } , ( ) =>
167
- this . faker . helpers . arrayElement ( charsArray )
168
- ) . join ( '' ) ;
203
+ return this . fromCharacters ( charsArray , length ) ;
169
204
}
170
205
171
206
/**
@@ -230,15 +265,7 @@ export class StringModule {
230
265
231
266
charsArray = charsArray . filter ( ( elem ) => ! exclude . includes ( elem ) ) ;
232
267
233
- if ( charsArray . length === 0 ) {
234
- throw new FakerError (
235
- 'Unable to generate string, because all possible characters are excluded.'
236
- ) ;
237
- }
238
-
239
- return Array . from ( { length } , ( ) =>
240
- this . faker . helpers . arrayElement ( charsArray )
241
- ) . join ( '' ) ;
268
+ return this . fromCharacters ( charsArray , length ) ;
242
269
}
243
270
244
271
/**
@@ -266,18 +293,10 @@ export class StringModule {
266
293
} = { }
267
294
) : string {
268
295
const { prefix = '0b' } = options ;
269
- const length = this . faker . helpers . rangeToNumber ( options . length ?? 1 ) ;
270
- if ( length <= 0 ) {
271
- return prefix ;
272
- }
273
-
274
- let binaryString = '' ;
275
296
276
- for ( let i = 0 ; i < length ; i ++ ) {
277
- binaryString += this . faker . helpers . arrayElement ( [ '0' , '1' ] ) ;
278
- }
279
-
280
- return `${ prefix } ${ binaryString } ` ;
297
+ let result = prefix ;
298
+ result += this . fromCharacters ( [ '0' , '1' ] , options . length ?? 1 ) ;
299
+ return result ;
281
300
}
282
301
283
302
/**
@@ -305,27 +324,13 @@ export class StringModule {
305
324
} = { }
306
325
) : string {
307
326
const { prefix = '0o' } = options ;
308
- const length = this . faker . helpers . rangeToNumber ( options . length ?? 1 ) ;
309
- if ( length <= 0 ) {
310
- return prefix ;
311
- }
312
327
313
- let octalString = '' ;
314
-
315
- for ( let i = 0 ; i < length ; i ++ ) {
316
- octalString += this . faker . helpers . arrayElement ( [
317
- '0' ,
318
- '1' ,
319
- '2' ,
320
- '3' ,
321
- '4' ,
322
- '5' ,
323
- '6' ,
324
- '7' ,
325
- ] ) ;
326
- }
327
-
328
- return `${ prefix } ${ octalString } ` ;
328
+ let result = prefix ;
329
+ result += this . fromCharacters (
330
+ [ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ] ,
331
+ options . length ?? 1
332
+ ) ;
333
+ return result ;
329
334
}
330
335
331
336
/**
@@ -362,10 +367,8 @@ export class StringModule {
362
367
return prefix ;
363
368
}
364
369
365
- let wholeString = '' ;
366
-
367
- for ( let i = 0 ; i < length ; i ++ ) {
368
- wholeString += this . faker . helpers . arrayElement ( [
370
+ let wholeString = this . fromCharacters (
371
+ [
369
372
'0' ,
370
373
'1' ,
371
374
'2' ,
@@ -388,8 +391,9 @@ export class StringModule {
388
391
'D' ,
389
392
'E' ,
390
393
'F' ,
391
- ] ) ;
392
- }
394
+ ] ,
395
+ length
396
+ ) ;
393
397
394
398
if ( casing === 'upper' ) {
395
399
wholeString = wholeString . toUpperCase ( ) ;
@@ -470,9 +474,7 @@ export class StringModule {
470
474
) ;
471
475
}
472
476
473
- while ( result . length < length ) {
474
- result += this . faker . helpers . arrayElement ( allowedDigits ) ;
475
- }
477
+ result += this . fromCharacters ( allowedDigits , length - result . length ) ;
476
478
477
479
return result ;
478
480
}
@@ -589,14 +591,8 @@ export class StringModule {
589
591
* @since 8.0.0
590
592
*/
591
593
special ( length : number | { min : number ; max : number } = 1 ) : string {
592
- length = this . faker . helpers . rangeToNumber ( length ) ;
593
- if ( length <= 0 ) {
594
- return '' ;
595
- }
596
-
597
- let specialString = '' ;
598
- for ( let i = 0 ; i < length ; i ++ ) {
599
- specialString += this . faker . helpers . arrayElement ( [
594
+ return this . fromCharacters (
595
+ [
600
596
'!' ,
601
597
'"' ,
602
598
'#' ,
@@ -629,9 +625,8 @@ export class StringModule {
629
625
'|' ,
630
626
'}' ,
631
627
'~' ,
632
- ] ) ;
633
- }
634
-
635
- return specialString ;
628
+ ] ,
629
+ length
630
+ ) ;
636
631
}
637
632
}
0 commit comments