npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@parsekit/string-to-boolean

v1.0.0

Published

A robust TypeScript utility to convert strings to boolean values with extensive options and type safety

Downloads

67

Readme

@parsekit/string-to-boolean

A ZERO-DEPENDENCY robust TypeScript utility to convert strings to boolean values with extensive options and type safety.

npm version License: MIT

Features

  • 🚀 TypeScript support with full type safety
  • 🎯 Configurable truthy and falsy values
  • ⚙️ Customizable options (case sensitivity, input trimming)
  • 💪 Strict mode for rigorous parsing
  • 🧩 Handles various input types (strings, numbers, booleans)
  • 0️⃣ Zero dependencies

Installation

# Using npm
npm install @parsekit/string-to-boolean

# Using yarn
yarn add @parsekit/string-to-boolean

# Using pnpm
pnpm add @parsekit/string-to-boolean

Usage

Basic Usage

import { stringToBoolean } from '@parsekit/string-to-boolean';

// Simple conversions
stringToBoolean('true');     // returns true
stringToBoolean('false');    // returns false
stringToBoolean('yes');      // returns true
stringToBoolean('no');       // returns false
stringToBoolean(1);          // returns true
stringToBoolean(0);          // returns false

Strict Mode

import { stringToBooleanStrict } from '@parsekit/string-to-boolean';

// Throws error for invalid values
stringToBooleanStrict('true');     // returns true
stringToBooleanStrict('invalid');  // throws StringToBooleanError

Custom Options

import { stringToBoolean } from '@parsekit/string-to-boolean';

const options = {
  truthyValues: ['yes', 'y', '1', 'true'],
  falsyValues: ['no', 'n', '0', 'false'],
  caseSensitive: false,
  trimInput: true,
  strict: false
};

stringToBoolean(' YES ', options);  // returns true
stringToBoolean('n', options);      // returns false

API Reference

stringToBoolean(value: StringBoolean, options?: StringToBooleanOptions): boolean

Main function to convert various input types to boolean.

Parameters

  • value: The value to convert (string, number, boolean, null, or undefined)
  • options: Optional configuration object

Options

interface StringToBooleanOptions {
  truthyValues?: string[];      // Default: ['true', 'yes', 'y', '1']
  falsyValues?: string[];       // Default: ['false', 'no', 'n', '0']
  caseSensitive?: boolean;      // Default: false
  trimInput?: boolean;          // Default: true
  strict?: boolean;             // Default: false
}

stringToBooleanStrict(value: StringBoolean): boolean

Convenience function that calls stringToBoolean with strict: true.

stringToBooleanWithOptions(value: StringBoolean, options: StringToBooleanOptions): boolean

Alternative function name for better code readability when using options.

Default Values

The package comes with the following default values:

const DEFAULT_OPTIONS = {
  truthyValues: ['true', 'yes', 'y', '1'],
  falsyValues: ['false', 'no', 'n', '0'],
  caseSensitive: false,
  trimInput: true,
  strict: false
};

Error Handling

The package exports StringToBooleanError for error cases:

try {
  stringToBooleanStrict('maybe');
} catch (error) {
  if (error instanceof StringToBooleanError) {
    console.error('Invalid boolean value:', error.message);
  }
}

Examples

Case Sensitivity

// Case insensitive (default)
stringToBoolean('TRUE');  // returns true
stringToBoolean('False'); // returns false

// Case sensitive
stringToBoolean('TRUE', { caseSensitive: true });  // returns false

Input Trimming

// Trimming enabled (default)
stringToBoolean(' true ');  // returns true

// Trimming disabled
stringToBoolean(' true ', { trimInput: false });  // returns false

Custom Values

const options = {
  truthyValues: ['on', 'active'],
  falsyValues: ['off', 'inactive']
};

stringToBoolean('on', options);      // returns true
stringToBoolean('active', options);  // returns true
stringToBoolean('off', options);     // returns false

Handling Special Values

// Null/undefined handling
stringToBoolean(null);              // returns false
stringToBoolean(undefined);         // returns false
stringToBoolean(null, { strict: true });  // throws StringToBooleanError

// Number handling
stringToBoolean(1);                 // returns true
stringToBoolean(0);                 // returns false
stringToBoolean(2, { strict: true });  // throws StringToBooleanError

TypeScript Support

The package includes TypeScript definitions and exports the following types:

type StringBoolean = string | number | boolean | null | undefined;

interface StringToBooleanOptions {
  truthyValues?: string[];
  falsyValues?: string[];
  caseSensitive?: boolean;
  trimInput?: boolean;
  strict?: boolean;
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © Srikar Phani Kumar Marti