* 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
)
| 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. |
no test coverage detected