Comparing version 0.0.1 to 1.0.0
@@ -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 |
23
index.js
@@ -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() | ||
}) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
81
3400
7