Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
52 changes: 52 additions & 0 deletions lib/node_modules/@stdlib/utils/async/any-by/internal/instrument.js
Original file line number Diff line number Diff line change
@@ -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;
77 changes: 52 additions & 25 deletions lib/node_modules/@stdlib/utils/async/any-by/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
* }
* }
*
Expand All @@ -73,25 +64,22 @@ 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 ) {
* if ( error ) {
* 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;
Expand Down Expand Up @@ -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 );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
});
98 changes: 98 additions & 0 deletions lib/node_modules/@stdlib/utils/async/any-by/test/test.status.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,}/
Expand Down