time-to-botec

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

sync.js (2419B)


      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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
     24 var isObject = require( '@stdlib/assert/is-plain-object' );
     25 var readFile = require( './../../read-file' ).sync;
     26 var removeBOM = require( '@stdlib/string/remove-utf8-bom' );
     27 var parseJSON = require( '@stdlib/utils/parse-json' );
     28 var instanceOf = require( '@stdlib/assert/instance-of' );
     29 
     30 
     31 // MAIN //
     32 
     33 /**
     34 * Synchronously reads a file as JSON.
     35 *
     36 * @param {(string|Buffer|integer)} file - file path or file descriptor
     37 * @param {(Options|string)} [options] - options
     38 * @param {(string|null)} [options.encoding] - file encoding
     39 * @param {string} [options.flag] - file status flag
     40 * @param {Function} [options.reviver] - JSON reviver
     41 * @throws {TypeError} options argument must be either a string or an object
     42 * @returns {(JSON|Error)} JSON or an error
     43 *
     44 * @example
     45 * var resolve = require( 'path' ).resolve;
     46 * var instanceOf = require( '@stdlib/assert/instance-of' );
     47 *
     48 * var out = readJSONSync( resolve( __dirname, '..', 'package.json' ) );
     49 * if ( instanceOf( out, Error ) ) {
     50 *     throw out;
     51 * }
     52 * console.dir( out );
     53 */
     54 function readJSONSync( file, options ) {
     55 	var opts;
     56 	var f;
     57 	if ( arguments.length > 1 ) {
     58 		if ( isString( options ) ) {
     59 			opts = {
     60 				'encoding': options
     61 			};
     62 		} else {
     63 			if ( !isObject( options ) ) {
     64 				throw new TypeError( 'invalid argument. Options argument must be either a string or an object. Value: `' + options + '`.' );
     65 			}
     66 			opts = options;
     67 		}
     68 	} else {
     69 		opts = {};
     70 	}
     71 	f = readFile( file, opts );
     72 	if ( instanceOf( f, Error ) ) {
     73 		return f;
     74 	}
     75 	f = f.toString();
     76 	if ( opts.encoding === 'utf8' ) {
     77 		f = removeBOM( f );
     78 	}
     79 	if ( opts.reviver ) {
     80 		return parseJSON( f, opts.reviver );
     81 	}
     82 	return parseJSON( f );
     83 }
     84 
     85 
     86 // EXPORTS //
     87 
     88 module.exports = readJSONSync;