@kumarshanu/string-to-boolean
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

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

Package Sidebar

Install

npm i @kumarshanu/string-to-boolean

Weekly Downloads

1

Version

1.0.6

License

ISC

Unpacked Size

4.38 kB

Total Files

8

Last publish

Collaborators

  • kumarshanu