swatchjs-utils
Version:
Utilities for SwatchJS, a framework for easily creating and exposing APIs as methods
29 lines (24 loc) • 714 B
JavaScript
;
var optional = require('./optional');
// This parser is used for required boolean params,
// and will coerce null or undefined values to false
function parseBoolean(param) {
// Special handling for string versions of true/false
if (param === 'true') {
return true;
}
if (param === 'false') {
return false;
}
// Everything else should let JS coerce to Boolean
return Boolean(param);
}
// This parser is used for optional boolean params,
// but will allow true, false, or undefined values
function parseOptionalBoolean(param) {
return optional.parser(parseBoolean)(param);
}
module.exports = {
parseBoolean: parseBoolean,
parseOptionalBoolean: parseOptionalBoolean
};