Comparing version 0.0.0 to 0.1.0
{ | ||
"name": "quack", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "Check the values of passed arguments in a function - see if they quack like a duck.", | ||
"main": "quack.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha should -w -G -r should test/unminified-test.js", | ||
"test-min": "npm run build; mocha test/minified-test.js", | ||
"prepublish": "npm test-min", | ||
"build": "./node_modules/uglify-js/bin/uglifyjs quack.js -m -o quack.min.js" | ||
}, | ||
@@ -22,3 +25,8 @@ "repository": { | ||
"author": "glen@screenrev.com", | ||
"license": "BSD" | ||
"license": "BSD", | ||
"devDependencies": { | ||
"mocha": "~1.9.0", | ||
"uglify-js": "~2.2.5", | ||
"should": "~1.2.2" | ||
} | ||
} |
55
quack.js
@@ -13,14 +13,27 @@ /* | ||
check the arguments against the expected signature | ||
quack('string, number, array', 'one', 2, [3]); // true | ||
@param {string|string[]} signature: string (for one), or array of strings to check against | ||
@param {arguments|array|string} args: array-like arguments (or array of arguments) to check | ||
@param {string|string[]} signature: string (for one), or array of strings to check against | ||
@returns {boolean} true if the arguments match the signature | ||
*/ | ||
var quack = function quack(args, signature){ | ||
// convert strings to arrays | ||
if (typeof signature === 'string') signature = [signature]; | ||
if (typeof args === 'string') args = [args]; | ||
var quack = function quack(signature, args){ | ||
// convert array-like "arguments" to an array | ||
if (isArguments(args)) args = [].slice.apply(args); | ||
args = [].slice.apply(args); | ||
// convert signature to an array | ||
if (typeof signature == 'string') { | ||
// trim whitespace and split on commas (with or without spaces) | ||
signature = signature.replace(/^\s*|\s*$/g, '').split(/\s*,\s*/); | ||
} | ||
// iterate over the signature, matching types against arguments | ||
for (var i = 0, max = signature.length; i < max; i++) { | ||
if (typeof signature[i] !== args[i].toLowerCase()) return false; | ||
var sig = signature[i].toLowerCase(); | ||
var arg = args[i]; | ||
if (sig == 'object') return isObject(arg); | ||
else if (sig == 'array') return isArray(arg); | ||
else if (sig !== typeof arg) return false; | ||
} | ||
@@ -31,3 +44,29 @@ | ||
// module definitions | ||
/* | ||
Type checking | ||
*/ | ||
var objectTypes = { | ||
'function': true, | ||
'object': true | ||
}; | ||
function isArguments(value){ | ||
return toString.call(value) == '[object Arguments]'; | ||
} | ||
function isObject(value) { | ||
return value ? !! objectTypes[typeof value] : false; | ||
} | ||
function isArray(value){ | ||
return value ? (typeof value == 'object' && toString.call(value) == '[object Array]') : false; | ||
} | ||
/* | ||
module definitions | ||
*/ | ||
if (exports) module.exports = quack; // NodeJS | ||
@@ -34,0 +73,0 @@ else if (define && define.amd) define(quack); // AMD |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9824
10
184
2
3