From b4d2390c20d4aab8a68682d388bbe9a34724f81b Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Fri, 28 Sep 2018 08:51:31 -0700 Subject: [PATCH 1/3] fix(strToBool): Cast plugins[].enabled as boolean Closes #296 --- src/report.js | 10 ++++++++++ src/util/strToBool.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/util/strToBool.js diff --git a/src/report.js b/src/report.js index 464fc0e..3671fca 100644 --- a/src/report.js +++ b/src/report.js @@ -1,5 +1,7 @@ import os from 'os'; + import { sendReport } from './sendReport'; +import strToBool from './util/strToBool'; import { COLDSTART, @@ -72,6 +74,14 @@ class Report { meta.uploads = meta.uploads || []; } + if (typeof meta.enabled === 'number') { + meta.enabled = Boolean(meta.enabled); + } + + if (typeof meta.enabled === 'string') { + meta.enabled = strToBool(meta.enabled); + } + return meta; }); diff --git a/src/util/strToBool.js b/src/util/strToBool.js new file mode 100644 index 0000000..7913a0b --- /dev/null +++ b/src/util/strToBool.js @@ -0,0 +1,15 @@ +export default function(string) { + switch (string.toLowerCase().trim()) { + case 'true': + case 't': + case '1': + return true; + case 'false': + case 'f': + case '0': + case null: + return false; + default: + return Boolean(string); + } +} From f559046ef513154d80a4c6f29479687aff15ec2e Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Fri, 28 Sep 2018 09:40:54 -0700 Subject: [PATCH 2/3] Ading test for strToBool --- src/util/strToBool.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/util/strToBool.test.js diff --git a/src/util/strToBool.test.js b/src/util/strToBool.test.js new file mode 100644 index 0000000..8274d47 --- /dev/null +++ b/src/util/strToBool.test.js @@ -0,0 +1,13 @@ +import strToBool from './strToBool'; + +test('Strings are cast as bools', () => { + expect(strToBool('true')).toBe(true); + expect(strToBool('True')).toBe(true); + expect(strToBool('t')).toBe(true); + expect(strToBool('1')).toBe(true); + + expect(strToBool('false')).toBe(false); + expect(strToBool('False')).toBe(false); + expect(strToBool('f')).toBe(false); + expect(strToBool('0')).toBe(false); +}); From b2b8946d1ccd2e61d5bf9896fc8160902568d51e Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Fri, 28 Sep 2018 09:48:24 -0700 Subject: [PATCH 3/3] Move type checks into meta condition --- src/report.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/report.js b/src/report.js index 3671fca..ad1bec8 100644 --- a/src/report.js +++ b/src/report.js @@ -72,14 +72,13 @@ class Report { if (meta) { meta.uploads = meta.uploads || []; - } - - if (typeof meta.enabled === 'number') { - meta.enabled = Boolean(meta.enabled); - } + if (meta.enabled && typeof meta.enabled === 'number') { + meta.enabled = Boolean(meta.enabled); + } - if (typeof meta.enabled === 'string') { - meta.enabled = strToBool(meta.enabled); + if (meta.enabled && typeof meta.enabled === 'string') { + meta.enabled = strToBool(meta.enabled); + } } return meta;