try-require
Advanced tools
Sorry, the diff of this file is not supported yet
| "use strict"; | ||
| var assert = require( 'assert' ); | ||
| var tryRequire = require( '../index' ); | ||
| describe( 'try-require', function() { | ||
| it( 'should return an object for async module', function() { | ||
| assert.equal( typeof tryRequire( 'async' ), 'object' ); | ||
| } ); | ||
| it( 'should return null for last error', function() { | ||
| assert.equal( tryRequire.lastError(), null ); | ||
| } ); | ||
| it( 'should return path for async module', function() { | ||
| assert.equal( typeof tryRequire.resolve( 'async' ), 'string' ); | ||
| } ); | ||
| it( 'should still return null for last error', function() { | ||
| assert.equal( tryRequire.lastError(), null ); | ||
| } ); | ||
| it( 'should return undefined for request module', function() { | ||
| assert.equal( typeof tryRequire( 'request' ), 'undefined' ); | ||
| } ); | ||
| it( 'should have set last error', function() { | ||
| assert.deepEqual( tryRequire.lastError(), { code: "MODULE_NOT_FOUND" } ); | ||
| } ); | ||
| it( 'should return undefined path for request module', function() { | ||
| assert.equal( typeof tryRequire.resolve( 'request' ), 'undefined' ); | ||
| } ); | ||
| it( 'should have set last error', function() { | ||
| assert.deepEqual( tryRequire.lastError(), { code: "MODULE_NOT_FOUND" } ); | ||
| } ); | ||
| it( 'should return null for last error again after success call', function() { | ||
| tryRequire( 'async' ); // the success | ||
| assert.strictEqual( tryRequire.lastError(), null ); | ||
| } ); | ||
| } ); |
+20
-10
| 'use strict'; | ||
| var tryRequire = function tryRequire(id, req) { | ||
| var lastError = null; | ||
| var tryRequire = function tryRequire( id, req ) { | ||
| var path; | ||
@@ -8,9 +10,11 @@ var _req = req || require; | ||
| try { | ||
| path = _req.resolve(id); | ||
| } catch (e) { | ||
| this.error = e; | ||
| path = _req.resolve( id ); | ||
| lastError = null; | ||
| } catch ( e ) { | ||
| lastError = e; | ||
| } | ||
| if (path) { | ||
| return _req(path); | ||
| if ( path ) { | ||
| return _req( path ); | ||
| } | ||
@@ -21,3 +25,3 @@ | ||
| var resolve = function tryRequireResolve(id, req) { | ||
| var resolve = function tryRequireResolve( id, req ) { | ||
| var path; | ||
@@ -27,5 +31,7 @@ var _req = req || require; | ||
| try { | ||
| path = _req.resolve(id); | ||
| } catch (e) { | ||
| this.error = e; | ||
| path = _req.resolve( id ); | ||
| lastError = null; | ||
| } catch ( e ) { | ||
| lastError = e; | ||
| } | ||
@@ -37,2 +43,6 @@ | ||
| tryRequire.resolve = resolve; | ||
| tryRequire.lastError = function() { | ||
| return lastError; | ||
| }; | ||
| module.exports = tryRequire; |
+8
-1
| { | ||
| "name": "try-require", | ||
| "version": "1.1.0", | ||
| "version": "1.2.1", | ||
| "description": "Conditional load modules.", | ||
@@ -11,4 +11,11 @@ "repository": { | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "mocha": "2.2.5", | ||
| "async": "1.0.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "npm update; node_modules/mocha/bin/mocha -b -u tdd --reporter list test/*.test.js" | ||
| }, | ||
| "license": "MIT", | ||
| "main": "./index.js" | ||
| } |
+29
-0
@@ -41,7 +41,36 @@ try-require | ||
| Optionally, check require and resolution exceptions with lastError. Note that | ||
| lastError will return null if no error has ever been triggered, or if the most | ||
| recent call to require or resolve was successful. | ||
| ```javascript | ||
| var tryRequire = require('try-require'); | ||
| var maybe = tryRequire('notAModule'); | ||
| console.error( tryRequire.lastError() ); | ||
| ``` | ||
| Note that both tryRequire and tryRequire.resolve accept an optional second | ||
| argument if you want to provide your own version of require. | ||
| # Contribute | ||
| If you would like to add to this library, please ensure that all existing test | ||
| cases pass and that all new code has proper test coverage in test/all.test.js. | ||
| To run tests, simply execute: | ||
| ```bash | ||
| npm test | ||
| ``` | ||
| Also, match styles within the project where language of the file allows. Some | ||
| core styles to follow are: | ||
| - indent lines with 4 spaces | ||
| - spaces around parameters in function definitions and calls | ||
| - opening/closing brackets on same line | ||
| # License | ||
| MIT |
5338
140.45%5
66.67%64
146.15%76
61.7%2
Infinity%