Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Sometimes you want your functions to accept optional arguments. It makes your API nicer.
If you're using this module, feel free to contact me on twitter if you have any questions! :) @rjrodger
Current Version: 0.3.0
Tested on: node 0.10.26
Let's take an example. Say your the basic function signature is:
myAPI.doStuff( 'the-stuff', function( err, result ){ ... } )
But you also want to support options:
myAPI.doStuff( 'the-stuff',
{ option1:'foo', option2:'bar'},
function( err, result ){ ... } )
The callback should be the last argument. That's the style. So you have to write a bit of logic to test if the second argument is an object or a function and act appropriately. Ths is cruft code getting in the way of real work.
myAPI.doStuff = function(){
var stuff = arguments[0]
var options = 'function' == typeof(arguments[1]) ? {} : arguments[1]
var callback = 'function' == typeof(arguments[2]) ? arguments[2] : arguments[1]
...
}
With this module, you specify what you want using a simple expression language:
myAPI.doStuff = function(){
var args = norma('so?f',arguments)
var stuff = args[0]
var options = args[1] || {}
var callback = args[2]
}
Now your arguments always come back in a well-defined array, and always at the same index.
The expression 'so?f' means match: a string, an optional object, and a function
You can also assign names:
myAPI.doStuff = function(){
var args = norma('stuff:s options:o? callback:f',arguments)
// args == {stuff:..., options:..., callback:...}
args.options = args.options || {}
}
And of course, if your function is called with arguments that do not match the expression, then an error is thrown.
If you're using this module, feel free to contact me on twitter if you have any questions! :) @rjrodger
Current Version: 0.2.6
Tested on: node 0.10.26
var norma = require('norma')
function foo() {
var args = norma('sf', arguments)
var content = args[0] // s => string, required
var cb = args[1] // f => function, required
cb(null,content+'!')
}
foo('bar',function(err,out){
console.log(out)
})
npm install norma
The expression you use to define the function argument types that you expect is a string containing individual characters that stand for JavaScript types. Each type is a single character. These are:
You list the types you expect, in the order you expect. The norma module will return an array with each position corresponding to the position of each type letter.
This works like so:
The syntax of the expression is similar to a regular expression (but it's not one!). You can use these special characters:
Now you can do this:
You can use whitespace and commas to make things more readable:
If no match is found for a given position, undefined is used as the placeholder:
Alternates can also be optional, so this works too:
Note that you'll get an undefined as the placeholder, as usual:
You can also give arguments names. These are set as properties on the returned array, as well being assigned an index:
If you really want an object, use the form:
If you use the * modifier, and a name, then you'll get back an array listing all the matches (zero or more).
And that's it!
You can compile a pattern ahead of time:
var norma = require('norma')
var needstring = norma.compile('s')
function foo() {
var args = needstring( arguments )
console.log( 'string:'+args[0] )
}
The parser uses PEG.js to understand the signature expression, and then it builds an internal regular expression to match the function argument types.
The source code is annotated.
Edit norma-parser.pegjs to modify the grammar. Rebuild with npm run build.
Test with:
npm test
FAQs
A function argument organizer
The npm package norma receives a total of 6,457 weekly downloads. As such, norma popularity was classified as popular.
We found that norma demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.