time-to-botec

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

sync.js (2380B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 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 logger = require( 'debug' );
     24 var readFile = require( './../../read-file' ).sync;
     25 var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
     26 
     27 
     28 // VARIABLES //
     29 
     30 var debug = logger( 'read-file-list:sync' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Synchronously reads the entire contents of each file in a file list.
     37 *
     38 * @param {StringArray} list - list of file paths
     39 * @param {(Object|string)} [options] - options
     40 * @param {(string|null)} [options.encoding] - file encoding
     41 * @param {string} [options.flag] - file status flag
     42 * @throws {TypeError} must provide a string array
     43 * @returns {ObjectArray} file contents
     44 *
     45 * @example
     46 * var list = [ __filename ];
     47 * var files = readFileListSync( list );
     48 *
     49 * if ( files instanceof Error ) {
     50 *     throw files;
     51 * }
     52 * console.dir( files );
     53 */
     54 function readFileListSync( list, options ) {
     55 	var results;
     56 	var opts;
     57 	var file;
     58 	var len;
     59 	var i;
     60 
     61 	if ( !isStringArray( list ) ) {
     62 		throw new TypeError( 'invalid argument. First argument must be a string array. Value: `' + list + '`.' );
     63 	}
     64 	if ( arguments.length > 1 ) {
     65 		opts = options;
     66 	} else {
     67 		opts = {};
     68 	}
     69 	len = list.length;
     70 	results = new Array( len );
     71 
     72 	debug( 'Reading %d files...', len );
     73 	for ( i = 0; i < len; i++ ) {
     74 		debug( 'Reading file: %s (%d of %d).', list[ i ], i+1, len );
     75 		file = readFile( list[ i ], opts );
     76 		if ( file instanceof Error ) {
     77 			debug( 'Encountered an error when reading file: %s (%d of %d). Error: %s', list[ i ], i, len, file.message );
     78 			return file;
     79 		}
     80 		debug( 'Successfully read file: %s (%d of %d).', list[ i ], i, len );
     81 		results[ i ] = {
     82 			'file': list[ i ],
     83 			'data': file
     84 		};
     85 	}
     86 	debug( 'Finished reading files.' );
     87 	return results;
     88 }
     89 
     90 
     91 // EXPORTS //
     92 
     93 module.exports = readFileListSync;