time-to-botec

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

README.md (4024B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # Read File
     22 
     23 > Read the entire contents of a file.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var readFile = require( '@stdlib/fs/read-file' );
     31 ```
     32 
     33 #### readFile( file\[, options], clbk )
     34 
     35 Asynchronously reads the entire contents of a file.
     36 
     37 ```javascript
     38 readFile( __filename, onFile );
     39 
     40 function onFile( error, data ) {
     41     if ( error ) {
     42         throw error;
     43     }
     44     console.log( data );
     45 }
     46 ```
     47 
     48 The function accepts the same `options` and has the same defaults as [`fs.readFile()`][node-fs].
     49 
     50 #### readFile.sync( file\[, options] )
     51 
     52 Synchronously reads the entire contents of a `file`.
     53 
     54 ```javascript
     55 var out = readFile.sync( __filename );
     56 if ( out instanceof Error ) {
     57     throw out;
     58 }
     59 console.log( out );
     60 ```
     61 
     62 The function accepts the same `options` and has the same defaults as [`fs.readFileSync()`][node-fs].
     63 
     64 </section>
     65 
     66 <!-- /.usage -->
     67 
     68 <section class="notes">
     69 
     70 ## Notes
     71 
     72 -   The difference between this API and [`fs.readFileSync()`][node-fs] is that [`fs.readFileSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a non-existent `path`) and this API will return an `error`. Hence, the following anti-pattern
     73 
     74 
     75     ```javascript
     76     var fs = require( 'fs' );
     77 
     78     var file = '/path/to/file.js';
     79 
     80     // Check for existence to prevent an error being thrown...
     81     if ( fs.existsSync( file ) ) {
     82         file = fs.readFileSync( file );
     83     }
     84     ```
     85 
     86     can be replaced by an approach which addresses existence via `error` handling.
     87 
     88     ```javascript
     89     var readFile = require( '@stdlib/fs/read-file' );
     90 
     91     var file = '/path/to/file.js';
     92 
     93     // Explicitly handle the error...
     94     file = readFile.sync( file );
     95     if ( file instanceof Error ) {
     96         // You choose what to do...
     97         console.error( file.message );
     98     }
     99     ```
    100 
    101 </section>
    102 
    103 <!-- /.notes -->
    104 
    105 <section class="examples">
    106 
    107 ## Examples
    108 
    109 <!-- eslint no-undef: "error" -->
    110 
    111 ```javascript
    112 var readFile = require( '@stdlib/fs/read-file' );
    113 
    114 /* Sync */
    115 
    116 var file = readFile.sync( __filename, 'utf8' );
    117 // returns <string>
    118 
    119 console.log( file instanceof Error );
    120 // => false
    121 
    122 file = readFile.sync( 'beepboop', {
    123     'encoding': 'utf8'
    124 });
    125 // returns <Error>
    126 
    127 console.log( file instanceof Error );
    128 // => true
    129 
    130 /* Async */
    131 
    132 readFile( __filename, onFile );
    133 readFile( 'beepboop', onFile );
    134 
    135 function onFile( error, data ) {
    136     if ( error ) {
    137         if ( error.code === 'ENOENT' ) {
    138             console.error( 'File does not exist.' );
    139         } else {
    140             throw error;
    141         }
    142     } else {
    143         console.log( data );
    144     }
    145 }
    146 ```
    147 
    148 </section>
    149 
    150 <!-- /.examples -->
    151 
    152 * * *
    153 
    154 <section class="cli">
    155 
    156 ## CLI
    157 
    158 <section class="usage">
    159 
    160 ### Usage
    161 
    162 ```text
    163 Usage: read-file [options] <filepath>
    164 
    165 Options:
    166 
    167   -h,    --help                Print this message.
    168   -V,    --version             Print the package version.
    169   --enc, --encoding encoding   Encoding.
    170          --flag flag           Flag. Default: 'r'.
    171 ```
    172 
    173 </section>
    174 
    175 <!-- /.usage -->
    176 
    177 <section class="notes">
    178 
    179 ### Notes
    180 
    181 -   Relative file paths are resolved relative to the current working directory.
    182 -   Errors are written to `stderr`.
    183 -   File contents are written to `stdout`.
    184 
    185 </section>
    186 
    187 <!-- /.notes -->
    188 
    189 <section class="examples">
    190 
    191 ### Examples
    192 
    193 ```bash
    194 $ read-file ./README.md
    195 <file_contents>
    196 ```
    197 
    198 </section>
    199 
    200 <!-- /.examples -->
    201 
    202 </section>
    203 
    204 <!-- /.cli -->
    205 
    206 <section class="links">
    207 
    208 [node-fs]: https://nodejs.org/api/fs.html
    209 
    210 </section>
    211 
    212 <!-- /.links -->