Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@kumarshanu/string-to-boolean

kumarshanu1930ISC1.0.6TypeScript support: included

it converts a given string value to a boolean value. It handles various cases such as 'true', 'false', 'yes', 'no', '1', '0', null, and undefined. It uses a switch statement with case-insensitive comparisons to determine the boolean value. If none of the

boolean, success, string, true, false, stringtoboolean, string-to-boolean, Kumarshanu

readme

@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-boolean

Usage

const stringToBoolean = require('@kumarshanu/string-to-boolean');

const result = stringToBoolean('true');
console.log(result); // Output: true

const result2 = stringToBoolean('no');
console.log(result2); // Output: false

API

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, or undefined, it returns false.
  • For any other string value, it attempts to parse it using JSON.parse and returns the parsed boolean value. If parsing fails, it throws a SyntaxError.

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!