From 34202380879cf87f663848b174c228385aa129bc Mon Sep 17 00:00:00 2001 From: JosephVoid Date: Mon, 18 Mar 2024 01:34:07 +0300 Subject: [PATCH] Bug Fix for `istrue` & `isfalse` utility function (#1099) * `bug`: fixed isTrue & isFalse error + when a number is ecountered isTrue and isFalse return error + now fixed * `improve`: added new test cases to verify --- lib/util/http.js | 4 ++-- test/unit/util/http.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/util/http.js b/lib/util/http.js index 81fd054d6..8ef2c3a7a 100644 --- a/lib/util/http.js +++ b/lib/util/http.js @@ -18,8 +18,8 @@ const sanitize = require('sanitize-filename'); // REQUEST PARSING // Returns t/f only if the input is a string of any upper/lowercase combination. -const isTrue = (x) => (!isBlank(x) && (x.toLowerCase() === 'true')); -const isFalse = (x) => (!isBlank(x) && (x.toLowerCase() === 'false')); +const isTrue = (x) => (!isBlank(x) && typeof x === 'string' && (x.toLowerCase() === 'true')); +const isFalse = (x) => (!isBlank(x) && typeof x === 'string' && (x.toLowerCase() === 'false')); // Returns just the pathname of the URL, omitting querystring and other non-path decoration. const urlPathname = (x) => parse(x).pathname; diff --git a/test/unit/util/http.js b/test/unit/util/http.js index 8987564cc..8a9c06453 100644 --- a/test/unit/util/http.js +++ b/test/unit/util/http.js @@ -17,6 +17,25 @@ describe('util/http', () => { isTrue('').should.equal(false); isTrue(null).should.equal(false); isTrue(undefined).should.equal(false); + isTrue(2).should.equal(false); + }); + }); + + describe('isFalse', () => { + const { isFalse } = http; + it('should return true for falsey strings', () => { + isFalse('FALSE').should.equal(true); + isFalse('False').should.equal(true); + isFalse('false').should.equal(true); + }); + + it('should return false for all other values', () => { + isFalse('no').should.equal(false); + isFalse('off').should.equal(false); + isFalse('').should.equal(false); + isFalse(null).should.equal(false); + isFalse(undefined).should.equal(false); + isFalse(2).should.equal(false); }); });