Socialify

Folder ..

Viewing unique.ts
51 lines (46 loc) • 1.3 KB

 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
import * as uniqueExec from './vendor/unique';

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);
    }
  }

  /**
   * unique
   *
   * @method unique
   */
  unique<Method extends (args: Args) => string, Args extends any[]>(
    method: Method,
    args: Args,
    opts?: {
      startTime?: number;
      maxTime?: number;
      maxRetries?: number;
      currentIterations?: number;
      exclude?: string | string[];
      compare?: (obj: Record<string, string>, key: string) => 0 | -1;
    }
  ): string {
    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);
  }
}