time-to-botec

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

sync.js (2762B)


      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' ).sync;
     28 var validate = require( './validate.js' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Synchronously 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 * @throws {TypeError} first argument must be a string
     41 * @throws {TypeError} options argument must be an object
     42 * @throws {TypeError} must provide valid options
     43 * @throws {TypeError} last argument must be a function
     44 * @returns {(string|null)} resolved path or null
     45 *
     46 * @example
     47 * function predicate() {
     48 *     return true;
     49 * }
     50 *
     51 * var path = resolveParentPathBy( 'package.json', predicate );
     52 * // e.g., returns '...'
     53 */
     54 function resolveParentPathBy( path, options, predicate ) {
     55 	var spath;
     56 	var child;
     57 	var test;
     58 	var opts;
     59 	var dir;
     60 	var err;
     61 	if ( !isString( path ) ) {
     62 		throw new TypeError( 'invalid argument. First argument must be a string primitive. Value: `' + path + '`.' );
     63 	}
     64 	opts = {};
     65 	if ( arguments.length > 2 ) {
     66 		err = validate( opts, options );
     67 		if ( err ) {
     68 			throw err;
     69 		}
     70 		test = predicate;
     71 	} else {
     72 		test = options;
     73 	}
     74 	if ( !isFunction( test ) ) {
     75 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `' + test + '`.' );
     76 	}
     77 	if ( opts.dir ) {
     78 		dir = resolve( cwd(), opts.dir );
     79 	} else {
     80 		dir = cwd();
     81 	}
     82 	// Start at a base directory and continue moving up through each parent directory until able to resolve a search path or until reaching the root directory...
     83 	while ( child !== dir ) {
     84 		spath = resolve( dir, path );
     85 		if ( exists( spath ) && test( spath ) ) {
     86 			return spath;
     87 		}
     88 		child = dir;
     89 		dir = resolve( dir, '..' );
     90 	}
     91 	return null;
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = resolveParentPathBy;