Skip to content

Commit

Permalink
feat: migrate unique (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored and damienwebdev committed Jan 14, 2022
1 parent 46d51ba commit 86580d8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -16,6 +16,7 @@ import { Phone } from './phone_number';
import { Random } from './random';
import { System } from './system';
import { Time } from './time';
import { Unique } from './unique';
import { Word } from './word';

export interface FakerOptions {
Expand Down Expand Up @@ -168,7 +169,7 @@ export class Faker {
seedValue?: any[] | any;

readonly fake: Fake['fake'] = new Fake(this).fake;
readonly unique = new (require('./unique'))(this).unique;
readonly unique: Unique['unique'] = new Unique().unique;

readonly mersenne: Mersenne = new Mersenne();
random: Random = new Random(this);
Expand Down
50 changes: 50 additions & 0 deletions src/unique.ts
@@ -0,0 +1,50 @@
const uniqueExec = require('../vendor/unique');

export class Unique {
// maximum time unique.exec will attempt to run before aborting
maxTime: number = 10;

// maximum retries unique.exec will recurse before aborting ( max loop depth )
maxRetries: number = 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: any,
args: any,
opts?: {
startTime?: number;
maxTime?: number;
maxRetries?: number;
currentIterations?: number;
[key: string]: any;
}
): any {
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);
}
}

0 comments on commit 86580d8

Please sign in to comment.