node-powertools
Advanced tools
Comparing version 0.0.22 to 0.0.23
@@ -304,11 +304,11 @@ (function (root, factory) { | ||
self.queue = []; | ||
self.list = []; | ||
self.running = false; | ||
} | ||
FunctionQueue.prototype.enqueue = function (fn) { | ||
FunctionQueue.prototype.add = function (fn) { | ||
var self = this; | ||
return new Promise(function (resolve, reject) { | ||
self.queue.push({ | ||
self.list.push({ | ||
function: fn, | ||
@@ -327,3 +327,3 @@ resolve: resolve, | ||
return new Promise(function (resolve, reject) { | ||
if (self.running || !self.queue.length) { | ||
if (self.running || !self.list.length) { | ||
return resolve(); | ||
@@ -333,3 +333,3 @@ } | ||
self.running = true; | ||
var current = self.queue.shift(); | ||
var current = self.list.shift(); | ||
@@ -336,0 +336,0 @@ current |
{ | ||
"name": "node-powertools", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"description": "Powerful assistive functions for Node and Browser environments.", | ||
@@ -33,2 +33,2 @@ "main": "dist/index.js", | ||
} | ||
} | ||
} |
@@ -59,3 +59,2 @@ <p align="center"> | ||
### powertools.random(min, max, options) | ||
Generate a random number between two numbers `min` and `max`. You can use `options` to supply a sign or randomize the sign as well. If an array is supplied, a random element from the array is returned. | ||
@@ -71,3 +70,2 @@ The default `options.mode` is `uniform` but you can also supply `gaussian` which will generate random values on a gaussian bell curve. | ||
### powertools.arrayify(input) | ||
Transform the `input` into an array if it is not already. | ||
@@ -80,3 +78,2 @@ ```js | ||
### powertools.wait(time) | ||
Asynchronously wait for the specified `time` in milliseconds. | ||
@@ -88,3 +85,2 @@ ```js | ||
### powertools.poll(fn, options) | ||
Asynchronously wait for the specified `fn` to return `true`. You can use `options` to supply a polling interval and timeout in milliseconds. The promise **rejects** if the timeout is reached. | ||
@@ -98,4 +94,25 @@ ```js | ||
### powertools.queue() | ||
Returns a `Queue` which you can run `.add(fn)` where `fn` is an Asynchronous function. The queue will process the functions in FIFO (first in, first out) order and will only process the next async function after the one before it resolves or rejects. | ||
```js | ||
// Call this function every 100 ms until it returns true or 30000 ms passes | ||
const queue = powertools.queue() | ||
// Queue the first function | ||
queue.add(async () => { | ||
console.log('Queue 1 started'); | ||
await powertools.wait(1000) | ||
console.log('Queue 1 finished'); | ||
}) | ||
// Queue the second function | ||
// This will only begin executing after the first function completes | ||
queue.add(async () => { | ||
console.log('Queue 2 started'); | ||
await powertools.wait(1000) | ||
console.log('Queue 2 finished'); | ||
}) | ||
``` | ||
### powertools.escape(str) | ||
Add the escape character `\` before any character in `str` that needs to be escaped for a `RegExp`. | ||
@@ -110,3 +127,2 @@ ```js | ||
### powertools.regexify(str) | ||
Revive a `str` into a `RegExp`. Supports flags. Depending on how you want special characters to be treated, you can use `powertools.escape(str)` prior to using `powertools.regexify(str)`. | ||
@@ -124,3 +140,2 @@ ```js | ||
### powertools.timestamp(date, options) | ||
Convert a `date` to a timestamp in 3 formats: an ISO `string`, a UNIX `number`, or a plain-ol' JS `Date` (as specified in `options`). | ||
@@ -139,3 +154,2 @@ The first argument `date` can be a JS `Date`, a UNIX timestamp `number`, or a `string` that will be parsed by the `new Date()` method. | ||
### powertools.force(value, type, options) | ||
Intelligently converts a `value` to a `type` how JavaScript **should**. The acceptable types are `string`, `number`, `boolean`, `array`. | ||
@@ -142,0 +156,0 @@ ```js |
@@ -304,11 +304,11 @@ (function (root, factory) { | ||
self.queue = []; | ||
self.list = []; | ||
self.running = false; | ||
} | ||
FunctionQueue.prototype.enqueue = function (fn) { | ||
FunctionQueue.prototype.add = function (fn) { | ||
var self = this; | ||
return new Promise(function (resolve, reject) { | ||
self.queue.push({ | ||
self.list.push({ | ||
function: fn, | ||
@@ -327,3 +327,3 @@ resolve: resolve, | ||
return new Promise(function (resolve, reject) { | ||
if (self.running || !self.queue.length) { | ||
if (self.running || !self.list.length) { | ||
return resolve(); | ||
@@ -333,3 +333,3 @@ } | ||
self.running = true; | ||
var current = self.queue.shift(); | ||
var current = self.list.shift(); | ||
@@ -336,0 +336,0 @@ current |
@@ -21,16 +21,23 @@ const package = require('../package.json'); | ||
// describe('.queue()', () => { | ||
describe('.queue()', () => { | ||
// describe('queue', () => { | ||
// // Normal | ||
// it('object (one key) => array (one key)', () => { | ||
// return assert.deepEqual(powertools.getKeys({name: 'ian'}), ['name']); | ||
// }); | ||
// it('object (one key + nested) => array (one key, nested)', () => { | ||
// return assert.deepEqual(powertools.getKeys({name: 'ian', favorites: {color: 'red'}}), ['name', 'favorites.color']); | ||
// }); | ||
// }); | ||
const queue = powertools.queue(); | ||
// }); | ||
queue.add(() => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve('Hello, World!'); | ||
}, 1000); | ||
}); | ||
}); | ||
describe('queue', () => { | ||
// Normal | ||
it('add to queue', () => { | ||
return queue.list === 1; | ||
}); | ||
}); | ||
}); | ||
describe('.getKeys()', () => { | ||
@@ -37,0 +44,0 @@ |
39379
837
184