diff options
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/helpers/index.ts | 20 | ||||
| -rw-r--r-- | src/modules/lorem/index.ts | 117 | ||||
| -rw-r--r-- | src/modules/string/index.ts | 56 | ||||
| -rw-r--r-- | src/modules/system/index.ts | 11 | ||||
| -rw-r--r-- | src/modules/word/index.ts | 16 |
5 files changed, 148 insertions, 72 deletions
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 7da4fbc8..406f7f19 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -628,6 +628,26 @@ export class HelpersModule { } /** + * Helper method that converts the given number or range to a number. + * + * @param numberOrRange The number or range to convert. + * @param numberOrRange.min The minimum value for the range. + * @param numberOrRange.max The maximum value for the range. + * + * @example + * faker.helpers.rangeToNumber(1) // 1 + * faker.helpers.rangeToNumber({ min: 1, max: 10 }) // 5 + * + * @since 8.0.0 + */ + rangeToNumber(numberOrRange: number | { min: number; max: number }): number { + if (typeof numberOrRange === 'number') { + return numberOrRange; + } + return this.faker.datatype.number(numberOrRange); + } + + /** * Generates a unique result using the results of the given method. * Used unique entries will be stored internally and filtered from subsequent calls. * diff --git a/src/modules/lorem/index.ts b/src/modules/lorem/index.ts index ac1ba756..c73a92c8 100644 --- a/src/modules/lorem/index.ts +++ b/src/modules/lorem/index.ts @@ -36,7 +36,7 @@ export class LoremModule { * faker.lorem.word() // 'temporibus' * faker.lorem.word(5) // 'velit' * faker.lorem.word({ strategy: 'shortest' }) // 'a' - * faker.lorem.word({ length: { min: 5, max: 7 }, strategy: "fail" }) // 'quaerat' + * faker.lorem.word({ length: { min: 5, max: 7 }, strategy: 'fail' }) // 'quaerat' * * @since 3.1.0 */ @@ -60,55 +60,62 @@ export class LoremModule { /** * Generates a space separated list of words. * - * @param num The number of words to generate. Defaults to `3`. + * @param wordCount The number of words to generate. Defaults to `3`. + * @param wordCount.min The minimum number of words to generate. + * @param wordCount.max The maximum number of words to generate. * * @example * faker.lorem.words() // 'qui praesentium pariatur' * faker.lorem.words(10) // 'debitis consectetur voluptatem non doloremque ipsum autem totam eum ratione' + * faker.lorem.words({ min: 1, max: 3 }) // 'tenetur error cum' * * @since 2.0.1 */ - words(num: number = 3): string { - const words: string[] = []; - for (let i = 0; i < num; i++) { - words.push(this.word()); - } - return words.join(' '); + words(wordCount: number | { min: number; max: number } = 3): string { + wordCount = this.faker.helpers.rangeToNumber(wordCount); + + return Array.from({ length: wordCount }) + .map(() => this.word()) + .join(' '); } /** - * Generates a space separated list of words beginning a capital letter and ending with a dot. + * Generates a space separated list of words beginning with a capital letter and ending with a period. * * @param wordCount The number of words, that should be in the sentence. Defaults to a random number between `3` and `10`. + * @param wordCount.min The minimum number of words to generate. Defaults to `3`. + * @param wordCount.max The maximum number of words to generate. Defaults to `10`. * * @example * faker.lorem.sentence() // 'Voluptatum cupiditate suscipit autem eveniet aut dolorem aut officiis distinctio.' * faker.lorem.sentence(5) // 'Laborum voluptatem officiis est et.' + * faker.lorem.sentence({ min: 3, max: 5 }) // 'Fugiat repellendus nisi.' * * @since 2.0.1 */ - sentence(wordCount?: number): string { - if (wordCount == null) { - wordCount = this.faker.datatype.number({ min: 3, max: 10 }); - } - + sentence( + wordCount: number | { min: number; max: number } = { min: 3, max: 10 } + ): string { const sentence = this.words(wordCount); - return `${sentence.charAt(0).toUpperCase() + sentence.slice(1)}.`; + return `${sentence.charAt(0).toUpperCase() + sentence.substring(1)}.`; } /** * Generates a slugified text consisting of the given number of hyphen separated words. * * @param wordCount The number of words to generate. Defaults to `3`. + * @param wordCount.min The minimum number of words to generate. + * @param wordCount.max The maximum number of words to generate. * * @example * faker.lorem.slug() // 'dolores-illo-est' + * faker.lorem.slug(5) // 'delectus-totam-iusto-itaque-placeat' + * faker.lorem.slug({ min: 1, max: 3 }) // 'illo-ratione' * * @since 4.0.0 */ - slug(wordCount?: number): string { + slug(wordCount: number | { min: number; max: number } = 3): string { const words = this.words(wordCount); - return this.faker.helpers.slugify(words); } @@ -116,6 +123,8 @@ export class LoremModule { * Generates the given number of sentences. * * @param sentenceCount The number of sentences to generate. Defaults to a random number between `2` and `6`. + * @param sentenceCount.min The minimum number of sentences to generate. Defaults to `2`. + * @param sentenceCount.max The maximum number of sentences to generate. Defaults to `6`. * @param separator The separator to add between sentences. Defaults to `' '`. * * @example @@ -124,39 +133,45 @@ export class LoremModule { * faker.lorem.sentences(2, '\n') * // 'Et rerum a unde tempora magnam sit nisi. * // Et perspiciatis ipsam omnis.' + * faker.lorem.sentences({ min: 1, max: 3 }) // 'Placeat ex natus tenetur repellendus repellendus iste. Optio nostrum veritatis.' * * @since 2.0.1 */ - sentences(sentenceCount?: number, separator: string = ' '): string { - if (sentenceCount == null) { - sentenceCount = this.faker.datatype.number({ min: 2, max: 6 }); - } - const sentences: string[] = []; - for (sentenceCount; sentenceCount > 0; sentenceCount--) { - sentences.push(this.sentence()); - } - return sentences.join(separator); + sentences( + sentenceCount: number | { min: number; max: number } = { min: 2, max: 6 }, + separator: string = ' ' + ): string { + sentenceCount = this.faker.helpers.rangeToNumber(sentenceCount); + + return Array.from({ length: sentenceCount }) + .map(() => this.sentence()) + .join(separator); } /** - * Generates a paragraph with at least the given number of sentences. + * Generates a paragraph with the given number of sentences. * - * @param sentenceCount The minim number of sentences to generate. Defaults to `3`. + * @param sentenceCount The number of sentences to generate. Defaults to `3`. + * @param sentenceCount.min The minimum number of sentences to generate. + * @param sentenceCount.max The maximum number of sentences to generate. * * @example * faker.lorem.paragraph() // 'Non architecto nam unde sint. Ex tenetur dolor facere optio aut consequatur. Ea laudantium reiciendis repellendus.' - * faker.lorem.paragraph() // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.' + * faker.lorem.paragraph(2) // 'Animi possimus nemo consequuntur ut ea et tempore unde qui. Quis corporis esse occaecati.' + * faker.lorem.paragraph({ min: 1, max: 3 }) // 'Quis doloribus necessitatibus sint. Rerum accusamus impedit corporis porro.' * * @since 2.0.1 */ - paragraph(sentenceCount: number = 3): string { - return this.sentences(sentenceCount + this.faker.datatype.number(3)); + paragraph(sentenceCount: number | { min: number; max: number } = 3): string { + return this.sentences(sentenceCount); } /** * Generates the given number of paragraphs. * * @param paragraphCount The number of paragraphs to generate. Defaults to `3`. + * @param paragraphCount.min The minimum number of paragraphs to generate. + * @param paragraphCount.max The maximum number of paragraphs to generate. * @param separator The separator to use. Defaults to `'\n'`. * * @example @@ -176,14 +191,22 @@ export class LoremModule { * // 'Eos magnam aut qui accusamus. Sapiente quas culpa totam excepturi. Blanditiis totam distinctio occaecati dignissimos cumque atque qui officiis.<br/> * // Nihil quis vel consequatur. Blanditiis commodi deserunt sunt animi dolorum. A optio porro hic dolorum fugit aut et sint voluptas. Minima ad sed ipsa est non dolores.' * + * faker.lorem.paragraphs({ min: 1, max: 3 }) + * // 'Eum nam fugiat laudantium. + * // Dignissimos tempore porro necessitatibus commodi nam. + * // Veniam at commodi iste perferendis totam dolorum corporis ipsam.' + * * @since 2.0.1 */ - paragraphs(paragraphCount: number = 3, separator: string = '\n'): string { - const paragraphs: string[] = []; - for (paragraphCount; paragraphCount > 0; paragraphCount--) { - paragraphs.push(this.paragraph()); - } - return paragraphs.join(separator); + paragraphs( + paragraphCount: number | { min: number; max: number } = 3, + separator: string = '\n' + ): string { + paragraphCount = this.faker.helpers.rangeToNumber(paragraphCount); + + return Array.from({ length: paragraphCount }) + .map(() => this.paragraph()) + .join(separator); } /** @@ -202,8 +225,6 @@ export class LoremModule { */ text(): string { const methods: Array<keyof LoremModule> = [ - 'word', - 'words', 'sentence', 'sentences', 'paragraph', @@ -220,6 +241,8 @@ export class LoremModule { * Generates the given number lines of lorem separated by `'\n'`. * * @param lineCount The number of lines to generate. Defaults to a random number between `1` and `5`. + * @param lineCount.min The minimum number of lines to generate. Defaults to `1`. + * @param lineCount.max The maximum number of lines to generate. Defaults to `5`. * * @example * faker.lorem.lines() @@ -232,12 +255,20 @@ export class LoremModule { * // 'Soluta deserunt eos quam reiciendis libero autem enim nam ut. * // Voluptate aut aut.' * + * faker.lorem.lines(2) + * // 'Quod quas nam quis impedit aut consequuntur. + * // Animi dolores aspernatur.' + * + * faker.lorem.lines({ min: 1, max: 3 }) + * // 'Error dolorem natus quos eum consequatur necessitatibus.' + * * @since 3.1.0 */ - lines(lineCount?: number): string { - if (lineCount == null) { - lineCount = this.faker.datatype.number({ min: 1, max: 5 }); - } + lines( + lineCount: number | { min: number; max: number } = { min: 1, max: 5 } + ): string { + lineCount = this.faker.helpers.rangeToNumber(lineCount); + return this.sentences(lineCount, '\n'); } } diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 22cb39d5..f1a13c1d 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -98,14 +98,15 @@ export class StringModule { /** * Generating a string consisting of letters in the English alphabet. * - * @param options Either the number of characters or an options instance. Defaults to `{ length: 1, casing: 'mixed', exclude: [] }`. - * @param options.length The number of characters to generate. Defaults to `1`. + * @param options Either the number of characters or an options instance. + * @param options.length The number or range of characters to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'mixed'`. * @param options.exclude An array with characters which should be excluded in the generated string. Defaults to `[]`. * * @example * faker.string.alpha() // 'b' * faker.string.alpha(10) // 'fEcAaCVbaR' + * faker.string.alpha({ length: { min: 5, max: 10 } }) // 'HcVrCf' * faker.string.alpha({ casing: 'lower' }) // 'r' * faker.string.alpha({ exclude: ['W'] }) // 'Z' * faker.string.alpha({ length: 5, casing: 'upper', exclude: ['A'] }) // 'DTCIC' @@ -116,7 +117,7 @@ export class StringModule { options: | number | { - length?: number; + length?: number | { min: number; max: number }; casing?: Casing; exclude?: readonly LiteralUnion<AlphaChar>[] | string; } = {} @@ -127,17 +128,18 @@ export class StringModule { }; } - const { length = 1, casing = 'mixed' } = options; + const length = this.faker.helpers.rangeToNumber(options.length ?? 1); + if (length <= 0) { + return ''; + } + + const { casing = 'mixed' } = options; let { exclude = [] } = options; if (typeof exclude === 'string') { exclude = exclude.split(''); } - if (length <= 0) { - return ''; - } - let charsArray: string[]; switch (casing) { case 'upper': @@ -168,14 +170,15 @@ export class StringModule { /** * Generating a string consisting of alpha characters and digits. * - * @param options Either the number of characters or an options instance. Defaults to `{ length: 1, casing: 'mixed', exclude: [] }`. - * @param options.length The number of characters and digits to generate. Defaults to `1`. + * @param options Either the number of characters or an options instance. + * @param options.length The number or range of characters and digits to generate. Defaults to `1`. * @param options.casing The casing of the characters. Defaults to `'mixed'`. * @param options.exclude An array of characters and digits which should be excluded in the generated string. Defaults to `[]`. * * @example * faker.string.alphanumeric() // '2' * faker.string.alphanumeric(5) // '3e5V7' + * faker.string.alphanumeric({ length: { min: 5, max: 10 } }) // 'muaApG' * faker.string.alphanumeric({ casing: 'upper' }) // 'A' * faker.string.alphanumeric({ exclude: ['W'] }) // 'r' * faker.string.alphanumeric({ length: 5, exclude: ["a"] }) // 'x1Z7f' @@ -186,7 +189,7 @@ export class StringModule { options: | number | { - length?: number; + length?: number | { min: number; max: number }; casing?: Casing; exclude?: readonly LiteralUnion<AlphaNumericChar>[] | string; } = {} @@ -197,12 +200,12 @@ export class StringModule { }; } - const { length = 1, casing = 'mixed' } = options; - + const length = this.faker.helpers.rangeToNumber(options.length ?? 1); if (length <= 0) { return ''; } + const { casing = 'mixed' } = options; let { exclude = [] } = options; if (typeof exclude === 'string') { @@ -241,13 +244,14 @@ export class StringModule { * Returns a [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) string. * * @param options The optional options object. - * @param options.length Length of the generated number. Defaults to `1`. + * @param options.length The number or range of characters to generate after the prefix. Defaults to `1`. * @param options.casing Casing of the generated number. Defaults to `'mixed'`. * @param options.prefix Prefix for the generated number. Defaults to `'0x'`. * * @example * faker.string.hexadecimal() // '0xB' * faker.string.hexadecimal({ length: 10 }) // '0xaE13d044cB' + * faker.string.hexadecimal({ length: { min: 5, max: 10 } }) // '0x7dEf7FCD' * faker.string.hexadecimal({ prefix: '0x' }) // '0xE' * faker.string.hexadecimal({ casing: 'lower' }) // '0xf' * faker.string.hexadecimal({ length: 10, prefix: '#' }) // '#f12a974eB1' @@ -259,12 +263,16 @@ export class StringModule { */ hexadecimal( options: { - length?: number; + length?: number | { min: number; max: number }; casing?: Casing; prefix?: string; } = {} ): string { - const { length = 1, casing = 'mixed', prefix = '0x' } = options; + const { casing = 'mixed', prefix = '0x' } = options; + const length = this.faker.helpers.rangeToNumber(options.length ?? 1); + if (length <= 0) { + return prefix; + } let wholeString = ''; @@ -307,8 +315,8 @@ export class StringModule { /** * Generates a given length string of digits. * - * @param options Either the number of characters or the options to use. Defaults to `{ length: 1, allowLeadingZeros = false, exclude = [] }`. - * @param options.length The number of digits to generate. Defaults to `1`. + * @param options Either the number of characters or the options to use. + * @param options.length The number or range of digits to generate. Defaults to `1`. * @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`. * @param options.exclude An array of digits which should be excluded in the generated string. Defaults to `[]`. * @@ -316,6 +324,7 @@ export class StringModule { * faker.string.numeric() // '2' * faker.string.numeric(5) // '31507' * faker.string.numeric(42) // '56434563150765416546479875435481513188548' + * faker.string.numeric({ length: { min: 5, max: 10 } }) // '197089478' * faker.string.numeric({ length: 42, allowLeadingZeros: true }) // '00564846278453876543517840713421451546115' * faker.string.numeric({ length: 6, exclude: ['0'] }) // '943228' * @@ -325,7 +334,7 @@ export class StringModule { options: | number | { - length?: number; + length?: number | { min: number; max: number }; allowLeadingZeros?: boolean; exclude?: readonly LiteralUnion<NumericChar>[] | string; } = {} @@ -336,11 +345,12 @@ export class StringModule { }; } - const { length = 1, allowLeadingZeros = false } = options; + const length = this.faker.helpers.rangeToNumber(options.length ?? 1); if (length <= 0) { return ''; } + const { allowLeadingZeros = false } = options; let { exclude = [] } = options; if (typeof exclude === 'string') { @@ -381,14 +391,18 @@ export class StringModule { * Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`). * * @param length Length of the generated string. Max length is `2^20`. Defaults to `10`. + * @param length.min The minimum number of characters to generate. + * @param length.max The maximum number of characters to generate. * * @example * faker.string.sample() // 'Zo!.:*e>wR' * faker.string.sample(5) // '6Bye8' + * faker.string.sample({ min: 5, max: 10 }) // 'FeKunG' * * @since 8.0.0 */ - sample(length = 10): string { + sample(length: number | { min: number; max: number } = 10): string { + length = this.faker.helpers.rangeToNumber(length); if (length >= SAMPLE_MAX_LENGTH) { length = SAMPLE_MAX_LENGTH; } diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 8c935c88..12bb25ba 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -50,23 +50,26 @@ export class SystemModule { * Returns a random file name with extension. * * @param options An options object. - * @param options.extensionCount Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`. + * @param options.extensionCount Define how many extensions the file name should have. Defaults to `1`. * * @example * faker.system.fileName() // 'faithfully_calculating.u8mdn' * faker.system.fileName({ extensionCount: 2 }) // 'times_after.swf.ntf' + * faker.system.fileName({ extensionCount: { min: 1, max: 2 } }) // 'jaywalk_like_ill.osfpvg' * * @since 3.1.0 */ fileName( options: { /** - * Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`. + * Define how many extensions the file name should have. Defaults to `1`. */ - extensionCount?: number; + extensionCount?: number | { min: number; max: number }; } = {} ): string { - const { extensionCount = 1 } = options; + const extensionCount = this.faker.helpers.rangeToNumber( + options.extensionCount ?? 1 + ); const baseName = this.faker.word.words().toLowerCase().replace(/\W/g, '_'); diff --git a/src/modules/word/index.ts b/src/modules/word/index.ts index 89795919..d326cd70 100644 --- a/src/modules/word/index.ts +++ b/src/modules/word/index.ts @@ -375,18 +375,26 @@ export class WordModule { /** * Returns a string containing a number of space separated random words. * - * @param count Number of words. Defaults to a random value between `1` and `3`. + * @param options The optional options object or the number of words to return. + * @param options.count The number of words to return. Defaults to a random value between `1` and `3`. * * @example * faker.word.words() // 'almost' * faker.word.words(5) // 'before hourly patiently dribble equal' + * faker.word.words({ count: 5 }) // 'whoever edible um kissingly faraway' + * faker.word.words({ count: { min: 5, max: 10 } }) // 'vice buoyant through apropos poised total wary boohoo' * * @since 8.0.0 */ - words(count?: number): string { - if (count == null) { - count = this.faker.datatype.number({ min: 1, max: 3 }); + words( + options: number | { count?: number | { min: number; max: number } } = {} + ): string { + if (typeof options === 'number') { + options = { count: options }; } + const count = this.faker.helpers.rangeToNumber( + options.count ?? { min: 1, max: 3 } + ); return Array.from({ length: count }, () => this.sample()).join(' '); } |
