Skip to content

Commit 86580d8

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate unique (#128)
1 parent 46d51ba commit 86580d8

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Phone } from './phone_number';
1616
import { Random } from './random';
1717
import { System } from './system';
1818
import { Time } from './time';
19+
import { Unique } from './unique';
1920
import { Word } from './word';
2021

2122
export interface FakerOptions {
@@ -168,7 +169,7 @@ export class Faker {
168169
seedValue?: any[] | any;
169170

170171
readonly fake: Fake['fake'] = new Fake(this).fake;
171-
readonly unique = new (require('./unique'))(this).unique;
172+
readonly unique: Unique['unique'] = new Unique().unique;
172173

173174
readonly mersenne: Mersenne = new Mersenne();
174175
random: Random = new Random(this);

src/unique.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const uniqueExec = require('../vendor/unique');
2+
3+
export class Unique {
4+
// maximum time unique.exec will attempt to run before aborting
5+
maxTime: number = 10;
6+
7+
// maximum retries unique.exec will recurse before aborting ( max loop depth )
8+
maxRetries: number = 10;
9+
10+
// time the script started
11+
// startTime: number = 0;
12+
13+
constructor() {
14+
// Bind `this` so namespaced is working correctly
15+
for (const name of Object.getOwnPropertyNames(Unique.prototype)) {
16+
if (name === 'constructor' || typeof this[name] !== 'function') {
17+
continue;
18+
}
19+
this[name] = this[name].bind(this);
20+
}
21+
}
22+
23+
/**
24+
* unique
25+
*
26+
* @method unique
27+
*/
28+
unique(
29+
method: any,
30+
args: any,
31+
opts?: {
32+
startTime?: number;
33+
maxTime?: number;
34+
maxRetries?: number;
35+
currentIterations?: number;
36+
[key: string]: any;
37+
}
38+
): any {
39+
opts ||= {};
40+
opts.startTime = new Date().getTime();
41+
if (typeof opts.maxTime !== 'number') {
42+
opts.maxTime = this.maxTime;
43+
}
44+
if (typeof opts.maxRetries !== 'number') {
45+
opts.maxRetries = this.maxRetries;
46+
}
47+
opts.currentIterations = 0;
48+
return uniqueExec.exec(method, args, opts);
49+
}
50+
}

0 commit comments

Comments
 (0)