Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

boolify

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

boolify - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

32

example/example.js

@@ -1,6 +0,12 @@

var boolify = require('../index.js')
var boolify = require('../')
var trueString = boolify('true')
var falseString = boolify('false')
var aString = boolify('A string')
boolify('true')
// true
boolify('false')
// false
boolify('A string')
// "A string"
var obj = boolify({

@@ -12,9 +18,13 @@ t: 'true',

})
// {
// t: true,
// f: false,
// s: 'Another string',
// n: 5
// }
console.log(trueString === true)
console.log(falseString === false)
console.log(aString === 'A string')
console.log(obj.t === true)
console.log(obj.f === false)
console.log(obj.s === 'Another string')
console.log(obj.n === 5)
boolify('TRUE')
// true
boolify('tRuE')
// true

@@ -1,9 +0,11 @@

module.exports = function(obj) {
if (typeof obj === 'string')
return convert(obj)
module.exports = function boolify (obj) {
if (typeof obj === 'string') return convert(obj)
if (toString.call(obj) === '[object Object]')
for (var key in obj) {
obj[key] = convert(obj[key])
if (toString.call(obj) === '[object Object]') {
var keys = Object.keys(obj)
for (var i = 0, l = keys.length; i < l; i++) {
obj[keys[i]] = boolify(obj[keys[i]])
}
}

@@ -13,5 +15,6 @@ return obj

function convert(value) {
return value === 'true' ? true :
value === 'false' ? false : value
}
function convert (value) {
var v = value.toLowerCase()
return v === 'true' ? true :
v === 'false' ? false : value
}
{
"name": "boolify",
"description": "Convert true/false strings to booleans",
"version": "0.0.1",
"version": "1.0.0",
"repository": {

@@ -10,8 +10,7 @@ "url": "git://github.com/timhudson/boolify.git"

"scripts": {
"test": "tap test/*.js"
"test": "tape test/*.js"
},
"dependencies": {},
"devDependencies": {
"tap": "~0.4.1"
"tape": "^4.0.0"
}
}
}

@@ -7,10 +7,16 @@ # boolify

# example
## Example
``` js
var boolify = require('../index.js')
var boolify = require('boolify')
var trueString = boolify('true')
var falseString = boolify('false')
var aString = boolify('A string')
boolify('true')
// true
boolify('false')
// false
boolify('A string')
// "A string"
var obj = boolify({

@@ -22,13 +28,23 @@ t: 'true',

})
// {
// t: true,
// f: false,
// s: 'Another string',
// n: 5
// }
console.log(trueString === true)
console.log(falseString === false)
console.log(aString === 'A string')
console.log(obj.t === true)
console.log(obj.f === false)
console.log(obj.s === 'Another string')
console.log(obj.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`
``` js

@@ -42,4 +58,14 @@ app.put('/users/:username', function(req, res) {

# install
### `boolify(object | string)`
If provided an object, `boolify` will recursively convert all values.
``` js
var result = boolify({one: {fish: {two: {fish: 'true'}}}})
console.log(result.one.fish.two.fish)
// true
```
## Install
With [npm](https://npmjs.org) do:

@@ -51,4 +77,8 @@

# license
## See also
* [yn](https://github.com/sindresorhus/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

@@ -1,2 +0,2 @@

var test = require('tap').test
var test = require('tape')
, boolify = require('../index.js')

@@ -24,1 +24,15 @@

})
test('Is case-insensitive', function(t) {
t.equal(boolify('TRUE'), true)
t.equal(boolify('tRuE'), true)
t.equal(boolify('FALSE'), false)
t.equal(boolify('fAlSe'), false)
t.end()
})
test('Traverses objects recursively', function(t) {
var data = boolify({one: {fish: {two: {fish: 'true'}}}})
t.equal(data.one.fish.two.fish, true)
t.end()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc