aboutsummaryrefslogtreecommitdiff
path: root/src/unique.ts
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/unique.ts
parent46d51bac072e1efee0b7c6ddfa4b6aac2a9aa0ee (diff)
downloadfaker-86580d89135bfa0c077d96bb9634a1e47c2f7ea9.tar.xz
faker-86580d89135bfa0c077d96bb9634a1e47c2f7ea9.zip
feat: migrate unique (#128)
Diffstat (limited to 'src/unique.ts')
-rw-r--r--src/unique.ts50
1 files changed, 50 insertions, 0 deletions
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);
+ }
+}