aboutsummaryrefslogtreecommitdiff
path: root/src/unique.ts
blob: 6ef5eee5502c97e53a920924d8bed9d888108191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { RecordKey } from './vendor/unique';
import * as uniqueExec from './vendor/unique';

/**
 * Module to generate unique entries.
 */
export class Unique {
  // maximum time unique.exec will attempt to run before aborting
  maxTime = 10;

  // maximum retries unique.exec will recurse before aborting ( max loop depth )
  maxRetries = 10;

  // time the script started
  // startTime: number = 0;

  constructor() {
    // Bind `this` so namespaced is working correctly
    for (const name of Object.getOwnPropertyNames(Unique.prototype)) {
      if (name === 'constructor' || typeof this[name] !== 'function') {
        continue;
      }
      this[name] = this[name].bind(this);
    }
  }

  /**
   * 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 opts The optional options used to configure this method.
   * @param opts.startTime The time this execution stared. This will be ignored/overwritten.
   * @param opts.maxTime The time this method may take before throwing an error.
   * @param opts.maxRetries The total number of attempts to try before throwing an error.
   * @param opts.currentIterations The current attempt. This will be ignored/overwritten.
   * @param opts.exclude The value or values that should be excluded/skipped.
   * @param opts.compare The function used to determine whether a value was already returned.
   *
   * @example
   * faker.unique(faker.name.firstName)
   */
  unique<Method extends (...parameters) => RecordKey>(
    method: Method,
    args?: Parameters<Method>,
    opts?: {
      startTime?: number;
      maxTime?: number;
      maxRetries?: number;
      currentIterations?: number;
      exclude?: RecordKey | RecordKey[];
      compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1;
    }
  ): ReturnType<Method> {
    opts = opts || {};
    opts.startTime = new Date().getTime();
    if (typeof opts.maxTime !== 'number') {
      opts.maxTime = this.maxTime;
    }
    if (typeof opts.maxRetries !== 'number') {
      opts.maxRetries = this.maxRetries;
    }
    opts.currentIterations = 0;
    return uniqueExec.exec(method, args, opts);
  }
}