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 to include objects that are created using new keyword #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ const validAddress = '10 Downing Street';
const invalidAddress = 'bwahahaha';
console.log(`"10 Downing Street" is a valid address - ${t(validAddress).isAddress}`); // true
console.log(`"bwahahaha" is a valid address - ${t(invalidAddress).isAddress}`); // false

const string = 'someString';
const stringObject = new String('MyString'); // eslint-disable-line no-new-wrappers
console.log(`String literal test: ${t(string).isString}`);
console.log(`String object test: ${t(stringObject).isString}`);

const booleanLiteral = false;
const booleanObject = new Boolean(123); // eslint-disable-line no-new-wrappers
console.log(`Boolean literal test: ${t(booleanLiteral).isBoolean}`);
console.log(`Boolean object test: ${t(booleanObject).isBoolean}`);

const numberLiteral = 23;
const numberObject = new Number(3241232352352); // eslint-disable-line no-new-wrappers
console.log(`Number literal test: ${t(numberLiteral).isNumber}`);
console.log(`Number object test: ${t(numberObject).isNumber}`);
17 changes: 14 additions & 3 deletions src/typy.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ class Typy {
}

get isBoolean() {
if (typeof this.input === typeof true) return true;
if (
typeof this.input === typeof true ||
Object.prototype.toString.call(this.input) === '[object Boolean]'
) {
return true;
}
return false;
}

Expand Down Expand Up @@ -111,7 +116,12 @@ class Typy {
}

get isString() {
if (typeof this.input === 'string') return true;
if (
typeof this.input === 'string' ||
Object.prototype.toString.call(this.input) === '[object String]'
) {
return true;
}
return false;
}

Expand All @@ -121,7 +131,8 @@ class Typy {
}

get isNumber() {
if (Number.isFinite(this.input)) return true;
if (Number.isFinite(this.input)
|| Object.prototype.toString.call(this.input) === '[object Number]') return true;
return false;
}

Expand Down
8 changes: 7 additions & 1 deletion test/typy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Typy', () => {

describe('Truthy/Falsy', () => {
test('should test if object is truthy', () => {
const truthyValues = ['hey', 11, {}, { yo: 'yoyo' }, true, [], [1, 2]];
const truthyValues = ['hey', 11, {}, { yo: 'yoyo' }, true, [], [1, 2], new Boolean(23)]; // eslint-disable-line no-new-wrappers
truthyValues.map((value) => {
expect(t(value).isTruthy === true).toBeTruthy();
expect(t(value).isFalsy === false).toBeTruthy();
Expand Down Expand Up @@ -113,7 +113,9 @@ describe('Typy', () => {

test('should test if type is string', () => {
const obj = 'hello';
const stringObject = new String('myStringObject'); // eslint-disable-line no-new-wrappers
expect(t(obj).isString === true).toBeTruthy();
expect(t(stringObject).isString === true).toBeTruthy();
});

test('should test if string is empty string', () => {
Expand All @@ -138,6 +140,10 @@ describe('Typy', () => {
expect(t(num).isNumber === true).toBeTruthy();
num = 'number';
expect(t(num).isNumber === false).toBeTruthy();
num = Infinity;
expect(t(num).isNumber === true).toBeTruthy();
num = new Number(23523452345); // eslint-disable-line no-new-wrappers
expect(t(num).isNumber === true).toBeTruthy();
});

test('should test if type is Boolean', () => {
Expand Down