File tree 2 files changed +52
-1
lines changed 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import { Phone } from './phone_number';
16
16
import { Random } from './random' ;
17
17
import { System } from './system' ;
18
18
import { Time } from './time' ;
19
+ import { Unique } from './unique' ;
19
20
import { Word } from './word' ;
20
21
21
22
export interface FakerOptions {
@@ -168,7 +169,7 @@ export class Faker {
168
169
seedValue ?: any [ ] | any ;
169
170
170
171
readonly fake : Fake [ 'fake' ] = new Fake ( this ) . fake ;
171
- readonly unique = new ( require ( './unique' ) ) ( this ) . unique ;
172
+ readonly unique : Unique [ 'unique' ] = new Unique ( ) . unique ;
172
173
173
174
readonly mersenne : Mersenne = new Mersenne ( ) ;
174
175
random : Random = new Random ( this ) ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments