Skip to content

Commit 1086a73

Browse files
committed
Added unit tests for toBoolean
1 parent a2fc9c6 commit 1086a73

9 files changed

+80
-21
lines changed

dist/exceptionless.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export declare class Utils {
189189
static startsWith(input: string, prefix: string): boolean;
190190
static endsWith(input: string, suffix: string): boolean;
191191
static stringify(data: any, exclusions?: string[], maxDepth?: number): string;
192-
static toBoolean(input: any): boolean;
192+
static toBoolean(input: any, defaultValue?: boolean): boolean;
193193
}
194194
export declare class Configuration implements IConfigurationSettings {
195195
private static _defaultSettings;

dist/exceptionless.js

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/exceptionless.node.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Utils-spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,50 @@ describe('Utils', () => {
270270
expect(Utils.endsWith('test', 'nopattern')).to.be.false;
271271
});
272272
});
273+
274+
describe('toBoolean', () => {
275+
it('input: blake', () => {
276+
expect(Utils.toBoolean('blake')).to.be.false;
277+
});
278+
279+
it('input: 0', () => {
280+
expect(Utils.toBoolean('0')).to.be.false;
281+
});
282+
283+
it('input: no', () => {
284+
expect(Utils.toBoolean('no')).to.be.false;
285+
});
286+
287+
it('input: false', () => {
288+
expect(Utils.toBoolean('false')).to.be.false;
289+
});
290+
291+
it('input: false', () => {
292+
expect(Utils.toBoolean(false)).to.be.false;
293+
});
294+
295+
it('input: undefined', () => {
296+
expect(Utils.toBoolean(undefined)).to.be.false;
297+
});
298+
299+
it('input: null', () => {
300+
expect(Utils.toBoolean(null)).to.be.false;
301+
});
302+
303+
it('input: 1', () => {
304+
expect(Utils.toBoolean('1')).to.be.true;
305+
});
306+
307+
it('input: yes', () => {
308+
expect(Utils.toBoolean('yes')).to.be.true;
309+
});
310+
311+
it('input: true', () => {
312+
expect(Utils.toBoolean('true')).to.be.true;
313+
});
314+
315+
it('input: true', () => {
316+
expect(Utils.toBoolean(true)).to.be.true;
317+
});
318+
});
273319
});

src/Utils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,20 @@ export class Utils {
226226
return stringifyImpl(data, exclusions);
227227
}
228228

229-
public static toBoolean(input): boolean {
230-
if (!input || !input.toLowerCase) {
231-
return !!input;
229+
public static toBoolean(input, defaultValue: boolean = false): boolean {
230+
if (typeof input === 'boolean') {
231+
return input;
232232
}
233233

234-
switch (input.toLowerCase().trim()) {
234+
if (input === null || typeof input !== 'number' && typeof input !== 'string') {
235+
return defaultValue;
236+
}
237+
238+
switch ((input + '').toLowerCase().trim()) {
235239
case 'true': case 'yes': case '1': return true;
236240
case 'false': case 'no': case '0': case null: return false;
237-
default: return Boolean(input);
238241
}
242+
243+
return defaultValue;
239244
}
240245
}

0 commit comments

Comments
 (0)