aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLeyla Jähnig <[email protected]>2022-08-29 13:10:45 +0200
committerGitHub <[email protected]>2022-08-29 11:10:45 +0000
commit7f8b8716ba69e24800e26d6c072c3076c01bfccf (patch)
tree909b28019e2d461ba5257ac91ea98ca1220b9a34 /src
parentc2108fa5db889bb1455a5735934776bcf91fabac (diff)
downloadfaker-7f8b8716ba69e24800e26d6c072c3076c01bfccf.tar.xz
faker-7f8b8716ba69e24800e26d6c072c3076c01bfccf.zip
refactor(unique): move to helpers (#1298)
Diffstat (limited to 'src')
-rw-r--r--src/faker.ts2
-rw-r--r--src/modules/helpers/index.ts44
-rw-r--r--src/modules/helpers/unique.ts (renamed from src/modules/unique/unique.ts)4
-rw-r--r--src/modules/unique/index.ts26
4 files changed, 63 insertions, 13 deletions
diff --git a/src/faker.ts b/src/faker.ts
index c84d77a4..9534e373 100644
--- a/src/faker.ts
+++ b/src/faker.ts
@@ -77,7 +77,7 @@ export class Faker {
readonly definitions: LocaleDefinition = this.initDefinitions();
readonly fake: Fake['fake'] = new Fake(this).fake;
- readonly unique: Unique['unique'] = new Unique().unique;
+ readonly unique: Unique['unique'] = new Unique(this).unique;
readonly mersenne: Mersenne = new Mersenne();
readonly random: Random = new Random(this);
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index fac7e3e2..5228c605 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -2,6 +2,8 @@ import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { luhnCheckValue } from './luhn-check';
+import type { RecordKey } from './unique';
+import * as uniqueExec from './unique';
/**
* Module with various helper methods that transform the method input rather than returning values from locales.
@@ -590,4 +592,46 @@ export class Helpers {
// return the response recursively until we are done finding all tags
return this.fake(res);
}
+
+ /**
+ * Generates a unique result using the results of the given method.
+ * Used unique entries will be stored internally and filtered from subsequent calls.
+ *
+ * @template Method The type of the method to execute.
+ * @param method The method used to generate the values.
+ * @param args The arguments used to call the method.
+ * @param options The optional options used to configure this method.
+ * @param options.startTime This parameter does nothing.
+ * @param options.maxTime The time in milliseconds this method may take before throwing an error. Defaults to `50`.
+ * @param options.maxRetries The total number of attempts to try before throwing an error. Defaults to `50`.
+ * @param options.currentIterations This parameter does nothing.
+ * @param options.exclude The value or values that should be excluded/skipped. Defaults to `[]`.
+ * @param options.compare The function used to determine whether a value was already returned. Defaults to check the existence of the key.
+ * @param options.store The store of unique entries. Defaults to a global store.
+ *
+ * @example
+ * faker.helpers.unique(faker.name.firstName) // 'Corbin'
+ */
+ unique<Method extends (...parameters) => RecordKey>(
+ method: Method,
+ args?: Parameters<Method>,
+ options: {
+ startTime?: number;
+ maxTime?: number;
+ maxRetries?: number;
+ currentIterations?: number;
+ exclude?: RecordKey | RecordKey[];
+ compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1;
+ store?: Record<RecordKey, RecordKey>;
+ } = {}
+ ): ReturnType<Method> {
+ const { maxTime = 50, maxRetries = 50 } = options;
+ return uniqueExec.exec(method, args, {
+ ...options,
+ startTime: new Date().getTime(),
+ maxTime,
+ maxRetries,
+ currentIterations: 0,
+ });
+ }
}
diff --git a/src/modules/unique/unique.ts b/src/modules/helpers/unique.ts
index 70408faf..31fddec8 100644
--- a/src/modules/unique/unique.ts
+++ b/src/modules/helpers/unique.ts
@@ -4,7 +4,7 @@ export type RecordKey = string | number | symbol;
/**
* Global store of unique values.
- * This means that faker should *never* return duplicate values across all API methods when using `Faker.unique` without passing `options.store`.
+ * This means that faker should *never* return duplicate values across all API methods when using `Faker.helpers.unique` without passing `options.store`.
*/
const GLOBAL_UNIQUE_STORE: Record<RecordKey, RecordKey> = {};
@@ -60,7 +60,7 @@ total time: ${now - startTime}ms`
`${code} for uniqueness check.
May not be able to generate any more unique values with current settings.
-Try adjusting maxTime or maxRetries parameters for faker.unique().`
+Try adjusting maxTime or maxRetries parameters for faker.helpers.unique().`
);
}
diff --git a/src/modules/unique/index.ts b/src/modules/unique/index.ts
index d77004e4..6c02d6d8 100644
--- a/src/modules/unique/index.ts
+++ b/src/modules/unique/index.ts
@@ -1,11 +1,14 @@
-import type { RecordKey } from './unique';
-import * as uniqueExec from './unique';
+import type { Faker } from '../..';
+import { deprecated } from '../../internal/deprecated';
+import type { RecordKey } from '../helpers/unique';
/**
* Module to generate unique entries.
+ *
+ * @deprecated
*/
export class Unique {
- constructor() {
+ constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(Unique.prototype)) {
if (
@@ -36,8 +39,12 @@ export class Unique {
* @param options.compare The function used to determine whether a value was already returned. Defaults to check the existence of the key.
* @param options.store The store of unique entries. Defaults to a global store.
*
+ * @see faker.helpers.unique()
+ *
* @example
* faker.unique(faker.name.firstName) // 'Corbin'
+ *
+ * @deprecated Use faker.helpers.unique() instead.
*/
unique<Method extends (...parameters) => RecordKey>(
method: Method,
@@ -52,13 +59,12 @@ export class Unique {
store?: Record<RecordKey, RecordKey>;
} = {}
): ReturnType<Method> {
- const { maxTime = 50, maxRetries = 50 } = options;
- return uniqueExec.exec(method, args, {
- ...options,
- startTime: new Date().getTime(),
- maxTime,
- maxRetries,
- currentIterations: 0,
+ deprecated({
+ deprecated: 'faker.unique()',
+ proposed: 'faker.helpers.unique()',
+ since: '7.5',
+ until: '8.0',
});
+ return this.faker.helpers.unique(method, args, options);
}
}