MCPcopy
hub / github.com/faker-js/faker / alphanumeric

Method alphanumeric

src/modules/string/index.ts:267–342  ·  view source on GitHub ↗

* Generating a string consisting of alpha characters and digits. * * @param options Either the length of the string to generate or the optional options object. * @param options.length The length of the string to generate either as a fixed length or as a length range. Defaults to `1`. * @

(
    options:
      | number
      | {
          /**
           * The length of the string to generate either as a fixed length or as a length range.
           *
           * @default 1
           */
          length?:
            | number
            | {
                /**
                 * The minimum length of the string to generate.
                 */
                min: number;
                /**
                 * The maximum length of the string to generate.
                 */
                max: number;
              };
          /**
           * The casing of the characters.
           *
           * @default 'mixed'
           */
          casing?: Casing;
          /**
           * An array of characters and digits which should be excluded in the generated string.
           *
           * @default []
           */
          exclude?: ReadonlyArray<LiteralUnion<AlphaNumericChar>> | string;
        } = {}
  )

Source from the content-addressed store, hash-verified

265 * @since 8.0.0
266 */
267 alphanumeric(
268 options:
269 | number
270 | {
271 /**
272 * The length of the string to generate either as a fixed length or as a length range.
273 *
274 * @default 1
275 */
276 length?:
277 | number
278 | {
279 /**
280 * The minimum length of the string to generate.
281 */
282 min: number;
283 /**
284 * The maximum length of the string to generate.
285 */
286 max: number;
287 };
288 /**
289 * The casing of the characters.
290 *
291 * @default 'mixed'
292 */
293 casing?: Casing;
294 /**
295 * An array of characters and digits which should be excluded in the generated string.
296 *
297 * @default []
298 */
299 exclude?: ReadonlyArray<LiteralUnion<AlphaNumericChar>> | string;
300 } = {}
301 ): string {
302 if (typeof options === 'number') {
303 options = {
304 length: options,
305 };
306 }
307
308 const length = this.faker.helpers.rangeToNumber(options.length ?? 1);
309 if (length <= 0) {
310 return '';
311 }
312
313 const { casing = 'mixed' } = options;
314 let { exclude = [] } = options;
315
316 if (typeof exclude === 'string') {
317 exclude = [...exclude];
318 }
319
320 let charsArray = [...DIGIT_CHARS];
321
322 switch (casing) {
323 case 'upper': {
324 charsArray.push(...UPPER_CHARS);

Callers 13

nanoidMethod · 0.95
string.spec.tsFile · 0.80
internet.spec.tsFile · 0.80
helpers.spec.tsFile · 0.80
base64.spec.tsFile · 0.80
urlPicsumPhotosMethod · 0.80
fromRegExpMethod · 0.80
bitcoinAddressMethod · 0.80
bicMethod · 0.80
recordLocatorMethod · 0.80
vinMethod · 0.80

Calls 2

fromCharactersMethod · 0.95
rangeToNumberMethod · 0.80

Tested by

no test coverage detected