Skip to content

Commit

Permalink
fix(strToBool): Cast plugins[].enabled as boolean (#297)
Browse files Browse the repository at this point in the history
* fix(strToBool): Cast plugins[].enabled as boolean

Closes #296

* Ading test for strToBool

* Move type checks into meta condition
  • Loading branch information
kolanos authored Sep 28, 2018
1 parent 8ec3c62 commit 050b08e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/report.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os from 'os';

import { sendReport } from './sendReport';
import strToBool from './util/strToBool';

import {
COLDSTART,
Expand Down Expand Up @@ -70,6 +72,13 @@ class Report {

if (meta) {
meta.uploads = meta.uploads || [];
if (meta.enabled && typeof meta.enabled === 'number') {
meta.enabled = Boolean(meta.enabled);
}

if (meta.enabled && typeof meta.enabled === 'string') {
meta.enabled = strToBool(meta.enabled);
}
}

return meta;
Expand Down
15 changes: 15 additions & 0 deletions src/util/strToBool.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/util/strToBool.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 050b08e

Please sign in to comment.