boolify
Convert true/false strings to booleans
Last updated 12 years ago by timhudson .
Repository · Bugs · Original npm · Tarball · package.json
$ npm install boolify 
SYNC missed versions from official npm registry.

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

Current Tags

  • 1.0.1                                ...           latest (10 years ago)

3 Versions

  • 1.0.1                                ...           10 years ago
  • 1.0.0                                ...           12 years ago
  • 0.0.1                                ...           14 years ago
Maintainers (1)
Downloads
Total 6
Today 0
This Week 0
This Month 6
Last Day 0
Last Week 3
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE