diff options
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/color/index.ts | 26 | ||||
| -rw-r--r-- | src/modules/food/index.ts | 18 | ||||
| -rw-r--r-- | src/modules/internet/index.ts | 83 | ||||
| -rw-r--r-- | src/modules/system/index.ts | 21 |
4 files changed, 95 insertions, 53 deletions
diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 2b355062..10d7023a 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -102,6 +102,15 @@ function toBinary(values: number[]): string { } /** + * Converts the given value to a percentage (`round(value * 100)`). + * + * @param value The value to convert to a percentage. + */ +function toPercentage(value: number): number { + return Math.round(value * 100); +} + +/** * Converts an array of numbers into CSS accepted format. * * @param values Array of values to be converted. @@ -113,7 +122,6 @@ function toCSS( cssFunction: CssFunctionType = 'rgb', space: CssSpaceType = 'sRGB' ): string { - const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { case 'rgba': { return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`; @@ -124,35 +132,35 @@ function toCSS( } case 'cmyk': { - return `cmyk(${percentage(values[0])}%, ${percentage( + return `cmyk(${toPercentage(values[0])}%, ${toPercentage( values[1] - )}%, ${percentage(values[2])}%, ${percentage(values[3])}%)`; + )}%, ${toPercentage(values[2])}%, ${toPercentage(values[3])}%)`; } case 'hsl': { - return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( + return `hsl(${values[0]}deg ${toPercentage(values[1])}% ${toPercentage( values[2] )}%)`; } case 'hsla': { - return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage( + return `hsl(${values[0]}deg ${toPercentage(values[1])}% ${toPercentage( values[2] - )}% / ${percentage(values[3])})`; + )}% / ${toPercentage(values[3])})`; } case 'hwb': { - return `hwb(${values[0]} ${percentage(values[1])}% ${percentage( + return `hwb(${values[0]} ${toPercentage(values[1])}% ${toPercentage( values[2] )}%)`; } case 'lab': { - return `lab(${percentage(values[0])}% ${values[1]} ${values[2]})`; + return `lab(${toPercentage(values[0])}% ${values[1]} ${values[2]})`; } case 'lch': { - return `lch(${percentage(values[0])}% ${values[1]} ${values[2]})`; + return `lch(${toPercentage(values[0])}% ${values[1]} ${values[2]})`; } case 'rgb': { diff --git a/src/modules/food/index.ts b/src/modules/food/index.ts index 45b91f30..8dd77741 100644 --- a/src/modules/food/index.ts +++ b/src/modules/food/index.ts @@ -1,4 +1,17 @@ import { ModuleBase } from '../../internal/module-base'; + +/** + * Converts the given string to title case. + * + * @param text The text to convert. + */ +function toTitleCase(text: string): string { + return text + .split(' ') + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join(' '); +} + /** * Module for generating food-related data. * @@ -47,11 +60,6 @@ export class FoodModule extends ModuleBase { */ dish(): string { // A 50/50 mix of specific dishes and dish_patterns - const toTitleCase = (s: string) => - s - .split(' ') - .map((w) => w.charAt(0).toUpperCase() + w.slice(1)) - .join(' '); if (this.faker.datatype.boolean()) { return toTitleCase( this.faker.helpers.fake(this.faker.definitions.food.dish_pattern) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 115bd215..4c78f4cb 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -1,4 +1,5 @@ import { FakerError } from '../../errors/faker-error'; +import type { Faker } from '../../faker'; import { toBase64Url } from '../../internal/base64'; import { deprecated } from '../../internal/deprecated'; import { ModuleBase } from '../../internal/module-base'; @@ -103,6 +104,50 @@ const ipv4Networks: Record<IPv4Network, string> = { }; /** + * Checks whether the given string is a valid slug for `domainWord`s. + * + * @param slug The slug to check. + */ +function isValidDomainWordSlug(slug: string): boolean { + return /^[a-z][a-z-]*[a-z]$/i.exec(slug) !== null; +} + +/** + * Tries various ways to produce a valid domain word slug, falling back to a random string if needed. + * + * @param faker The faker instance to use. + * @param word The initial word to slugify. + */ +function makeValidDomainWordSlug(faker: Faker, word: string): string { + const slug1 = faker.helpers.slugify(word); + if (isValidDomainWordSlug(slug1)) { + return slug1; + } + + const slug2 = faker.helpers.slugify(faker.lorem.word()); + if (isValidDomainWordSlug(slug2)) { + return slug2; + } + + return faker.string.alpha({ + casing: 'lower', + length: faker.number.int({ min: 4, max: 8 }), + }); +} + +/** + * Generates a random color in hex format with the given base color. + * + * @param faker The faker instance to use. + * @param base The base color to use. + */ +function colorFromBase(faker: Faker, base: number): string { + return Math.floor((faker.number.int(256) + base) / 2) + .toString(16) + .padStart(2, '0'); +} + +/** * Module to generate internet related entries. * * ### Overview @@ -597,29 +642,12 @@ export class InternetModule extends ModuleBase { domainWord(): string { // Generate an ASCII "word" in the form `noun-adjective` // For locales with non-ASCII characters, we fall back to lorem words, or a random string - const isValidSlug = (slug: string): boolean => { - return /^[a-z][a-z-]*[a-z]$/i.exec(slug) !== null; - }; - - const makeValidSlug = (word: string): string => { - const slug1 = this.faker.helpers.slugify(word); - if (isValidSlug(slug1)) { - return slug1; - } - const slug2 = this.faker.helpers.slugify(this.faker.lorem.word()); - if (isValidSlug(slug2)) { - return slug2; - } - - return this.faker.string.alpha({ - casing: 'lower', - length: this.faker.number.int({ min: 4, max: 8 }), - }); - }; - - const word1 = makeValidSlug(this.faker.word.adjective()); - const word2 = makeValidSlug(this.faker.word.noun()); + const word1 = makeValidDomainWordSlug( + this.faker, + this.faker.word.adjective() + ); + const word2 = makeValidDomainWordSlug(this.faker, this.faker.word.noun()); return `${word1}-${word2}`.toLowerCase(); } @@ -819,14 +847,9 @@ export class InternetModule extends ModuleBase { ): string { const { redBase = 0, greenBase = 0, blueBase = 0 } = options; - const colorFromBase = (base: number): string => - Math.floor((this.faker.number.int(256) + base) / 2) - .toString(16) - .padStart(2, '0'); - - const red = colorFromBase(redBase); - const green = colorFromBase(greenBase); - const blue = colorFromBase(blueBase); + const red = colorFromBase(this.faker, redBase); + const green = colorFromBase(this.faker, greenBase); + const blue = colorFromBase(this.faker, blueBase); return `#${red}${green}${blue}`; } diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index aec3525e..1c387e51 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -263,17 +263,17 @@ export class SystemModule extends ModuleBase { let suffix: string; let prefix = ''; - const digit = () => this.faker.string.numeric({ allowLeadingZeros: true }); switch (interfaceSchema) { case 'index': { - suffix = digit(); + suffix = this.faker.string.numeric(); break; } case 'slot': { - suffix = `${digit()}${ - this.faker.helpers.maybe(() => `f${digit()}`) ?? '' - }${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`; + suffix = `${this.faker.string.numeric()}${ + this.faker.helpers.maybe(() => `f${this.faker.string.numeric()}`) ?? + '' + }${this.faker.helpers.maybe(() => `d${this.faker.string.numeric()}`) ?? ''}`; break; } @@ -283,10 +283,13 @@ export class SystemModule extends ModuleBase { } case 'pci': { - prefix = this.faker.helpers.maybe(() => `P${digit()}`) ?? ''; - suffix = `${digit()}s${digit()}${ - this.faker.helpers.maybe(() => `f${digit()}`) ?? '' - }${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`; + prefix = + this.faker.helpers.maybe(() => `P${this.faker.string.numeric()}`) ?? + ''; + suffix = `${this.faker.string.numeric()}s${this.faker.string.numeric()}${ + this.faker.helpers.maybe(() => `f${this.faker.string.numeric()}`) ?? + '' + }${this.faker.helpers.maybe(() => `d${this.faker.string.numeric()}`) ?? ''}`; break; } } |
