time-to-botec

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

README.md (3087B)


      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 # tryRequire
     22 
     23 > Wrap `require` in a try/catch block.
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <!-- Package usage documentation. -->
     34 
     35 <section class="usage">
     36 
     37 ## Usage
     38 
     39 ```javascript
     40 var tryRequire = require( '@stdlib/utils/try-require' );
     41 ```
     42 
     43 #### tryRequire( id )
     44 
     45 Wraps `require` in a `try/catch` block. If able to resolve a module `id`, the function returns the value bound to `module.exports` in the resolved module. Otherwise, the function returns an `Error`.
     46 
     47 ```javascript
     48 var x = tryRequire( 'beep' );
     49 
     50 if ( x instanceof Error ) {
     51     console.log( x.message );
     52 }
     53 ```
     54 
     55 </section>
     56 
     57 <!-- /.usage -->
     58 
     59 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     60 
     61 <section class="notes">
     62 
     63 ## Notes
     64 
     65 -   Use caution when attempting to resolve a relative path or a local module. This function attempts to resolve a module from its current path. Thus, the function is **unable** to resolve anything which is not along its search path. For local requires, use an absolute file path.
     66 
     67     ```javascript
     68     var resolve = require( 'path' ).resolve;
     69 
     70     var out = tryRequire( resolve( '/foo/bar/baz', '..', 'lib', 'beep.js' ) );
     71 
     72     if ( out instanceof Error ) {
     73         console.error( out.message );
     74     }
     75     ```
     76 
     77 </section>
     78 
     79 <!-- /.notes -->
     80 
     81 <!-- Package usage examples. -->
     82 
     83 <section class="examples">
     84 
     85 ## Examples
     86 
     87 <!-- eslint no-undef: "error" -->
     88 
     89 ```javascript
     90 var tryRequire = require( '@stdlib/utils/try-require' );
     91 
     92 var out;
     93 
     94 out = tryRequire( '_abcdefghijklmnopqrstuvwxyz123456789_' );
     95 if ( out instanceof Error ) {
     96     console.error( out.message );
     97 } else {
     98     throw new Error( 'expected an error' );
     99 }
    100 
    101 out = tryRequire( '@stdlib/utils/try-require' );
    102 if ( out !== tryRequire ) {
    103     throw new Error( 'expected export' );
    104 }
    105 ```
    106 
    107 </section>
    108 
    109 <!-- /.examples -->
    110 
    111 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    112 
    113 <section class="references">
    114 
    115 </section>
    116 
    117 <!-- /.references -->
    118 
    119 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    120 
    121 <section class="links">
    122 
    123 </section>
    124 
    125 <!-- /.links -->