time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

async.js (4002B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2021 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var resolve = require( 'path' ).resolve;
     24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     25 var isFunction = require( '@stdlib/assert/is-function' );
     26 var cwd = require( '@stdlib/process/cwd' );
     27 var exists = require( './../../exists' );
     28 var validate = require( './validate.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Asynchronously resolves a path according to a predicate function by walking parent directories.
     35 *
     36 * @param {string} path - path to resolve
     37 * @param {Options} [options] - function options
     38 * @param {string} [options.dir] - base directory
     39 * @param {Function} predicate - callback which tests whether a resolved path passes a test
     40 * @param {Function} clbk - callback to invoke after resolving a path
     41 * @throws {TypeError} first argument must be a string
     42 * @throws {TypeError} predicate function argument must be a function
     43 * @throws {TypeError} callback argument must be a function
     44 * @throws {TypeError} options argument must be an object
     45 * @throws {TypeError} must provide valid options
     46 *
     47 * @example
     48 * resolveParentPathBy( 'package.json', predicate, onPath );
     49 *
     50 * function predicate( path, next ) {
     51 *     next( null, true );
     52 * }
     53 *
     54 * function onPath( error, path ) {
     55 *     if ( error ) {
     56 *         throw error;
     57 *     }
     58 *     console.log( path );
     59 * }
     60 */
     61 function resolveParentPathBy( path, options, predicate, clbk ) {
     62 	var spath;
     63 	var child;
     64 	var test;
     65 	var opts;
     66 	var done;
     67 	var dir;
     68 	var err;
     69 	if ( !isString( path ) ) {
     70 		throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + path + '`.' );
     71 	}
     72 	opts = {};
     73 	if ( arguments.length > 3 ) {
     74 		err = validate( opts, options );
     75 		if ( err ) {
     76 			throw err;
     77 		}
     78 		test = predicate;
     79 		done = clbk;
     80 	} else {
     81 		test = options;
     82 		done = predicate;
     83 	}
     84 	if ( !isFunction( test ) ) {
     85 		throw new TypeError( 'invalid argument. Predicate function argument must be a function. Value: `' + test + '`.' );
     86 	}
     87 	if ( !isFunction( done ) ) {
     88 		throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + done + '`.' );
     89 	}
     90 	if ( opts.dir ) {
     91 		dir = resolve( cwd(), opts.dir );
     92 	} else {
     93 		dir = cwd();
     94 	}
     95 	spath = resolve( dir, path );
     96 	exists( spath, onExists );
     97 
     98 	/**
     99 	* Resolves the next candidate path.
    100 	*
    101 	* @private
    102 	* @returns {void}
    103 	*/
    104 	function next() {
    105 		// Resolve a parent directory:
    106 		child = dir;
    107 		dir = resolve( dir, '..' );
    108 
    109 		// If we have already reached root, we cannot resolve any higher directories...
    110 		if ( child === dir ) {
    111 			return done( null, null );
    112 		}
    113 		// Resolve the next search path:
    114 		spath = resolve( dir, path );
    115 		exists( spath, onExists );
    116 	}
    117 
    118 	/**
    119 	* Callback invoked after checking for path existence.
    120 	*
    121 	* @private
    122 	* @param {(Error|null)} error - error object
    123 	* @param {boolean} bool - boolean indicating if a path exists
    124 	* @returns {void}
    125 	*/
    126 	function onExists( error, bool ) { // eslint-disable-line handle-callback-err
    127 		if ( bool ) {
    128 			return test( spath, onTest );
    129 		}
    130 		next();
    131 	}
    132 
    133 	/**
    134 	* Callback invoked after testing a resolved path.
    135 	*
    136 	* @private
    137 	* @param {(Error|null)} error - error object
    138 	* @param {boolean} bool - boolean indicating if a path exists
    139 	* @returns {void}
    140 	*/
    141 	function onTest( error, bool ) {
    142 		if ( error ) {
    143 			return done( error );
    144 		}
    145 		if ( bool ) {
    146 			return done( null, spath );
    147 		}
    148 		next();
    149 	}
    150 }
    151 
    152 
    153 // EXPORTS //
    154 
    155 module.exports = resolveParentPathBy;