Comparing version 0.1.4 to 0.2.0
{ | ||
"name": "quack", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Check the values of passed arguments in a function - see if they quack like a duck.", | ||
@@ -10,3 +10,3 @@ "main": "quack.js", | ||
"prepublish": "npm run test-min && git diff --exit-code", | ||
"build": "./node_modules/uglify-js/bin/uglifyjs quack.js -m -o quack.min.js" | ||
"build": "./node_modules/uglify-js/bin/uglifyjs quack.js -m -o quack.min.js --source-map quack.js.src" | ||
}, | ||
@@ -13,0 +13,0 @@ "repository": { |
48
quack.js
@@ -5,2 +5,3 @@ /* | ||
https://github.com/screenRev/quack | ||
borrowed some type checking from https://github.com/bestiejs/lodash | ||
*/ | ||
@@ -22,2 +23,4 @@ | ||
var quack = function quack(signature, args){ | ||
var i, max, sig, arg; | ||
// convert array-like "arguments" to an array | ||
@@ -33,5 +36,5 @@ if (isArguments(args)) args = [].slice.apply(args); | ||
// iterate over the signature, matching types against arguments | ||
for (var i = 0, max = signature.length; i < max; i++) { | ||
var sig = signature[i].toLowerCase(); | ||
var arg = args[i]; | ||
for (i = 0, max = signature.length; i < max; i++) { | ||
sig = signature[i].toLowerCase(); | ||
arg = args[i]; | ||
@@ -44,2 +47,14 @@ if (sig == 'object') { | ||
} | ||
else if (sig == 'function') { | ||
if (! isFunction(arg)) return false; | ||
} | ||
else if (sig == 'date') { | ||
if (! isDate(arg)) return false; | ||
} | ||
else if (sig == 'regexp') { | ||
if (! isRegExp(arg)) return false; | ||
} | ||
else if (sig == 'arguments') { | ||
if (! isArguments(arg)) return false; | ||
} | ||
else if (sig !== typeof arg) return false; | ||
@@ -56,2 +71,4 @@ } | ||
var toString = Object().toString; | ||
var objectTypes = { | ||
@@ -66,2 +83,9 @@ 'function': true, | ||
// fallback for browsers that can't detect `arguments` objects by [[Class]] | ||
if (! isArguments(arguments)) { | ||
isArguments = function(value) { | ||
return value ? Object().hasOwnProperty.call(value, 'callee') : false; | ||
}; | ||
} | ||
function isObject(value) { | ||
@@ -75,3 +99,21 @@ return value ? !! objectTypes[typeof value] : false; | ||
function isFunction(value){ | ||
return typeof value == 'function'; | ||
} | ||
// fallback for older versions of Chrome and Safari | ||
if (isFunction(/x/)) { | ||
isFunction = function(value){ | ||
return typeof value == 'function' && toString.call(value) == '[object Function]'; | ||
}; | ||
} | ||
function isDate(value){ | ||
return value ? (typeof value == 'object' && toString.call(value) == '[object Date]') : false; | ||
} | ||
function isRegExp(value) { | ||
return value ? (typeof value == 'object' && toString.call(value) == '[object RegExp]') : false; | ||
} | ||
/* | ||
@@ -78,0 +120,0 @@ module definitions |
@@ -1,1 +0,4 @@ | ||
void function(e,t){"use strict";var r=function r(e,t){if(n(t))t=[].slice.apply(t);if(typeof e=="string"){e=e.replace(/^\s*|\s*$/g,"").split(/\s*,\s*/)}for(var r=0,f=e.length;r<f;r++){var s=e[r].toLowerCase();var a=t[r];if(s=="object"){if(!i(a))return false}else if(s=="array"){if(!o(a))return false}else if(s!==typeof a)return false}return true};var f={"function":true,object:true};function n(e){return toString.call(e)=="[object Arguments]"}function i(e){return e?!!f[typeof e]:false}function o(e){return e?typeof e=="object"&&toString.call(e)=="[object Array]":false}if(exports)module.exports=r;else if(define&&define.amd)define(r);else e.quack=r}(this); | ||
void function(e,t){"use strict";var f=function f(e,t){var f,r,n,a;if(i(t))t=[].slice.apply(t);if(typeof e=="string"){e=e.replace(/^\s*|\s*$/g,"").split(/\s*,\s*/)}for(f=0,r=e.length;f<r;f++){n=e[f].toLowerCase();a=t[f];if(n=="object"){if(!o(a))return false}else if(n=="array"){if(!l(a))return false}else if(n=="function"){if(!u(a))return false}else if(n=="date"){if(!c(a))return false}else if(n=="regexp"){if(!s(a))return false}else if(n=="arguments"){if(!i(a))return false}else if(n!==typeof a)return false}return true};var r=Object().toString;var n={"function":true,object:true};function i(e){return r.call(e)=="[object Arguments]"}if(!i(arguments)){i=function(e){return e?Object().hasOwnProperty.call(e,"callee"):false}}function o(e){return e?!!n[typeof e]:false}function l(e){return e?typeof e=="object"&&r.call(e)=="[object Array]":false}function u(e){return typeof e=="function"}if(u(/x/)){u=function(e){return typeof e=="function"&&r.call(e)=="[object Function]"}}function c(e){return e?typeof e=="object"&&r.call(e)=="[object Date]":false}function s(e){return e?typeof e=="object"&&r.call(e)=="[object RegExp]":false}if(exports)module.exports=f;else if(define&&define.amd)define(f);else e.quack=f}(this); | ||
/* | ||
//@ sourceMappingURL=quack.js.src | ||
*/ |
@@ -23,2 +23,35 @@ # quack | ||
## installation | ||
### NodeJS | ||
```bash | ||
npm install quack | ||
``` | ||
```javascript | ||
var quack = require('quack'); | ||
``` | ||
### Browser | ||
Download `quack.js`, `quack.min.js`, and `quack.js.src`, and add them to your project. | ||
**RequireJS** (AMD) | ||
```javascript | ||
require(['path/to/quack'], function(quack){ | ||
// use quack here | ||
}); | ||
``` | ||
**Standard** | ||
```html | ||
<script src="path/to/quack.min.js"></script> | ||
``` | ||
## usage | ||
@@ -48,8 +81,12 @@ | ||
- **string**: `string` (primitive) | ||
- **number**: `number` (primitive) | ||
- **boolean**: `boolean` (primitive) | ||
- **object**: ECMAScript `object`, | ||
including `array`, `function`, `object`, `regex`, `new Number(0)`, and `new String('')` | ||
- **array**: `array` (not including `arguments` object) | ||
- **string**: `String` (primitive) | ||
- **number**: `Number` (primitive) | ||
- **boolean**: `Boolean` (primitive) | ||
- **object**: ECMAScript `Object`, | ||
including `Array`, `Function`, `Object`, `RegExp`, `new Number(0)`, and `new String('')` | ||
- **array**: `Array` (not including `arguments` object) | ||
- **function**: `Function` | ||
- **date**: `Date` object | ||
- **regexp**: `RegExp` object | ||
- **arguments**: `arguments` object | ||
@@ -59,10 +96,10 @@ | ||
- 0.1: initial release, with `string`, `number`, `object`, & `array` (with tests) | ||
- 0.2: add `function`, `date`, `regex`, `arguments`, `simpleObject` | ||
- 0.1: **DONE** initial release, with `string`, `number`, `object`, & `array` (with tests) | ||
- 0.2: **DONE** add `function`, `date`, `regexp`, `arguments` | ||
- 0.3: shorthand | ||
eg. `'"", {}, [], 1'` short for `'string, object, array, number'` | ||
- 0.4: `options`: a third argument | ||
- *throws* `boolean`: throw and error if validation fails | ||
- *console* `boolean`: log to the console if validation fails | ||
- *message* `string`: a specific message for logged/thrown errors | ||
- **throws** `boolean`: throw and error if validation fails | ||
- **console** `boolean`: log to the console if validation fails | ||
- **message** `string`: a specific message for logged/thrown errors | ||
- 0.5: types of `arrays` & `object` properties | ||
@@ -69,0 +106,0 @@ - `'string[]'`: an `array` of `strings` |
@@ -18,3 +18,3 @@ // common types | ||
aDate: new Date(), | ||
aRegex: / /i, | ||
aRegExp: / /i, | ||
aNull: null, | ||
@@ -82,7 +82,7 @@ anUndefined: void 0 | ||
quack('object', [types.aFunction]).should.equal(true); | ||
quack('object', [types.aRegex]).should.equal(true); | ||
quack('object', [types.aRegExp]).should.equal(true); | ||
}); | ||
it('should fail for invalid objects', function(){ | ||
testAllExcept(['object', 'date', 'array', 'function', 'regex'], function(value){ | ||
testAllExcept(['object', 'date', 'array', 'function', 'regexp'], function(value){ | ||
quack('object', [value]).should.equal(false); | ||
@@ -102,3 +102,43 @@ }); | ||
}); | ||
it('should pass for valid functions', function(){ | ||
quack('function', [types.aFunction]).should.equal(true); | ||
}); | ||
it('should fail for invalid functions', function(){ | ||
testAllExcept('function', function(value){ | ||
quack('function', [value]).should.equal(false); | ||
}); | ||
}); | ||
it('should pass for valid dates', function(){ | ||
quack('date', [types.aDate]).should.equal(true); | ||
}); | ||
it('should fail for invalid dates', function(){ | ||
testAllExcept('date', function(value){ | ||
quack('date', [value]).should.equal(false); | ||
}); | ||
}); | ||
it('should pass for valid regular expressions', function(){ | ||
quack('regexp', [types.aRegExp]).should.equal(true); | ||
}); | ||
it('should fail for invalid regular expressions', function(){ | ||
testAllExcept('regexp', function(value){ | ||
quack('regexp', [value]).should.equal(false); | ||
}); | ||
}); | ||
it('should pass for valid arguments objects', function(){ | ||
quack('arguments', [types.anArgsObject]).should.equal(true); | ||
}); | ||
it('should fail for invalid arguments objects', function(){ | ||
testAllExcept('arguments', function(value){ | ||
quack('args', [value]).should.equal(false); | ||
}); | ||
}); | ||
}); | ||
}; |
67542
14
266
107