-
-
Notifications
You must be signed in to change notification settings - Fork 7
Accept predicate function or array of types as type parameter #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ var types = [ | |
| function normalize(type, value) { | ||
| var args = Array.prototype.slice.call(arguments, 2); | ||
|
|
||
| var isType = typeof value === type; | ||
| var isType = conforms(type, value); | ||
| var isFunction = typeof value === 'function'; | ||
|
|
||
| if (!isType && !isFunction) { | ||
|
|
@@ -33,6 +33,18 @@ function normalize(type, value) { | |
| return result; | ||
| } | ||
|
|
||
| function conforms(type, value) { | ||
| if (typeof type === 'string') { | ||
| return typeof value === type; | ||
| } | ||
| if (typeof type === 'function') { | ||
| return !!type(value); | ||
| } | ||
| return type.some(function(type) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need an array test around this so it doesn't throw strange errors ("some is not a function")?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return conforms(type, value); | ||
| }); | ||
| } | ||
|
|
||
| // Add methods for each type | ||
| types.forEach(function(type) { | ||
| normalize[type] = normalize.bind(null, type); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| "lint": "eslint . && jscs index.js test/", | ||
| "pretest": "npm run lint", | ||
| "test": "mocha --async-only", | ||
| "cover": "istanbul cover _mocha --report lcovonly", | ||
| "cover": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird that this would be needed, the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "coveralls": "npm run cover && istanbul-coveralls" | ||
| }, | ||
| "dependencies": {}, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it best to support all 3 types (string, array, function) or should it just support function and the utility methods like
string(),boolean(), etc can just have the predicates applied?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think multiple types is common enough and more clearly conveyed by an array than by a function (
function(value) { return typeof value === "string" || typeof ...). I also like supporting the string, because if I have the type in a variable, I would prefernormalize(type, ...)tonormalize[type](...).I don't go for flexibility at all cost however I think the overhead of supporting the 3 case is quite light here.