Skip to content

Commit bf4e3a3

Browse files
authored
Merge branch 'next' into test/cleanup/randomSeed
2 parents 10f0e33 + eae226e commit bf4e3a3

File tree

16 files changed

+1014
-475
lines changed

16 files changed

+1014
-475
lines changed

.versionrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"scripts": {
3+
"postbump": "export VERSION=$(jq -r .version package.json); sed -i -E \"s/@faker-js\\/faker@v[0-9]+\\.[0-9]+\\.[0-9]+(-(alpha|beta|rc)\\.[0-9]+)?/@faker-js\\/faker@v$VERSION/g\" docs/guide/usage.md; git add docs/guide/usage.md"
4+
},
25
"skip": {
36
"tag": true
47
},

eslint.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
162162

163163
// TODO @Shinigami92 2023-09-23: The following rules currently conflict with our code.
164164
// Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently.
165-
'unicorn/consistent-function-scoping': 'off',
166165
'unicorn/prefer-export-from': 'off',
167166
'unicorn/prevent-abbreviations': 'off',
168167
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"docs:test:e2e:ci": "run-s docs:build:ci docs:test:e2e:run",
2929
"docs:test:e2e:run": "run-p --race docs:serve \"cypress run\"",
3030
"docs:test:e2e:open": "run-p --race docs:serve \"cypress open\"",
31-
"release": "commit-and-tag-version",
31+
"release": "commit-and-tag-version --commit-all",
3232
"prepublishOnly": "pnpm run clean && pnpm install && pnpm run build",
3333
"preflight": "pnpm install && run-s generate format lint build test:update-snapshots ts-check"
3434
},

pnpm-lock.yaml

Lines changed: 425 additions & 375 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/apidocs/processing/class.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
processClassConstructors,
1414
processClassMethods,
1515
processInterfaceMethods,
16-
processProjectFunctions,
16+
processUtilityFunctions,
1717
} from './method';
1818

