Skip to content

Instantly share code, notes, and snippets.

@cjroth
Created September 1, 2014 08:42
Show Gist options
  • Save cjroth/7afbb59c5ec8cb512bfa to your computer and use it in GitHub Desktop.
Save cjroth/7afbb59c5ec8cb512bfa to your computer and use it in GitHub Desktop.
validate and parse integer ids
/**
* 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