Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sniff

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sniff - npm Package Compare versions

Comparing version 0.0.1 to 0.1.2

.travis.yml

24

package.json

@@ -5,17 +5,16 @@ {

"author": "Damon Oehlman <damon.oehlman@sidelab.com>",
"tags": [ "tool", "type", "reflection" ],
"version": "0.0.1",
"tags": [
"tool",
"type",
"reflection"
],
"version": "0.1.2",
"main": "sniff.js",
"engines": {
"node": ">= 0.4.x < 0.7.0"
},
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"chai": "0.3.x"
},
"repository": {

@@ -28,6 +27,9 @@ "type": "git",

},
"scripts": {
"test": "mocha --reporter spec"
},
"contributors": [
"Angus Croll"
]
}
],
"optionalDependencies": {}
}

@@ -10,2 +10,42 @@ # sniff

As always, feedback would be greatly appreciated.
## Detecting Function Signature Helper
A small helper has been added to sniff to assist with the process of determining the function signature that a function has been called with. This code was written to help with those cases where you have a function that might be called with a variety of different combinations and permutations.
In simple cases, while you might do something like the following:
```js
function example(name, opts, callback) {
// if (typeof opts == 'function') {
callback = opts;
opts = {};
}
// rest of your function here
}
```
This can get tiresome and somewhat error prone when you have more complicated functions. This is where the `sniff.args` helper can come in handy. For example, this next function is designed to be called with just a name, or a name and an age, or name, age and callback, etc, etc. While writing a function like this would probably generally be discouraged in JS every now and again they are needed.
Anyway, let's take a look:
```js
function example(name, age, opts, callback) {
// first analyse the arguments and get a matcher function
var matchSig = sniff.args(arguments);
// check for the name, age and callback case
if (matchSig('string', 'number|string', 'function')) {
// remap opts
callback = opts;
opts = {};
}
// or the name, opts, function case
else if (matchSig('string', 'object', 'function')) {
// remap age, opts, and the callback
callback = opts,
opts = age;
age = null;
}
}
```
(function (glob) {
var rePipeDelim = /\s*\|\s*/;
// toType function from http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/

@@ -35,3 +37,38 @@ // follow @angusTweets for more useful stuff like this...

/**
## sniff.args(arguments)
Return a function matcher that can be used to determine if a function has a function signature
matching a particular type.
*/
sniff.args = function(args) {
var types = [], ii;
// ensure that args is an array
args = Array.prototype.slice.call(args);
// iterate through the args and detect the type of each argument
for (ii = args.length; ii--; ) {
types[ii] = sniff(args[ii]);
}
return function() {
var match = arguments.length === types.length;
// iterate through the types and for a match against the argument
for (ii = types.length; match && ii--; ) {
// split the argument on the pipe character
var okTypes = arguments[ii].split(rePipeDelim);
match = false;
for (var jj = okTypes.length; jj--; ) {
match = match || types[ii] === okTypes[jj];
}
}
return match;
};
};
(typeof module != "undefined" && module.exports) ? (module.exports = sniff) : (glob.eve = sniff);
})(this);

@@ -28,6 +28,3 @@ var sniff = require('../sniff'),

expect(sniff(CustomPonger)).to.equal('prototype');
var test = new CustomPonger;
test.ping();
});
});
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