1. boolify
Convert true/false strings to booleans
boolify
Package: boolify
Created by: timhudson
Last modified: Mon, 13 Jun 2022 04:56:04 GMT
Version: 1.0.1
License: MIT
Downloads: 1,526,626
Repository: https://github.com/timhudson/boolify

Install

npm install boolify
yarn add boolify

boolify

Convert true/false strings to booleans

Build Status

Example

 var boolify = require('boolify')

boolify('true')
// true

boolify('false')
// false

boolify('A string')
// "A string"

var obj = boolify({
  t: 'true',
  f: 'false',
  s: 'Another string',
  n: 5
})
// {
//   t: true,
//   f: false,
//   s: 'Another string',
//   n: 5
// }

boolify('TRUE')
// true

boolify('tRuE')
// true

Usage

boolify is case-insensitive and will convert any string of "true" or "false" in to the
appropriate boolean.

This is helpful for handling checkboxes in express' req.body

 app.put('/users/:username', function(req, res) {
  console.log(req.body.someCheckbox) // 'true'
  req.body = boolify(req.body)
  console.log(req.body.someCheckbox) // true
})

boolify(object | string)

If provided an object, boolify will recursively convert all values.

 var result = boolify({one: {fish: {two: {fish: 'true'}}}})
console.log(result.one.fish.two.fish)
// true

Install

With npm do:

npm install boolify

See also

  • yn supports additional values for booleans, both uppercase and lowercase, and returns null when a value is recognized is neither true or false

License

MIT

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code