str2bool
Version:
Converts a string or number like boolean to true / false of type boolean.
18 lines (14 loc) • 461 B
JavaScript
/*
* Created on Sun Jan 14 2018
* Copyright (c) 2018
* author kyle
* file description:
*/
;
exports = module.exports = value => {
const booleanLike = [true, 'true', 1, '1', false, 'false', 0, '0'];
if (!booleanLike.includes(value)) {
throw new Error('parameter must be like a boolean data.');
}
return (value && typeof value === 'string') ? (value.toLowerCase() === 'true' || value === '1') : (value === true || value === 1);
};