1919
/**
@@ -192,12 +192,7 @@ export function processProjectUtilities(project: Project): RawApiDocsPage {
192192
deprecated: undefined,
193193
description: 'A list of all the utilities available in Faker.js.',
194194
examples: [],
195-
methods: processProjectFunctions(
196-
project,
197-
'mergeLocales',
198-
'generateMersenne32Randomizer',
199-
'generateMersenne53Randomizer'
200-
),
195+
methods: processUtilityFunctions(project),
201196
};
202197
}
203198

scripts/apidocs/processing/method.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
type MethodDeclaration,
1212
} from 'ts-morph';
1313
import { groupBy } from '../../../src/internal/group-by';
14-
import { valuesForKeys } from '../utils/value-checks';
1514
import { newProcessingError } from './error';
1615
import type {
1716
RawApiDocsSignature,
@@ -138,12 +137,11 @@ function getAllFunctions(
138137
);
139138
}
140139

141-
export function processProjectFunctions(
142-
project: Project,
143-
...names: string[]
144-
): RawApiDocsMethod[] {
140+
export function processUtilityFunctions(project: Project): RawApiDocsMethod[] {
145141
return processMethodLikes(
146-
valuesForKeys(getAllFunctions(project), names),
142+
Object.values(getAllFunctions(project)).filter((fn) =>
143+
fn.getSourceFile().getFilePath().includes('/src/utils/')
144+
),
147145
(f) => f.getNameOrThrow()
148146
);
149147
}

src/modules/color/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ function toBinary(values: number[]): string {
101101
return binary.join(' ');
102102
}
103103

104+
/**
105+
* Converts the given value to a percentage (`round(value * 100)`).
106+
*
107+
* @param value The value to convert to a percentage.
108+
*/
109+
function toPercentage(value: number): number {
110+
return Math.round(value * 100);
111+
}
112+
104113
/**
105114
* Converts an array of numbers into CSS accepted format.
106115
*
@@ -113,7 +122,6 @@ function toCSS(
113122
cssFunction: CssFunctionType = 'rgb',
114123
space: CssSpaceType = 'sRGB'
115124
): string {
116-
const percentage = (value: number) => Math.round(value * 100);
117125
switch (cssFunction) {
118126
case 'rgba': {
119127
return `rgba(${values[0]}, ${values[1]}, ${values[2]}, ${values[3]})`;
@@ -124,35 +132,35 @@ function toCSS(
124132
}
125133

126134
case 'cmyk': {
127-
return `cmyk(${percentage(values[0])}%, ${percentage(
135+
return `cmyk(${toPercentage(values[0])}%, ${toPercentage(
128136
values[1]
129-
)}%, ${percentage(values[2])}%, ${percentage(values[3])}%)`;
137+
)}%, ${toPercentage(values[2])}%, ${toPercentage(values[3])}%)`;
130138
}
131139

132140
case 'hsl': {
133-
return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage(
141+
return `hsl(${values[0]}deg ${toPercentage(values[1])}% ${toPercentage(
134142
values[2]
135143
)}%)`;
136144
}
137145

138146
case 'hsla': {
139-
return `hsl(${values[0]}deg ${percentage(values[1])}% ${percentage(
147+
return `hsl(${values[0]}deg ${toPercentage(values[1])}% ${toPercentage(
140148
values[2]
141-
)}% / ${percentage(values[3])})`;
149+
)}% / ${toPercentage(values[3])})`;
142150
}
143151

144152
case 'hwb': {
145-
return `hwb(${values[0]} ${percentage(values[1])}% ${percentage(
153+
return `hwb(${values[0]} ${toPercentage(values[1])}% ${toPercentage(
146154
values[2]
147155
)}%)`;
148156
}
149157

150158
case 'lab': {
151-
return `lab(${percentage(values[0])}% ${values[1]} ${values[2]})`;
159+
return `lab(${toPercentage(values[0])}% ${values[1]} ${values[2]})`;
152160
}
153161

154162
case 'lch': {
155-
return `lch(${percentage(values[0])}% ${values[1]} ${values[2]})`;
163+
return `lch(${toPercentage(values[0])}% ${values[1]} ${values[2]})`;
156164
}
157165

158166
case 'rgb': {

src/modules/food/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { ModuleBase } from '../../internal/module-base';
2+
3+
/**
4+
* Converts the given string to title case.
5+
*
6+
* @param text The text to convert.
7+
*/
8+
function toTitleCase(text: string): string {
9+
return text
10+
.split(' ')
11+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
12+
.join(' ');
13+
}
14+
215
/**
316
* Module for generating food-related data.
417
*
@@ -47,11 +60,6 @@ export class FoodModule extends ModuleBase {
4760
*/
4861
dish(): string {
4962
// A 50/50 mix of specific dishes and dish_patterns
50-
const toTitleCase = (s: string) =>
51-
s
52-
.split(' ')
53-
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
54-
.join(' ');
5563
if (this.faker.datatype.boolean()) {
5664
return toTitleCase(
5765
this.faker.helpers.fake(this.faker.definitions.food.dish_pattern)

src/modules/internet/index.ts

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FakerError } from '../../errors/faker-error';
2+
import type { Faker } from '../../faker';
23
import { toBase64Url } from '../../internal/base64';
34
import { deprecated } from '../../internal/deprecated';
45
import { ModuleBase } from '../../internal/module-base';
@@ -102,6 +103,50 @@ const ipv4Networks: Record<IPv4Network, string> = {
102103
[IPv4Network.Multicast]: '224.0.0.0/4',
103104
};
104105

106+
/**
107+
* Checks whether the given string is a valid slug for `domainWord`s.
108+
*
109+
* @param slug The slug to check.
110+
*/
111+
function isValidDomainWordSlug(slug: string): boolean {
112+
return /^[a-z][a-z-]*[a-z]$/i.exec(slug) !== null;
113+
}
114+
115+
/**
116+
* Tries various ways to produce a valid domain word slug, falling back to a random string if needed.
117+
*
118+
* @param faker The faker instance to use.
119+
* @param word The initial word to slugify.
120+
*/
121+
function makeValidDomainWordSlug(faker: Faker, word: string): string {
122+
const slug1 = faker.helpers.slugify(word);
123+
if (isValidDomainWordSlug(slug1)) {
124+
return slug1;
125+
}
126+
127+
const slug2 = faker.helpers.slugify(faker.lorem.word());
128+
if (isValidDomainWordSlug(slug2)) {
129+
return slug2;
130+
}
131+
132+
return faker.string.alpha({
133+
casing: 'lower',
134+
length: faker.number.int({ min: 4, max: 8 }),
135+
});
136+
}
137+
138+
/**
139+
* Generates a random color in hex format with the given base color.
140+
*
141+
* @param faker The faker instance to use.
142+
* @param base The base color to use.
143+
*/
144+
function colorFromBase(faker: Faker, base: number): string {
145+
return Math.floor((faker.number.int(256) + base) / 2)
146+
.toString(16)
147+
.padStart(2, '0');
148+
}
149+
105150
/**
106151
* Module to generate internet related entries.
107152
*
@@ -597,29 +642,12 @@ export class InternetModule extends ModuleBase {
597642
domainWord(): string {
598643
// Generate an ASCII "word" in the form `noun-adjective`
599644
// For locales with non-ASCII characters, we fall back to lorem words, or a random string
600-
const isValidSlug = (slug: string): boolean => {
601-
return /^[a-z][a-z-]*[a-z]$/i.exec(slug) !== null;
602-
};
603-
604-
const makeValidSlug = (word: string): string => {
605-
const slug1 = this.faker.helpers.slugify(word);
606-
if (isValidSlug(slug1)) {
607-
return slug1;
608-
}
609645

610-
const slug2 = this.faker.helpers.slugify(this.faker.lorem.word());
611-
if (isValidSlug(slug2)) {
612-
return slug2;
613-
}
614-
615-
return this.faker.string.alpha({
616-
casing: 'lower',
617-
length: this.faker.number.int({ min: 4, max: 8 }),
618-
});
619-
};
620-
621-
const word1 = makeValidSlug(this.faker.word.adjective());
622-
const word2 = makeValidSlug(this.faker.word.noun());
646+
const word1 = makeValidDomainWordSlug(
647+
this.faker,
648+
this.faker.word.adjective()
649+
);
650+
const word2 = makeValidDomainWordSlug(this.faker, this.faker.word.noun());
623651
return `${word1}-${word2}`.toLowerCase();
624652
}
625653

@@ -819,14 +847,9 @@ export class InternetModule extends ModuleBase {
819847
): string {
820848
const { redBase = 0, greenBase = 0, blueBase = 0 } = options;
821849

822-
const colorFromBase = (base: number): string =>
823-
Math.floor((this.faker.number.int(256) + base) / 2)
824-
.toString(16)
825-
.padStart(2, '0');
826-
827-
const red = colorFromBase(redBase);
828-
const green = colorFromBase(greenBase);
829-
const blue = colorFromBase(blueBase);
850+
const red = colorFromBase(this.faker, redBase);
851+
const green = colorFromBase(this.faker, greenBase);
852+
const blue = colorFromBase(this.faker, blueBase);
830853

831854
return `#${red}${green}${blue}`;
832855
}

src/modules/system/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,17 @@ export class SystemModule extends ModuleBase {
263263

264264
let suffix: string;
265265
let prefix = '';
266-
const digit = () => this.faker.string.numeric({ allowLeadingZeros: true });
267266
switch (interfaceSchema) {
268267
case 'index': {
269-
suffix = digit();
268+
suffix = this.faker.string.numeric();
270269
break;
271270
}
272271

273272
case 'slot': {
274-
suffix = `${digit()}${
275-
this.faker.helpers.maybe(() => `f${digit()}`) ?? ''
276-
}${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`;
273+
suffix = `${this.faker.string.numeric()}${
274+
this.faker.helpers.maybe(() => `f${this.faker.string.numeric()}`) ??
275+
''
276+
}${this.faker.helpers.maybe(() => `d${this.faker.string.numeric()}`) ?? ''}`;
277277
break;
278278
}
279279

@@ -283,10 +283,13 @@ export class SystemModule extends ModuleBase {
283283
}
284284

285285
case 'pci': {
286-
prefix = this.faker.helpers.maybe(() => `P${digit()}`) ?? '';
287-
suffix = `${digit()}s${digit()}${
288-
this.faker.helpers.maybe(() => `f${digit()}`) ?? ''
289-
}${this.faker.helpers.maybe(() => `d${digit()}`) ?? ''}`;
286+
prefix =
287+
this.faker.helpers.maybe(() => `P${this.faker.string.numeric()}`) ??
288+
'';
289+
suffix = `${this.faker.string.numeric()}s${this.faker.string.numeric()}${
290+
this.faker.helpers.maybe(() => `f${this.faker.string.numeric()}`) ??
291+
''
292+
}${this.faker.helpers.maybe(() => `d${this.faker.string.numeric()}`) ?? ''}`;
290293
break;
291294
}
292295
}

test/modules/date.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ const converterMap = [
1212
const NON_SEEDED_BASED_RUN = 5;
1313
const refDate = '2021-02-21T17:09:15.711Z';
1414

15+
function calculateAge(birthdate: Date, refDate: Date): number {
16+
let age = refDate.getFullYear() - birthdate.getFullYear();
17+
if (
18+
refDate.getMonth() < birthdate.getMonth() ||
19+
(refDate.getMonth() === birthdate.getMonth() &&
20+
refDate.getDate() < birthdate.getDate())
21+
) {
22+
age--;
23+
}
24+
25+
return age;
26+
}
27+
1528
describe('date', () => {
1629
seededTests(faker, 'date', (t) => {
1730
t.describe('anytime', (t) => {
@@ -530,19 +543,6 @@ describe('date', () => {
530543
});
531544

532545
describe('birthdate', () => {
533-
function calculateAge(birthdate: Date, refDate: Date): number {
534-
let age = refDate.getFullYear() - birthdate.getFullYear();
535-
if (
536-
refDate.getMonth() < birthdate.getMonth() ||
537-
(refDate.getMonth() === birthdate.getMonth() &&
538-
refDate.getDate() < birthdate.getDate())
539-
) {
540-
age--;
541-
}
542-
543-
return age;
544-
}
545-
546546
it('returns a random birthdate', () => {
547547
const birthdate = faker.date.birthdate();
548548
expect(birthdate).toBeInstanceOf(Date);

0 commit comments

Comments
 (0)