Created
September 1, 2014 08:42
-
-
Save cjroth/7afbb59c5ec8cb512bfa to your computer and use it in GitHub Desktop.
validate and parse integer ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* validates and parses ids. optionally accepts multi-ids (comma separated integer ids) | |
*/ | |
var validator = require('validator'); | |
var parse = function(ids, multiple) { | |
if (!ids) { | |
return false; | |
} | |
if (typeof ids === 'string') { | |
ids = ids.split(','); | |
} | |
if (!multiple) { | |
return validator.isInt(ids) ? parseInt(ids[0]) : false; | |
} | |
for (var i in ids) { | |
ids[i] = parse(ids[i]); | |
if (!ids[i]) { | |
return false; | |
} | |
} | |
return ids; | |
}; | |
module.exports = parse; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment