Proper way to parse Boolean query string param in node/express
Docs if you are using query-string
const queryString = require('query-string');queryString.parse('foo=true', {parseBooleans: true});//=> {foo: true}
I use this pair of lines:
let test = (value).toString().trim().toLowerCase(); let result = !((test === 'false') || (test === '0') || (test === ''));
Here is my generic solution for getting a query params as a boolean:
const isTrue = Boolean((req.query.myParam || "").replace(/\s*(false|null|undefined|0)\s*/i, ""))
It converts the query param into a string which isthen cleaned by suppressing any falsy string.Any resulting non-empty string will be true
.