Comparing version 0.2.0 to 0.3.0
@@ -19,6 +19,8 @@ declare namespace contextor { | ||
* @param {*} defaultValue - The default value to return in case. | ||
* @param {boolean} allowUndefinedContext - Whether or not to allow undefined context, | ||
* default false. | ||
* @returns {*} The value or default value for missing key. | ||
* @throws {ReferenceError} On missing value for given key in current context. | ||
*/ | ||
get: (key: string, defaultValue: any) => any; | ||
get: (key: string, defaultValue: any, allowUndefinedContext: boolean = false) => any; | ||
} | ||
@@ -25,0 +27,0 @@ } |
21
index.js
@@ -138,10 +138,27 @@ 'use strict'; | ||
* @param {*} defaultValue - The default value to return in case. | ||
* @param {boolean} allowUndefinedContext - Whether or not to allow undefined context, | ||
* default false. | ||
* @returns {*} The value or default value for missing key. | ||
* @throws {ReferenceError} On missing value for given key in current context. | ||
*/ | ||
exports.get = function get(key, defaultValue) { | ||
const context = retrieveCurrentContext(); | ||
exports.get = function get(key, defaultValue, allowUndefinedContext = false) { | ||
let context; | ||
let previousError; | ||
try { | ||
context = retrieveCurrentContext(); | ||
} catch (error) { | ||
if (!allowUndefinedContext || !/^No current context found/.test(error.message)) { | ||
throw error; | ||
} | ||
previousError = error; | ||
context = {}; | ||
} | ||
const exists = key in context; | ||
if (!exists && defaultValue === undefined) { | ||
if (previousError) { | ||
throw previousError; | ||
} | ||
throw new ReferenceError(`No value found in context for '${key}' key`); | ||
@@ -148,0 +165,0 @@ } |
{ | ||
"name": "contextor", | ||
"description": "Package allowing to pass a context along an asynchronous process", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"author": "Thomas Prelot <tprelot@gmail.com> (https://github.com/Gnucki)", | ||
@@ -32,3 +32,5 @@ "contributors": [], | ||
"test-debug": "NODE_ENV='test' mocha --recursive --full-trace --check-leaks index.js test.js", | ||
"test-watch": "NODE_ENV='test' mocha -w -b --recursive index.js test.js" | ||
"test-watch": "NODE_ENV='test' mocha -w -b --recursive index.js test.js", | ||
"preversion": "npm run check", | ||
"postversion": "git push && git push --tags" | ||
}, | ||
@@ -35,0 +37,0 @@ "bin": {}, |
15
test.js
@@ -67,2 +67,17 @@ 'use strict'; | ||
it('should failed to get a value on a missing context if allowed but no default value is given', (done) => { | ||
executeInSpecificAsyncContext(() => { | ||
expect(() => contextor.get('foo', undefined, true)).to.throw( | ||
ReferenceError, | ||
'No current context found; use \'create\' method to create one' | ||
); | ||
}, done); | ||
}); | ||
it('should succeed to get a value on a missing context if allowed and a default value is given', (done) => { | ||
executeInSpecificAsyncContext(() => { | ||
expect(contextor.get('foo', 'bar', true)).to.equal('bar'); | ||
}, done); | ||
}); | ||
it('should pass context along asynchronous resource chain', (done) => { | ||
@@ -69,0 +84,0 @@ const multiDone = buildMultiDone(done); |
16397
271