diff --git a/lib/node_modules/@stdlib/assert/is-negative-zero/benchmark/benchmark.js b/lib/node_modules/@stdlib/assert/is-negative-zero/benchmark/benchmark.js index f1f29385cc05..3841b6921b58 100644 --- a/lib/node_modules/@stdlib/assert/is-negative-zero/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/assert/is-negative-zero/benchmark/benchmark.js @@ -16,7 +16,7 @@ * limitations under the License. */ -/* eslint-disable no-new-wrappers, no-undefined, no-empty-function */ +/* eslint-disable no-undefined, no-empty-function */ 'use strict'; diff --git a/lib/node_modules/@stdlib/utils/async/any-by/internal/instrument.js b/lib/node_modules/@stdlib/utils/async/any-by/internal/instrument.js new file mode 100644 index 000000000000..d6c6bd9ceba8 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/async/any-by/internal/instrument.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +// MAIN // + +/** +* Creates a high-resolution timer to measure async operations. +* +* @returns {Function} function to stop the timer and return the duration in milliseconds +*/ +function createTimer() { + var start = process.hrtime(); + + return stop; + + /** + * Stops the timer and returns the elapsed time. + * + * @private + * @returns {number} elapsed time in milliseconds + */ + function stop() { + var diff = process.hrtime( start ); + + // Convert to milliseconds: + return ( diff[ 0 ] * 1e3 ) + ( diff[ 1 ] / 1e6 ); + } +} + + +// EXPORTS // + +module.exports = createTimer; diff --git a/lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js b/lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js index 192ba1923c98..281c0de4b93a 100644 --- a/lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js +++ b/lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js @@ -49,19 +49,10 @@ var limit = require( './limit.js' ); * @returns {Function} function which invokes the predicate function once for each element in a collection * * @example -* var readFile = require( '@stdlib/fs/read-file' ); -* -* function predicate( file, next ) { -* var opts = { -* 'encoding': 'utf8' -* }; -* readFile( file, opts, onFile ); -* -* function onFile( error ) { -* if ( error ) { -* return next( null, false ); -* } -* next( null, true ); +* function predicate( value, index, next ) { +* setTimeout( onTimeout, value ); +* function onTimeout() { +* next( null, false ); * } * } * @@ -73,10 +64,7 @@ var limit = require( './limit.js' ); * var anyByAsync = factory( opts, predicate ); * * // Create a collection over which to iterate: -* var files = [ -* './beep.js', -* './boop.js' -* ]; +* var arr = [ 3000, 2500, 1000 ]; * * // Define a callback which handles results: * function done( error, bool ) { @@ -84,14 +72,14 @@ var limit = require( './limit.js' ); * throw error; * } * if ( bool ) { -* console.log( 'Successfully read at least one file.' ); +* console.log( 'At least one element passed.' ); * } else { -* console.log( 'Unable to read any files.' ); +* console.log( 'No elements passed.' ); * } * } * -* // Try to read each element in `files`: -* anyByAsync( files, done ); +* // Try to invoke the predicate for each element in `arr`: +* anyByAsync( arr, done ); */ function factory( options, predicate ) { var opts; @@ -129,26 +117,65 @@ function factory( options, predicate ) { * @returns {void} */ function anyByAsync( collection, done ) { + var status; + if ( !isCollection( collection ) ) { throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', collection ) ); } if ( !isFunction( done ) ) { throw new TypeError( format( 'invalid argument. Last argument must be a function. Value: `%s`.', done ) ); } - return limit( collection, opts, f, clbk ); + status = { + 'status': 'pending', + 'ndone': 0, + 'nerrors': 0 + }; + + // Wrap the predicate to track progress: + limit( collection, opts, wrapper, clbk ); + + return status; + + /** + * Wraps the predicate function to track the number of completed invocations. + * + * @private + * @param {*} value - collection element + * @param {NonNegativeInteger} index - element index + * @param {Callback} next - callback to invoke upon predicate completion + * @returns {void} + */ + function wrapper( value, index, next ) { + f( value, index, onResult ); + + /** + * Callback invoked upon predicate completion. + * + * @private + * @param {(Error|null)} err - error argument + * @param {*} result - predicate result + * @returns {void} + */ + function onResult( err, result ) { + status.ndone += 1; + next( err, result ); + } + } /** - * Callback invoked upon completion. + * Callback invoked upon completion of all predicate invocations. * * @private - * @param {*} [error] - error - * @param {boolean} bool - test result + * @param {(Error|null)} error - error argument + * @param {boolean} bool - whether at least one element passed the predicate * @returns {void} */ function clbk( error, bool ) { if ( error ) { + status.status = 'fail'; return done( error, false ); } + status.status = 'ok'; done( null, bool ); } } diff --git a/lib/node_modules/@stdlib/utils/async/any-by/test/test.instrument.js b/lib/node_modules/@stdlib/utils/async/any-by/test/test.instrument.js new file mode 100644 index 000000000000..92959ca85017 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/async/any-by/test/test.instrument.js @@ -0,0 +1,55 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var createTimer = require( './../internal/instrument.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof createTimer, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a function', function test( t ) { + var stop = createTimer(); + + t.strictEqual( typeof stop, 'function', 'returns a function' ); + t.end(); +}); + +tape( 'the returned function returns a number (elapsed milliseconds)', function test( t ) { + var duration; + var stop = createTimer(); + + setTimeout( onTimeout, 10 ); + + function onTimeout() { + duration = stop(); + + t.strictEqual( typeof duration, 'number', 'returns a number' ); + t.ok( duration >= 0.0, 'returns a non-negative number' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/utils/async/any-by/test/test.status.js b/lib/node_modules/@stdlib/utils/async/any-by/test/test.status.js new file mode 100644 index 000000000000..bca155c07819 --- /dev/null +++ b/lib/node_modules/@stdlib/utils/async/any-by/test/test.status.js @@ -0,0 +1,98 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// cspell:words ndone + +// MODULES // + +var tape = require( 'tape' ); +var factory = require( './../lib/factory.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.ok( typeof factory === 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a status object that tracks progress (short-circuit)', function test( t ) { + var anyByAsync; + var files; + var op; + + anyByAsync = factory( predicate ); + files = [ './file1.js', './file2.js' ]; + op = anyByAsync( files, done ); + + // Verify the status object immediately after invocation: + t.equal( typeof op, 'object', 'returns an object' ); + t.equal( op.status, 'pending', 'initial status is pending' ); + t.equal( op.ndone, 0, 'initial ndone is 0' ); + + function predicate( value, next ) { + setTimeout( onTimeout, 0 ); + + function onTimeout() { + next( null, true ); + } + } + + function done( err, result ) { + if ( err ) { + t.fail( err.message ); + return t.end(); + } + t.equal( result, true, 'returns expected result' ); + t.equal( op.status, 'ok', 'status is ok upon completion' ); + + // Short-circuit: only one file was processed: + t.equal( op.ndone, 1, 'short-circuited after first match' ); + t.end(); + } +}); + +tape( 'the function returns a status object that tracks a full scan', function test( t ) { + var files; + var op; + + files = [ './file1.js', './file2.js' ]; + op = factory( predicate )( files, done ); + + function predicate( value, next ) { + setTimeout( onTimeout, 50 ); + + function onTimeout() { + // Always false, so all files must be checked: + next( null, false ); + } + } + + function done( err, result ) { + if ( err ) { + t.fail( err.message ); + return t.end(); + } + t.equal( result, false, 'returns expected result' ); + t.equal( op.ndone, 2, 'all operations completed for a full scan' ); + t.end(); + } +}); diff --git a/lib/node_modules/@stdlib/utils/regexp-from-string/examples/index.js b/lib/node_modules/@stdlib/utils/regexp-from-string/examples/index.js index 3e9a3d37cac8..ff10935fd82f 100644 --- a/lib/node_modules/@stdlib/utils/regexp-from-string/examples/index.js +++ b/lib/node_modules/@stdlib/utils/regexp-from-string/examples/index.js @@ -26,8 +26,8 @@ console.log( reFromString( '/beep/' ) ); console.log( reFromString( '/[A-Z]*/' ) ); // => /[A-Z]*/ -console.log( reFromString( '/\\\\\\\//ig' ) ); // eslint-disable-line no-useless-escape -// => /\\\//ig +console.log( reFromString( '/\\\\\\\//gi' ) ); // eslint-disable-line no-useless-escape +// => /\\\//gi console.log( reFromString( '/[A-Z]{0,}/' ) ); // => /[A-Z]{0,}/