Skip to content

Commit 13d0aeb

Browse files
authored
Merge branch 'main' into fix-vehicle-vin-length
2 parents 02f4b88 + 8ab5eff commit 13d0aeb

19 files changed

+366
-227
lines changed

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"license": "MIT",
2424
"main": "dist/cjs/index.js",
25-
"module": "dist/esm/index.js",
25+
"module": "dist/esm/index.mjs",
2626
"types": "index.d.ts",
2727
"typesVersions": {
2828
">=4.0": {
@@ -33,14 +33,12 @@
3333
},
3434
"exports": {
3535
".": {
36-
"node": "./dist/cjs/index.js",
37-
"es2015": "./dist/esm/index.js",
38-
"default": "./dist/esm/index.js"
36+
"require": "./dist/cjs/index.js",
37+
"import": "./dist/esm/index.mjs"
3938
},
4039
"./locale/*": {
41-
"node": "./dist/cjs/locale/*.js",
42-
"es2015": "./dist/esm/locale/*.js",
43-
"default": "./dist/esm/locale/*.js"
40+
"require": "./dist/cjs/locale/*.js",
41+
"import": "./dist/esm/locale/*.mjs"
4442
},
4543
"./package.json": "./package.json"
4644
},

scripts/bundle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ buildSync({
5353
splitting: true,
5454
format: 'esm',
5555
target: 'node12.20',
56+
outExtension: { '.js': '.mjs' },
5657
});

src/fake.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export class Fake {
1717
/**
1818
* Generator method for combining faker methods based on string input.
1919
*
20-
* This will check the given string for placeholders and replace them by calling the specified faker method.
20+
* Note: If you just want to create a string on the fly, we recommend using string template literals instead.
21+
* This method is useful if you wish to choose a random format from a non-executable source or persistent storage (json etc.).
22+
*
23+
* It checks the given string for placeholders and replace them by calling the specified faker method.
2124
* E.g. the input `Hi, my name is {{name.firstName}}!`,
2225
* will use the `faker.name.firstName()` method to resolve the placeholder.
2326
* It is also possible to combine static text with placeholders,

src/faker.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type UsableLocale = LiteralUnion<KnownLocale>;
3636
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
3737

3838
export interface FakerOptions {
39-
locales?: UsedLocales;
39+
locales: UsedLocales;
4040
locale?: UsableLocale;
4141
localeFallback?: UsableLocale;
4242
}
@@ -81,50 +81,59 @@ export class Faker {
8181
readonly vehicle: Vehicle = new Vehicle(this);
8282
readonly word: Word = new Word(this);
8383

84-
constructor(opts: FakerOptions = {}) {
85-
this.locales = this.locales || opts.locales || {};
84+
constructor(opts: FakerOptions) {
85+
if (!opts) {
86+
throw new Error(
87+
'Options with at least one entry in locales must be provided'
88+
);
89+
}
90+
91+
if (Object.keys(opts.locales ?? {}).length === 0) {
92+
throw new Error(
93+
'At least one entry in locales must be provided in the locales parameter'
94+
);
95+
}
96+
97+
this.locales = opts.locales;
8698
this.locale = this.locale || opts.locale || 'en';
8799
this.localeFallback = this.localeFallback || opts.localeFallback || 'en';
88100

89101
this.loadDefinitions();
90102
}
91103

92104
/**
93-
* Load the definitions contained in the locales file for the given types
105+
* Load the definitions contained in the locales file for the given types.
106+
*
107+
* Background: Certain localization sets contain less data then others.
108+
* In the case of a missing definition, use the localeFallback's values
109+
* to substitute the missing data.
94110
*/
95111
private loadDefinitions(): void {
96112
// TODO @Shinigami92 2022-01-11: Find a way to load this even more dynamically
97113
// In a way so that we don't accidentally miss a definition
98-
Object.entries(DEFINITIONS).forEach(([t, v]) => {
99-
if (this.definitions[t] == null) {
100-
this.definitions[t] = {};
114+
for (const [moduleName, entryNames] of Object.entries(DEFINITIONS)) {
115+
if (typeof entryNames === 'string') {
116+
// For 'title' and 'separator'
117+
Object.defineProperty(this.definitions, moduleName, {
118+
get: (): unknown /* string */ =>
119+
this.locales[this.locale][moduleName] ??
120+
this.locales[this.localeFallback][moduleName],
121+
});
122+
continue;
101123
}
102124

103-
if (typeof v === 'string') {
104-
this.definitions[t] = v;
105-
return;
125+
if (this.definitions[moduleName] == null) {
126+
this.definitions[moduleName] = {};
106127
}
107128

108-
v.forEach((p) => {
109-
Object.defineProperty(this.definitions[t], p, {
110-
get: () => {
111-
if (
112-
this.locales[this.locale][t] == null ||
113-
this.locales[this.locale][t][p] == null
114-
) {
115-
// certain localization sets contain less data then others.
116-
// in the case of a missing definition, use the default localeFallback
117-
// to substitute the missing set data
118-
// throw new Error('unknown property ' + d + p)
119-
return this.locales[this.localeFallback][t][p];
120-
} else {
121-
// return localized data
122-
return this.locales[this.locale][t][p];
123-
}
124-
},
129+
for (const entryName of entryNames) {
130+
Object.defineProperty(this.definitions[moduleName], entryName, {
131+
get: (): unknown =>
132+
this.locales[this.locale][moduleName]?.[entryName] ??
133+
this.locales[this.localeFallback][moduleName]?.[entryName],
125134
});
126-
});
127-
});
135+
}
136+
}
128137
}
129138

130139
seed(seed?: number | number[]): void {

src/git.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,17 @@ export class Git {
6161
*/
6262
commitEntry(options: { merge?: boolean } = {}): string {
6363
// TODO @Shinigami92 2022-01-11: We may want to make it configurable to use just `\n` instead of `\r\n`
64-
let entry = 'commit {{git.commitSha}}\r\n';
64+
let entry = `commit ${this.commitSha()}\r\n`;
6565

6666
if (options.merge || this.faker.datatype.number({ min: 0, max: 4 }) === 0) {
67-
entry += 'Merge: {{git.shortSha}} {{git.shortSha}}\r\n';
67+
entry += `Merge: ${this.shortSha()}} ${this.shortSha()}\r\n`;
6868
}
6969

70-
entry +=
71-
'Author: {{name.firstName}} {{name.lastName}} <{{internet.email}}>\r\n';
72-
entry += 'Date: ' + this.faker.date.recent().toString() + '\r\n';
73-
entry += '\r\n\xa0\xa0\xa0\xa0{{git.commitMessage}}\r\n';
70+
entry += `Author: ${this.faker.name.firstName()} ${this.faker.name.lastName()} <${this.faker.internet.email()}>\r\n`;
71+
entry += `Date: ${this.faker.date.recent().toString()}\r\n`;
72+
entry += `\r\n\xa0\xa0\xa0\xa0${this.commitMessage()}\r\n`;
7473

75-
return this.faker.fake(entry);
74+
return entry;
7675
}
7776

7877
/**
@@ -82,8 +81,7 @@ export class Git {
8281
* faker.git.commitMessage() // 'reboot cross-platform driver'
8382
*/
8483
commitMessage(): string {
85-
const format = '{{hacker.verb}} {{hacker.adjective}} {{hacker.noun}}';
86-
return this.faker.fake(format);
84+
return `${this.faker.hacker.verb()} ${this.faker.hacker.adjective()} ${this.faker.hacker.noun()}`;
8785
}
8886

8987
/**

src/helpers.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '.';
2+
import { deprecated } from './internal/deprecated';
23

34
/**
45
* A full card with various details.
@@ -143,9 +144,12 @@ export class Helpers {
143144
randomize<T = string>(
144145
array: ReadonlyArray<T> = ['a', 'b', 'c'] as unknown as ReadonlyArray<T>
145146
): T {
146-
console.warn(
147-
'Deprecation Warning: faker.helpers.randomize is now located in faker.random.arrayElement'
148-
);
147+
deprecated({
148+
deprecated: 'faker.helpers.randomize()',
149+
proposed: 'faker.random.arrayElement()',
150+
// since: 'v5.0.0', (?)
151+
until: 'v7.0.0',
152+
});
149153
return this.faker.random.arrayElement(array);
150154
}
151155

@@ -511,9 +515,12 @@ export class Helpers {
511515
* @deprecated If you need some specific object you should create your own method.
512516
*/
513517
createCard(): Card {
514-
console.warn(
515-
'Deprecation Warning: If you need some specific object you should create your own method.'
516-
);
518+
deprecated({
519+
deprecated: 'helpers.createCard()',
520+
proposed: 'a self-build function',
521+
since: 'v6.1.0',
522+
until: 'v7.0.0',
523+
});
517524
return {
518525
name: this.faker.name.findName(),
519526
username: this.faker.internet.userName(),
@@ -582,9 +589,12 @@ export class Helpers {
582589
* @deprecated If you need some specific object you should create your own method.
583590
*/
584591
contextualCard(): ContextualCard {
585-
console.warn(
586-
'Deprecation Warning: If you need some specific object you should create your own method.'
587-
);
592+
deprecated({
593+
deprecated: 'helpers.contextualCard()',
594+
proposed: 'a self-build function',
595+
since: 'v6.1.0',
596+
until: 'v7.0.0',
597+
});
588598
const name = this.faker.name.firstName();
589599
const userName = this.faker.internet.userName(name);
590600
return {
@@ -631,9 +641,12 @@ export class Helpers {
631641
* @deprecated If you need some specific object you should create your own method.
632642
*/
633643
userCard(): UserCard {
634-
console.warn(
635-
'Deprecation Warning: If you need some specific object you should create your own method.'
636-
);
644+
deprecated({
645+
deprecated: 'helpers.userCard()',
646+
proposed: 'a self-build function',
647+
since: 'v6.1.0',
648+
until: 'v7.0.0',
649+
});
637650
return {
638651
name: this.faker.name.findName(),
639652
username: this.faker.internet.userName(),

src/internal/deprecated.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable jsdoc/check-tag-names */
2+
/* eslint-disable jsdoc/require-param */
3+
4+
/** @internal */
5+
export interface DeprecatedOptions {
6+
deprecated: string;
7+
proposed?: string;
8+
since?: string;
9+
until?: string;
10+
}
11+
12+
/** @internal */
13+
export function deprecated(opts: DeprecatedOptions): void {
14+
let message = `[@faker-js/faker]: ${opts.deprecated} is deprecated`;
15+
16+
if (opts.since) {
17+
message += ` since ${opts.since}`;
18+
}
19+
20+
if (opts.until) {
21+
message += ` and will be removed in ${opts.until}`;
22+
}
23+
24+
if (opts.proposed) {
25+
message += `. Please use ${opts.proposed} instead`;
26+
}
27+
28+
console.warn(message + '.');
29+
}

src/lorem.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,19 @@ export class Lorem {
175175
* // Dolor tempora iusto.'
176176
*/
177177
text(): string {
178-
const loremMethods = [
179-
'lorem.word',
180-
'lorem.words',
181-
'lorem.sentence',
182-
'lorem.sentences',
183-
'lorem.paragraph',
184-
'lorem.paragraphs',
185-
'lorem.lines',
178+
const methods: Array<keyof Lorem> = [
179+
'word',
180+
'words',
181+
'sentence',
182+
'sentences',
183+
'paragraph',
184+
'paragraphs',
185+
'lines',
186186
];
187-
const randomLoremMethod = this.faker.random.arrayElement(loremMethods);
188-
return this.faker.fake(`{{${randomLoremMethod}}}`);
187+
188+
const method = this.faker.random.arrayElement(methods);
189+
190+
return `${this[method]()}`;
189191
}
190192

191193
/**

0 commit comments

Comments
 (0)