
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
add some sanity to loosely typed javascript option parameters.
this module simply pulls in a options object (from a function parameter) and returns a "cleaned up" version bassed on a declaration made in the code itself. check out the examples below.
passing in an object literal containing parameters is super popular in all the favorite javascript libraries. it's super cool.
however, there is a downside in that, unless the code is well documented, it's hard to keep track of all the possible parameters passed through. this solves that issue by making the code self-documenting. (this also lets client look at your source and understand what they need to pass in without much effort)
npm install --save opt-param
opt = optparam(opt, parameterDeclaration);
opt: the option parameter to be usedparameterDeclaration: an object whose keys are declared parameter names and whose values are objects declaring information about a parameter (as below in examples)type value is basically the instanceof value as a string, to lower case.required = false) and not present, the output object will still defined that key, but set the value explicitly to undefinedoptparam call will be dropped from the outputrequired: true will assume they are optionalvar optparam = require('opt-param');
var doAThing = function(opt) {
// parse and modify the option parameter
opt = optparam(opt, {
w: {
// basic object type
type: 'number',
required: true
},
h: {
type: 'number',
required: true
}
/*
* we can also declare it as such:
* w: 'required number',
* h: 'required number',
* name: 'string', // optional string parameter
* opt: 'required' //required "whatever" parameter
*/
});
// lets see what it did
console.log('out:', opt);
console.log('w=' + opt.w + ', h=' + opt.h);
}
doAThing({
w: 12,
h: 34,
somethingExtra: 'this will be dropped'
});
/*
* outputs:
* out: { w: 12, h: 34 }
* w=12, h=34
*/
doAThing({
w: '12', // coerce to a 'number' type
h: 34
});
/*
* outputs:
* out: { w: 12, h: 34 }
* w=12, h=34
*/
doAThing({
w: 12
// we're missing a required field!
});
/*
* outputs:
* throws and error!
*/
we can do more stuff (more examples just for the sake of it);
// but wait, there's more!
var doAnotherThing = function(opt) {
// parse and modify the option parameter
opt = optparam(opt, {
arr: {
// we can use arrays!
type: 'array',
required: false
}
});
if (opt.arr) {
console.log(opt.arr.length);
}
}
doAnotherThing(); // no output (opt.arr will be explicitly set to `undefined`)
doAnotherThing([]); // prints: 0
doAnotherThing([1, 2, 3]) // prints: 3
FAQs
extract and assert the existence of an opt parameter
The npm package opt-param receives a total of 10 weekly downloads. As such, opt-param popularity was classified as not popular.
We found that opt-param demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.