Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(strToBool): Cast plugins[].enabled as boolean #297

Merged
merged 3 commits into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});