diff options
| author | Shinigami <[email protected]> | 2024-02-24 10:38:56 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-24 10:38:56 +0100 |
| commit | 4382fd9313821bf542829859e97d4422acc9e284 (patch) | |
| tree | 3cc116f2a2e1c7225b68211a8fa42521c6b70ed9 /src | |
| parent | fd05126fda62cd8b8d16f28ec46af1e66d4e8823 (diff) | |
| download | faker-4382fd9313821bf542829859e97d4422acc9e284.tar.xz faker-4382fd9313821bf542829859e97d4422acc9e284.zip | |
refactor(helpers)!: remove v8 deprecated unique (#2661)
Co-authored-by: Eric Cheng <[email protected]>
Co-authored-by: Matt Mayer <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/helpers/index.ts | 118 | ||||
| -rw-r--r-- | src/modules/helpers/unique.ts | 152 |
2 files changed, 0 insertions, 270 deletions
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 36f050e8..a42da85f 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -4,8 +4,6 @@ import { deprecated } from '../../internal/deprecated'; import { SimpleModuleBase } from '../../internal/module-base'; import { fakeEval } from './eval'; import { luhnCheckValue } from './luhn-check'; -import type { RecordKey } from './unique'; -import * as uniqueExec from './unique'; /** * Returns a number based on given RegEx-based quantifier symbol or quantifier values. @@ -205,14 +203,6 @@ export function legacyReplaceSymbolWithNumber( */ export class SimpleHelpersModule extends SimpleModuleBase { /** - * Global store of unique values. - * This means that faker should *never* return duplicate values across all API methods when using `faker.helpers.unique` without passing `options.store`. - * - * @internal - */ - private readonly uniqueStore: Record<RecordKey, RecordKey> = {}; - - /** * Slugifies the given string. * For that all spaces (` `) are replaced by hyphens (`-`) * and most non word characters except for dots and hyphens will be removed. @@ -1130,114 +1120,6 @@ export class SimpleHelpersModule extends SimpleModuleBase { } /** - * Generates a unique result using the results of the given method. - * Used unique entries will be stored internally and filtered from subsequent calls. - * - * @template TMethod 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. Defaults to `[]`. - * @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. - * - * @see https://github.com/faker-js/faker/issues/1785#issuecomment-1407773744 - * - * @example - * faker.helpers.unique(faker.person.firstName) // 'Corbin' - * - * @since 7.5.0 - * - * @deprecated Please find a dedicated npm package instead, or even create one on your own if you want to. - * More info can be found in issue [faker-js/faker #1785](https://github.com/faker-js/faker/issues/1785). - */ - unique< - TMethod extends ( - // TODO @Shinigami92 2023-02-14: This `any` type can be fixed by anyone if they want to. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ...parameters: any[] - ) => RecordKey, - >( - method: TMethod, - args: Parameters<TMethod> = [] as unknown as Parameters<TMethod>, - options: { - /** - * This parameter does nothing. - * - * @default new Date().getTime() - */ - startTime?: number; - /** - * The time in milliseconds this method may take before throwing an error. - * - * @default 50 - */ - maxTime?: number; - /** - * The total number of attempts to try before throwing an error. - * - * @default 50 - */ - maxRetries?: number; - /** - * This parameter does nothing. - * - * @default 0 - */ - currentIterations?: number; - /** - * The value or values that should be excluded/skipped. - * - * @default [] - */ - exclude?: RecordKey | RecordKey[]; - /** - * The function used to determine whether a value was already returned. - * - * Defaults to check the existence of the key. - * - * @default (obj, key) => (obj[key] === undefined ? -1 : 0) - */ - compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; - /** - * The store of unique entries. - * - * Defaults to a global store. - */ - store?: Record<RecordKey, RecordKey>; - } = {} - ): ReturnType<TMethod> { - deprecated({ - deprecated: 'faker.helpers.unique', - proposed: - 'https://github.com/faker-js/faker/issues/1785#issuecomment-1407773744', - since: '8.0', - until: '9.0', - }); - - const { - maxTime = 50, - maxRetries = 50, - exclude = [], - store = this.uniqueStore, - } = options; - return uniqueExec.exec(method, args, { - ...options, - startTime: Date.now(), - maxTime, - maxRetries, - currentIterations: 0, - exclude, - store, - }); - } - - /** * Generates an array containing values returned by the given method. * * @template TResult The type of elements. diff --git a/src/modules/helpers/unique.ts b/src/modules/helpers/unique.ts deleted file mode 100644 index 20805530..00000000 --- a/src/modules/helpers/unique.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { FakerError } from '../../errors/faker-error'; - -export type RecordKey = string | number | symbol; - -/** - * Uniqueness compare function. - * Default behavior is to check value as key against object hash. - * - * @param obj The object to check. - * @param key The key to check. - */ -function defaultCompare( - obj: Record<RecordKey, RecordKey>, - key: RecordKey -): 0 | -1 { - if (obj[key] === undefined) { - return -1; - } - - return 0; -} - -/** - * Logs the given code as an error and throws it. - * Also logs a message for helping the user. - * - * @param startTime The time the execution started. - * @param now The current time. - * @param code The error code. - * @param store The store of unique entries. - * @param currentIterations Current iteration or retries of `unique.exec` (current loop depth). - * - * @throws The given error code with additional text. - */ -function errorMessage( - startTime: number, - now: number, - code: string, - store: Record<RecordKey, RecordKey>, - currentIterations: number -): never { - console.error('Error', code); - console.log( - `Found ${Object.keys(store).length} unique entries before throwing error. -retried: ${currentIterations} -total time: ${now - startTime}ms` - ); - throw new FakerError( - `${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.helpers.unique().` - ); -} - -/** - * Generates a unique result using the results of the given method. - * Used unique entries will be stored internally and filtered from subsequent calls. - * - * @template TMethod 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 The time this execution stared. Defaults to `new Date().getTime()`. - * @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 The current attempt. Defaults to `0`. - * @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 `GLOBAL_UNIQUE_STORE`. - */ -export function exec< - TMethod extends ( - // TODO @Shinigami92 2023-02-14: This `any` type can be fixed by anyone if they want to. - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ...parameters: any[] - ) => RecordKey, ->( - method: TMethod, - args: Parameters<TMethod>, - 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<TMethod> { - const now = Date.now(); - - const { - startTime = Date.now(), - maxTime = 50, - maxRetries = 50, - currentIterations = 0, - compare = defaultCompare, - store = {}, - } = options; - let { exclude = [] } = options; - options.currentIterations = currentIterations; - - // Support single exclude argument as string - if (!Array.isArray(exclude)) { - exclude = [exclude]; - } - - // If out of time -> throw error. - if (now - startTime >= maxTime) { - return errorMessage( - startTime, - now, - `Exceeded maxTime: ${maxTime}`, - store, - currentIterations - ); - } - - // If out of retries -> throw error. - if (currentIterations >= maxRetries) { - return errorMessage( - startTime, - now, - `Exceeded maxRetries: ${maxRetries}`, - store, - currentIterations - ); - } - - // Execute the provided method to find a potential satisfied value. - const result: ReturnType<TMethod> = method(...args) as ReturnType<TMethod>; - - // If the result has not been previously found, add it to the found array and return the value as it's unique. - if (compare(store, result) === -1 && !exclude.includes(result)) { - store[result] = result; - options.currentIterations = 0; - return result; - } - - // Conflict, try again. - options.currentIterations++; - return exec(method, args, { - ...options, - startTime, - maxTime, - maxRetries, - compare, - exclude, - }); -} |
