1.0.6 • Published 3 years ago
@kumarshanu/string-to-boolean v1.0.6
@kumarshanu/string-to-boolean
A lightweight npm package that provides a function stringToBoolean for converting a string value to a boolean value.
Installation
npm install @kumarshanu/string-to-booleanUsage
const stringToBoolean = require('@kumarshanu/string-to-boolean');
const result = stringToBoolean('true');
console.log(result); // Output: true
const result2 = stringToBoolean('no');
console.log(result2); // Output: falseAPI
stringToBoolean(stringValue)
This function takes a string value as input and returns a boolean value based on the following rules:
- If the string value is one of 'true', 'True', 'TRUE', 'yes', or '1', it returns
true. - If the string value is one of 'false', 'False', 'FALSE', 'no', '0',
null, orundefined, it returnsfalse. - For any other string value, it attempts to parse it using
JSON.parseand returns the parsed boolean value. If parsing fails, it throws aSyntaxError.
License
This package is licensed under the MIT License. See the LICENSE file for details.
Feel free to use this npm package, @kumarshanu/string-to-boolean, to easily convert string values to boolean in your JavaScript projects. Contributions and feedback are always welcome!