New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

contextor

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contextor - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

4

index.d.ts

@@ -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 @@ }

@@ -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": {},

@@ -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);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc