From 14df7d3f70b54c4a153f5dcf111ef90575bbbe9e Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Thu, 10 Feb 2022 23:52:14 +0100 Subject: fix: fix unique method types (#457) --- src/unique.ts | 13 +++++++------ src/vendor/unique.ts | 27 +++++++++++++++------------ 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/unique.ts b/src/unique.ts index 3084da4b..62402df0 100644 --- a/src/unique.ts +++ b/src/unique.ts @@ -1,4 +1,5 @@ import * as uniqueExec from './vendor/unique'; +import type { RecordKey } from './vendor/unique'; /** * Module to generate unique entries. @@ -38,20 +39,20 @@ export class Unique { * @param opts.compare The function used to determine whether a value was already returned. * * @example - * faker.unique(faker.name.firstName()) + * faker.unique(faker.name.firstName) */ - unique string, Args extends any[]>( + unique RecordKey>( method: Method, - args: Args, + args?: Parameters, opts?: { startTime?: number; maxTime?: number; maxRetries?: number; currentIterations?: number; - exclude?: string | string[]; - compare?: (obj: Record, key: string) => 0 | -1; + exclude?: RecordKey | RecordKey[]; + compare?: (obj: Record, key: RecordKey) => 0 | -1; } - ): string { + ): ReturnType { opts = opts || {}; opts.startTime = new Date().getTime(); if (typeof opts.maxTime !== 'number') { diff --git a/src/vendor/unique.ts b/src/vendor/unique.ts index 31056b03..0d6e6317 100644 --- a/src/vendor/unique.ts +++ b/src/vendor/unique.ts @@ -1,19 +1,24 @@ +export type RecordKey = string | number | symbol; + // global results store // currently uniqueness is global to entire faker instance // this means that faker should currently *never* return duplicate values across all API methods when using `Faker.unique` // it's possible in the future that some users may want to scope found per function call instead of faker instance -const found: Record = {}; +const found: Record = {}; // global exclude list of results // defaults to nothing excluded -const exclude: string[] = []; +const exclude: RecordKey[] = []; // current iteration or retries of unique.exec ( current loop depth ) const currentIterations = 0; // uniqueness compare function // default behavior is to check value as key against object hash -function defaultCompare(obj: T, key: Key): 0 | -1 { +function defaultCompare( + obj: Record, + key: RecordKey +): 0 | -1 { if (typeof obj[key] === 'undefined') { return -1; } @@ -42,20 +47,18 @@ function errorMessage( ); } -// TODO @Shinigami92 2022-01-24: We should investigate deeper into the types -// Especially the `opts.compare` parameter and `Result` type -export function exec string, Args extends any[]>( +export function exec RecordKey>( method: Method, - args: Args, + args: Parameters, opts: { maxTime?: number; maxRetries?: number; - exclude?: string | string[]; - compare?: (obj: Record, key: string) => 0 | -1; + exclude?: RecordKey | RecordKey[]; + compare?: (obj: Record, key: RecordKey) => 0 | -1; currentIterations?: number; startTime?: number; } -): string { +): ReturnType { const now = new Date().getTime(); opts = opts || {}; @@ -75,7 +78,7 @@ export function exec string, Args extends any[]>( const startTime = opts.startTime; // support single exclude argument as string - if (typeof opts.exclude === 'string') { + if (!Array.isArray(opts.exclude)) { opts.exclude = [opts.exclude]; } @@ -103,7 +106,7 @@ export function exec string, Args extends any[]>( } // execute the provided method to find a potential satisfied value - const result: string = method.apply(this, args); + const result: ReturnType = method.apply(this, args); // if the result has not been previously found, add it to the found array and return the value as it's unique if ( -- cgit v1.2.3