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

Method fromCharacters

src/modules/string/index.ts:118–153  ·  view source on GitHub ↗

* Generates a string from the given characters. * * @param characters The characters to use for the string. Can be a string or an array of characters. * If it is an array, then each element is treated as a single character even if it is a string with multiple characters. * @param length

(
    characters: string | ReadonlyArray<string>,
    length:
      | number
      | {
          /**
           * The minimum length of the string to generate.
           */
          min: number;
          /**
           * The maximum length of the string to generate.
           */
          max: number;
        } = 1
  )

Source from the content-addressed store, hash-verified

116 * @since 8.0.0
117 */
118 fromCharacters(
119 characters: string | ReadonlyArray<string>,
120 length:
121 | number
122 | {
123 /**
124 * The minimum length of the string to generate.
125 */
126 min: number;
127 /**
128 * The maximum length of the string to generate.
129 */
130 max: number;
131 } = 1
132 ): string {
133 length = this.faker.helpers.rangeToNumber(length);
134 if (length <= 0) {
135 return '';
136 }
137
138 if (typeof characters === 'string') {
139 characters = [...characters];
140 }
141
142 if (characters.length === 0) {
143 throw new FakerError(
144 'Unable to generate string: No characters to select from.'
145 );
146 }
147
148 return this.faker.helpers
149 .multiple(() => this.faker.helpers.arrayElement(characters as string[]), {
150 count: length,
151 })
152 .join('');
153 }
154
155 /**
156 * Generating a string consisting of letters in the English alphabet.

Callers 12

alphaMethod · 0.95
alphanumericMethod · 0.95
binaryMethod · 0.95
octalMethod · 0.95
hexadecimalMethod · 0.95
numericMethod · 0.95
ulidMethod · 0.95
symbolMethod · 0.95
string.spec.tsFile · 0.80
fromRegExpMethod · 0.80
litecoinAddressMethod · 0.80

Calls 3

rangeToNumberMethod · 0.80
multipleMethod · 0.80
arrayElementMethod · 0.80

Tested by

no test coverage detected