Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate unique #128

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}