From 2a801a4ff1e6bb7c906f88543a49afce338b745a Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 04:25:40 +1200 Subject: [PATCH 01/13] Add `.range()` method Signed-off-by: Richie Bendall --- index.d.ts | 18 ++++++++++++++++++ index.js | 3 +++ index.test-d.ts | 3 +++ package.json | 3 +++ readme.md | 6 +++++- test.js | 6 ++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 02ebd0e..3f917db 100644 --- a/index.d.ts +++ b/index.d.ts @@ -55,6 +55,24 @@ type Delay = { } ): delay.ClearablePromise; + /** + Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. + + @param minimum - Minimum amount of milliseconds to delay the promise. + @param maximum - Maximum amount of milliseconds to delay the promise. + @returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed. + */ + range( + minimum: number, + maximum: number, + options?: delay.Options & { + /** + Value to resolve in the returned promise. + */ + value: T; + } + ): delay.ClearablePromise; + /** Create a promise which rejects after the specified `milliseconds`. diff --git a/index.js b/index.js index 557bdbc..6188feb 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ 'use strict'; +const randomInteger = require('random-int'); + const createAbortError = () => { const error = new Error('Delay aborted'); error.name = 'AbortError'; @@ -56,6 +58,7 @@ const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve}) const delay = createDelay({willResolve: true}); delay.reject = createDelay({willResolve: false}); +delay.range = (minimum, maximum, options) => delay(randomInteger(minimum, maximum), options); delay.createWithTimers = ({clearTimeout, setTimeout}) => { const delay = createDelay({clearTimeout, setTimeout, willResolve: true}); delay.reject = createDelay({clearTimeout, setTimeout, willResolve: false}); diff --git a/index.test-d.ts b/index.test-d.ts index 3999c2f..1b54f8e 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -11,6 +11,8 @@ expectType>( delay(200, {signal: new AbortController().signal}) ); +expectType>(delay.range(50, 200, { value: 0 })); + expectType>(delay.reject(200, {value: '🦄'})); expectType>(delay.reject(200, {value: 0})); @@ -22,3 +24,4 @@ expectType>(customDelay(200, {value: 0})); expectType>(customDelay.reject(200, {value: '🦄'})); expectType>(customDelay.reject(200, {value: 0})); + diff --git a/package.json b/package.json index 83786bd..340ae98 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,9 @@ "promises", "bluebird" ], + "dependencies": { + "random-int": "^2.0.1" + }, "devDependencies": { "abort-controller": "^3.0.0", "ava": "1.4.1", diff --git a/readme.md b/readme.md index 3d62fba..a1e2a8d 100644 --- a/readme.md +++ b/readme.md @@ -36,7 +36,11 @@ Create a promise which resolves after the specified `milliseconds`. Create a promise which rejects after the specified `milliseconds`. -#### milliseconds +### delay.range(minimum, maximum, [options]) + +Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. + +#### milliseconds (minimum and maximum for delay.range) Type: `number` diff --git a/test.js b/test.js index ed1b298..fec6caa 100644 --- a/test.js +++ b/test.js @@ -163,3 +163,9 @@ test('can create a new instance with fixed timeout methods', async t => { t.is(cleared.length, 1); t.is(cleared[0], callbacks[2].handle); }); + +test('returns a promise that is resolved in a random range of time', async t => { + const end = timeSpan(); + await m.range(50, 150); + t.true(inRange(end(), 30, 170), 'is delayed'); +}); From b49512158d2afde8c9a868c65c2b84c2bc3f8464 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 04:27:07 +1200 Subject: [PATCH 02/13] Remove extra newline --- index.test-d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/index.test-d.ts b/index.test-d.ts index 1b54f8e..3327693 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -24,4 +24,3 @@ expectType>(customDelay(200, {value: 0})); expectType>(customDelay.reject(200, {value: '🦄'})); expectType>(customDelay.reject(200, {value: 0})); - From ae15386c624f2198033f8f8f00f802c24a82a688 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 04:27:46 +1200 Subject: [PATCH 03/13] Remove extra newline --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index 6188feb..9a75313 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ 'use strict'; - const randomInteger = require('random-int'); const createAbortError = () => { From 48df9428b73b5f4d377cda8a56091d3116de7c37 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 17 Jul 2020 05:05:09 +0800 Subject: [PATCH 04/13] Update index.test-d.ts --- index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test-d.ts b/index.test-d.ts index 3327693..d41cc2b 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -11,7 +11,7 @@ expectType>( delay(200, {signal: new AbortController().signal}) ); -expectType>(delay.range(50, 200, { value: 0 })); +expectType>(delay.range(50, 200, {value: 0})); expectType>(delay.reject(200, {value: '🦄'})); expectType>(delay.reject(200, {value: 0})); From ee8eb4a0f8cdb7700acd3cbac30fce2f1430cc03 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 14:43:27 +1200 Subject: [PATCH 05/13] Inline `random-int` Signed-off-by: Richie Bendall --- index.js | 3 ++- package.json | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6188feb..1d0c01c 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ 'use strict'; -const randomInteger = require('random-int'); +// https://github.com/sindresorhus/random-int/blob/c37741b56f76b9160b0b63dae4e9c64875128146/index.js#L13-L15 +const randomInteger = (minimum, maximum) => Math.floor((Math.random() * (maximum - minimum + 1)) + minimum); const createAbortError = () => { const error = new Error('Delay aborted'); diff --git a/package.json b/package.json index 340ae98..5f17219 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,7 @@ "promises", "bluebird" ], - "dependencies": { - "random-int": "^2.0.1" - }, + "dependencies": {}, "devDependencies": { "abort-controller": "^3.0.0", "ava": "1.4.1", From 86c1e4e1d9d96a9c59250b05bb339c60db8d149b Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 14:43:59 +1200 Subject: [PATCH 06/13] Update readme.md Co-authored-by: Sindre Sorhus --- readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a1e2a8d..8589e60 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,9 @@ Create a promise which rejects after the specified `milliseconds`. Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. -#### milliseconds (minimum and maximum for delay.range) +#### milliseconds +#### mininum +#### maximum Type: `number` From a9ef6fbdff212ce72e0604f77ed721ed55661e79 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Fri, 17 Jul 2020 14:47:21 +1200 Subject: [PATCH 07/13] Elaborate on use case Signed-off-by: Richie Bendall --- index.d.ts | 2 ++ readme.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/index.d.ts b/index.d.ts index 3f917db..0970255 100644 --- a/index.d.ts +++ b/index.d.ts @@ -58,6 +58,8 @@ type Delay = { /** Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. + Useful for testing and web scraping. + @param minimum - Minimum amount of milliseconds to delay the promise. @param maximum - Maximum amount of milliseconds to delay the promise. @returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed. diff --git a/readme.md b/readme.md index 8589e60..d9202af 100644 --- a/readme.md +++ b/readme.md @@ -40,6 +40,8 @@ Create a promise which rejects after the specified `milliseconds`. Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. +Useful for testing and web scraping. + #### milliseconds #### mininum #### maximum From d1f5cb2b39000129f60f77e43b2580415b7179e5 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 17 Jul 2020 23:09:34 +0800 Subject: [PATCH 08/13] Update package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 5f17219..83786bd 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "promises", "bluebird" ], - "dependencies": {}, "devDependencies": { "abort-controller": "^3.0.0", "ava": "1.4.1", From 81d01f9e64dbb1e0c033b8434d070e2a27355b0e Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 18 Jul 2020 13:23:38 +1200 Subject: [PATCH 09/13] Explain usefulness Signed-off-by: Richie Bendall --- index.d.ts | 2 +- readme.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0970255..41f4538 100644 --- a/index.d.ts +++ b/index.d.ts @@ -58,7 +58,7 @@ type Delay = { /** Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. - Useful for testing and web scraping. + Useful for tests and scraping since they can have unpredictable speed. For example, if you have a test that asserts a method should not take longer than a certain amount, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. @param minimum - Minimum amount of milliseconds to delay the promise. @param maximum - Maximum amount of milliseconds to delay the promise. diff --git a/readme.md b/readme.md index d9202af..0955f0b 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,7 @@ Create a promise which rejects after the specified `milliseconds`. Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. -Useful for testing and web scraping. +Useful for tests and scraping since they can have unpredictable speed. For example, if you have a test that asserts a method should not take longer than a certain amount, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. #### milliseconds #### mininum From 64ebf1d9377e1b58eaa45ddc849c3122b8f62644 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Sat, 18 Jul 2020 13:25:09 +1200 Subject: [PATCH 10/13] Add related tags Signed-off-by: Richie Bendall --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 83786bd..f7e03ee 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,10 @@ "async", "await", "promises", - "bluebird" + "bluebird", + "threshold", + "range", + "random" ], "devDependencies": { "abort-controller": "^3.0.0", From 554a2262fb98ca4127d98d667911332f41b7ff21 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 18 Jul 2020 21:53:15 +0800 Subject: [PATCH 11/13] Update index.d.ts --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 41f4538..8b45222 100644 --- a/index.d.ts +++ b/index.d.ts @@ -58,7 +58,7 @@ type Delay = { /** Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. - Useful for tests and scraping since they can have unpredictable speed. For example, if you have a test that asserts a method should not take longer than a certain amount, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. + Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. @param minimum - Minimum amount of milliseconds to delay the promise. @param maximum - Maximum amount of milliseconds to delay the promise. From 6a8600193c2da6aedeec022892981c3145a7e95b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 18 Jul 2020 21:53:36 +0800 Subject: [PATCH 12/13] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1d0c01c..b863595 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -// https://github.com/sindresorhus/random-int/blob/c37741b56f76b9160b0b63dae4e9c64875128146/index.js#L13-L15 +// From https://github.com/sindresorhus/random-int/blob/c37741b56f76b9160b0b63dae4e9c64875128146/index.js#L13-L15 const randomInteger = (minimum, maximum) => Math.floor((Math.random() * (maximum - minimum + 1)) + minimum); const createAbortError = () => { From fc7663af0064b62d6785d694a32b7d0642d94d22 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 18 Jul 2020 21:54:20 +0800 Subject: [PATCH 13/13] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 0955f0b..9a55098 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,7 @@ Create a promise which rejects after the specified `milliseconds`. Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed. -Useful for tests and scraping since they can have unpredictable speed. For example, if you have a test that asserts a method should not take longer than a certain amount, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. +Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead. #### milliseconds #### mininum