1
1
import type { Faker } from '../..' ;
2
2
import { FakerError } from '../../errors/faker-error' ;
3
+ import { deprecated } from '../../internal/deprecated' ;
4
+
5
+ export type Casing = 'upper' | 'lower' | 'mixed' ;
6
+
7
+ const UPPER_CHARS : readonly string [ ] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . split ( '' ) ;
8
+ const LOWER_CHARS : readonly string [ ] = 'abcdefghijklmnopqrstuvwxyz' . split ( '' ) ;
9
+ const DIGIT_CHARS : readonly string [ ] = '0123456789' . split ( '' ) ;
3
10
4
11
/**
5
12
* Method to reduce array of characters.
@@ -141,25 +148,30 @@ export class Random {
141
148
}
142
149
143
150
/**
144
- * Generating a string consisting of lower/upper alpha characters based on count and upcase options .
151
+ * Generating a string consisting of alpha characters.
145
152
*
146
- * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, upcase: false , bannedChars: [] }`.
153
+ * @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'lower' , bannedChars: [] }`.
147
154
* @param options.count The number of characters to generate. Defaults to `1`.
148
- * @param options.upcase If true, the result will be uppercase. If false, it will be lowercase. Defaults to `false`.
155
+ * @param options.casing The casing of the characters. Defaults to `'lower'`.
156
+ * @param options.upcase Deprecated, use `casing: 'upper'` instead.
149
157
* @param options.bannedChars An array with characters to exclude. Defaults to `[]`.
150
158
*
151
159
* @example
152
160
* faker.random.alpha() // 'b'
153
161
* faker.random.alpha(10) // 'qccrabobaf'
154
- * faker.random.alpha({ count: 5, upcase: true , bannedChars: ['a '] }) // 'DTCIC'
162
+ * faker.random.alpha({ count: 5, casing: 'upper' , bannedChars: ['A '] }) // 'DTCIC'
155
163
*/
156
164
// TODO @Shinigami 92 2022-02-14: Tests covered `(count, options)`, but they were never typed like that
157
165
alpha (
158
166
options :
159
167
| number
160
168
| {
161
169
count ?: number ;
170
+ /**
171
+ * @deprecated Use `casing` instead.
172
+ */
162
173
upcase ?: boolean ;
174
+ casing ?: Casing ;
163
175
bannedChars ?: readonly string [ ] ;
164
176
} = { }
165
177
) : string {
@@ -168,52 +180,59 @@ export class Random {
168
180
count : options ,
169
181
} ;
170
182
}
171
- const { count = 1 , upcase = false , bannedChars = [ ] } = options ;
172
-
173
- let charsArray = [
174
- 'a' ,
175
- 'b' ,
176
- 'c' ,
177
- 'd' ,
178
- 'e' ,
179
- 'f' ,
180
- 'g' ,
181
- 'h' ,
182
- 'i' ,
183
- 'j' ,
184
- 'k' ,
185
- 'l' ,
186
- 'm' ,
187
- 'n' ,
188
- 'o' ,
189
- 'p' ,
190
- 'q' ,
191
- 'r' ,
192
- 's' ,
193
- 't' ,
194
- 'u' ,
195
- 'v' ,
196
- 'w' ,
197
- 'x' ,
198
- 'y' ,
199
- 'z' ,
200
- ] ;
183
+ const { count = 1 , upcase, bannedChars = [ ] } = options ;
184
+
185
+ if ( count <= 0 ) {
186
+ return '' ;
187
+ }
188
+
189
+ const {
190
+ // Switch to 'mixed' with v8.0
191
+ casing = upcase ? 'upper' : 'lower' ,
192
+ } = options ;
193
+
194
+ if ( upcase != null ) {
195
+ deprecated ( {
196
+ deprecated : 'faker.random.alpha({ upcase: true })' ,
197
+ proposed : "faker.random.alpha({ casing: 'upper' })" ,
198
+ since : 'v7.0' ,
199
+ until : 'v8.0' ,
200
+ } ) ;
201
+ }
202
+
203
+ let charsArray : string [ ] ;
204
+ switch ( casing ) {
205
+ case 'upper' :
206
+ charsArray = [ ...UPPER_CHARS ] ;
207
+ break ;
208
+ case 'lower' :
209
+ charsArray = [ ...LOWER_CHARS ] ;
210
+ break ;
211
+ case 'mixed' :
212
+ default :
213
+ charsArray = [ ...LOWER_CHARS , ...UPPER_CHARS ] ;
214
+ break ;
215
+ }
201
216
202
217
charsArray = arrayRemove ( charsArray , bannedChars ) ;
203
218
204
- let wholeString = '' ;
205
- for ( let i = 0 ; i < count ; i ++ ) {
206
- wholeString += this . faker . helpers . arrayElement ( charsArray ) ;
219
+ if ( charsArray . length === 0 ) {
220
+ throw new FakerError (
221
+ 'Unable to generate string, because all possible characters are banned.'
222
+ ) ;
207
223
}
208
224
209
- return upcase ? wholeString . toUpperCase ( ) : wholeString ;
225
+ return Array . from ( { length : count } , ( ) =>
226
+ this . faker . helpers . arrayElement ( charsArray )
227
+ ) . join ( '' ) ;
210
228
}
211
229
212
230
/**
213
- * Generating a string consisting of lower/upper alpha characters and digits based on count and upcase options .
231
+ * Generating a string consisting of alpha characters and digits.
214
232
*
215
233
* @param count The number of characters and digits to generate. Defaults to `1`.
216
234
* @param options The options to use. Defaults to `{ bannedChars: [] }`.
235
+ * @param options.casing The casing of the characters. Defaults to `'lower'`.
217
236
* @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`.
218
237
*
219
238
* @example
@@ -223,48 +242,35 @@ export class Random {
223
242
*/
224
243
alphaNumeric (
225
244
count : number = 1 ,
226
- options : { bannedChars ?: readonly string [ ] } = { }
245
+ options : {
246
+ casing ?: Casing ;
247
+ bannedChars ?: readonly string [ ] ;
248
+ } = { }
227
249
) : string {
228
- const { bannedChars = [ ] } = options ;
229
-
230
- let charsArray = [
231
- '0' ,
232
- '1' ,
233
- '2' ,
234
- '3' ,
235
- '4' ,
236
- '5' ,
237
- '6' ,
238
- '7' ,
239
- '8' ,
240
- '9' ,
241
- 'a' ,
242
- 'b' ,
243
- 'c' ,
244
- 'd' ,
245
- 'e' ,
246
- 'f' ,
247
- 'g' ,
248
- 'h' ,
249
- 'i' ,
250
- 'j' ,
251
- 'k' ,
252
- 'l' ,
253
- 'm' ,
254
- 'n' ,
255
- 'o' ,
256
- 'p' ,
257
- 'q' ,
258
- 'r' ,
259
- 's' ,
260
- 't' ,
261
- 'u' ,
262
- 'v' ,
263
- 'w' ,
264
- 'x' ,
265
- 'y' ,
266
- 'z' ,
267
- ] ;
250
+ if ( count <= 0 ) {
251
+ return '' ;
252
+ }
253
+
254
+ const {
255
+ // Switch to 'mixed' with v8.0
256
+ casing = 'lower' ,
257
+ bannedChars = [ ] ,
258
+ } = options ;
259
+
260
+ let charsArray = [ ...DIGIT_CHARS ] ;
261
+
262
+ switch ( casing ) {
263
+ case 'upper' :
264
+ charsArray . push ( ...UPPER_CHARS ) ;
265
+ break ;
266
+ case 'lower' :
267
+ charsArray . push ( ...LOWER_CHARS ) ;
268
+ break ;
269
+ case 'mixed' :
270
+ default :
271
+ charsArray . push ( ...LOWER_CHARS , ...UPPER_CHARS ) ;
272
+ break ;
273
+ }
268
274
269
275
charsArray = arrayRemove ( charsArray , bannedChars ) ;
270
276
@@ -274,12 +280,9 @@ export class Random {
274
280
) ;
275
281
}
276
282
277
- let wholeString = '' ;
278
- for ( let i = 0 ; i < count ; i ++ ) {
279
- wholeString += this . faker . helpers . arrayElement ( charsArray ) ;
280
- }
281
-
282
- return wholeString ;
283
+ return Array . from ( { length : count } , ( ) =>
284
+ this . faker . helpers . arrayElement ( charsArray )
285
+ ) . join ( '' ) ;
283
286
}
284
287
285
288
/**
@@ -310,9 +313,9 @@ export class Random {
310
313
311
314
const { allowLeadingZeros = false , bannedDigits = [ ] } = options ;
312
315
313
- const allowedDigits = '0123456789'
314
- . split ( '' )
315
- . filter ( ( digit ) => ! bannedDigits . includes ( digit ) ) ;
316
+ const allowedDigits = DIGIT_CHARS . filter (
317
+ ( digit ) => ! bannedDigits . includes ( digit )
318
+ ) ;
316
319
317
320
if (
318
321
allowedDigits . length === 0 ||
0 commit comments