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

Added support for primitive object wrappers (fixes #295) #296

Merged
merged 4 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 10 additions & 10 deletions lib/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const boolean = function (value: any): boolean {
if (typeof value === 'string') {
return [ 'true', 't', 'yes', 'y', 'on', '1' ].includes(value.trim().toLowerCase());
}
switch (Object.prototype.toString.call(value)) {
case '[object String]':
return [ 'true', 't', 'yes', 'y', 'on', '1' ].includes(value.trim().toLowerCase());

if (typeof value === 'number') {
return value === 1;
}
case '[object Number]':
return value.valueOf() === 1;

if (typeof value === 'boolean') {
return value;
}
case '[object Boolean]':
return value.valueOf();

return false;
default:
return false;
}
};

export { boolean };
116 changes: 116 additions & 0 deletions test/unit/booleanTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ suite('boolean', (): void => {
test('false returns false.', async (): Promise<void> => {
assert.that(boolean(false)).is.false();
});

test('Boolean(true) returns true.', async (): Promise<void> => {
assert.that(boolean(new Boolean(true))).is.true();
});

test('Boolean(false) returns false.', async (): Promise<void> => {
assert.that(boolean(new Boolean(false))).is.false();
});
});

suite('string', (): void => {
Expand Down Expand Up @@ -129,6 +137,102 @@ suite('boolean', (): void => {
test('trims whitespace.', async (): Promise<void> => {
assert.that(boolean(' true ')).is.true();
});

test('String("true") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('true'))).is.true();
});

test('String("false") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('false'))).is.false();
});

test('String("TRUE") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('TRUE'))).is.true();
});

test('String("FALSE") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('FALSE'))).is.false();
});

test('String("t") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('t'))).is.true();
});

test('String("f") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('f'))).is.false();
});

test('String("T") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('T'))).is.true();
});

test('String("F") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('F'))).is.false();
});

test('String("yes") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('yes'))).is.true();
});

test('String("no") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('no'))).is.false();
});

test('String("YES") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('YES'))).is.true();
});

test('String("NO") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('NO'))).is.false();
});

test('String("y") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('y'))).is.true();
});

test('String("n") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('n'))).is.false();
});

test('String("Y") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('Y'))).is.true();
});

test('String("N") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('N'))).is.false();
});

test('String("on") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('on'))).is.true();
});

test('String("ON") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('ON'))).is.true();
});

test('String("1") returns true.', async (): Promise<void> => {
assert.that(boolean(new String('1'))).is.true();
});

test('String("0") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('0'))).is.false();
});

test('String("contains-the-letter-t") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('contains-the-letter-t'))).is.false();
});

test('String("contains-the-word-yes") returns false.', async (): Promise<void> => {
assert.that(boolean(new String('noyesno'))).is.false();
});

test('arbitrary string object wrapper string returns false.', async (): Promise<void> => {
assert.that(boolean(new String('123'))).is.false();
});

test('trims whitespace in string object wrapper.', async (): Promise<void> => {
assert.that(boolean(new String(' true '))).is.true();
});
});

suite('number', (): void => {
Expand All @@ -143,5 +247,17 @@ suite('boolean', (): void => {
test('123 returns false.', async (): Promise<void> => {
assert.that(boolean(123)).is.false();
});

test('Number(1) returns true.', async (): Promise<void> => {
assert.that(boolean(new Number(1))).is.true();
});

test('Number(0) returns false.', async (): Promise<void> => {
assert.that(boolean(new Number(0))).is.false();
});

test('Number(123) returns false.', async (): Promise<void> => {
assert.that(boolean(new Number(123))).is.false();
});
});
});