Skip to content

Commit 8395e69

Browse files
authored
chore(helpers): move unique store into faker instance (#2072)
1 parent 8cd1965 commit 8395e69

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/modules/helpers/index.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ function getRepetitionsBasedOnQuantifierParameters(
8383
* A number of methods can generate strings according to various patterns: [`replaceSymbols()`](https://next.fakerjs.dev/api/helpers.html#replacesymbols), [`replaceSymbolWithNumber()`](https://next.fakerjs.dev/api/helpers.html#replacesymbolwithnumber), and [`fromRegExp()`](https://next.fakerjs.dev/api/helpers.html#fromregexp).
8484
*/
8585
export class HelpersModule {
86+
/**
87+
* Global store of unique values.
88+
* This means that faker should *never* return duplicate values across all API methods when using `faker.helpers.unique` without passing `options.store`.
89+
*
90+
* @internal
91+
*/
92+
private readonly uniqueStore: Record<RecordKey, RecordKey> = {};
93+
8694
constructor(private readonly faker: Faker) {
8795
// Bind `this` so namespaced is working correctly
8896
for (const name of Object.getOwnPropertyNames(
@@ -1335,13 +1343,20 @@ export class HelpersModule {
13351343
until: '9.0',
13361344
});
13371345

1338-
const { maxTime = 50, maxRetries = 50 } = options;
1346+
const {
1347+
maxTime = 50,
1348+
maxRetries = 50,
1349+
exclude = [],
1350+
store = this.uniqueStore,
1351+
} = options;
13391352
return uniqueExec.exec(method, args, {
13401353
...options,
13411354
startTime: new Date().getTime(),
13421355
maxTime,
13431356
maxRetries,
13441357
currentIterations: 0,
1358+
exclude,
1359+
store,
13451360
});
13461361
}
13471362

src/modules/helpers/unique.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ import { FakerError } from '../../errors/faker-error';
22

33
export type RecordKey = string | number | symbol;
44

5-
/**
6-
* Global store of unique values.
7-
* This means that faker should *never* return duplicate values across all API methods when using `Faker.helpers.unique` without passing `options.store`.
8-
*/
9-
const GLOBAL_UNIQUE_STORE: Record<RecordKey, RecordKey> = {};
10-
11-
/**
12-
* Global exclude list of results.
13-
* Defaults to nothing excluded.
14-
*/
15-
const GLOBAL_UNIQUE_EXCLUDE: RecordKey[] = [];
16-
175
/**
186
* Uniqueness compare function.
197
* Default behavior is to check value as key against object hash.
@@ -108,9 +96,9 @@ export function exec<
10896
maxTime = 50,
10997
maxRetries = 50,
11098
compare = defaultCompare,
111-
store = GLOBAL_UNIQUE_STORE,
99+
store,
112100
} = options;
113-
let { exclude = GLOBAL_UNIQUE_EXCLUDE } = options;
101+
let { exclude } = options;
114102
options.currentIterations = options.currentIterations ?? 0;
115103

116104
// Support single exclude argument as string

0 commit comments

Comments
 (0)