aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-01-14 17:49:24 +0100
committerDamien Retzinger <[email protected]>2022-01-14 18:37:49 -0500
commit86580d89135bfa0c077d96bb9634a1e47c2f7ea9 (patch)
tree00f7387af73e79134453902111be8dfec8628654 /src
parent46d51bac072e1efee0b7c6ddfa4b6aac2a9aa0ee (diff)
downloadfaker-86580d89135bfa0c077d96bb9634a1e47c2f7ea9.tar.xz
faker-86580d89135bfa0c077d96bb9634a1e47c2f7ea9.zip
feat: migrate unique (#128)
Diffstat (limited to 'src')
-rw-r--r--src/index.ts3
-rw-r--r--src/unique.ts50
2 files changed, 52 insertions, 1 deletions
diff --git a/src/index.ts b/src/index.ts
index 98b5f51e..6e5b297d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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 {
@@ -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);
diff --git a/src/unique.ts b/src/unique.ts
new file mode 100644
index 00000000..c077ea58
--- /dev/null
+++ b/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);
+ }
+}