From 93f17ff092ba40b798c0d42ae382912db1adb86c Mon Sep 17 00:00:00 2001 From: Abdul Rehman Date: Mon, 30 Sep 2019 19:03:48 +0530 Subject: [PATCH 1/3] Ensure string type even if string is created using new String() constructor --- examples/example.js | 6 ++++++ src/typy.js | 7 ++++++- test/typy.test.js | 2 ++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/example.js b/examples/example.js index 546a2dc..0454e8f 100644 --- a/examples/example.js +++ b/examples/example.js @@ -92,3 +92,9 @@ 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}`); diff --git a/src/typy.js b/src/typy.js index 7bcdbad..5df9ae0 100644 --- a/src/typy.js +++ b/src/typy.js @@ -111,7 +111,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; } diff --git a/test/typy.test.js b/test/typy.test.js index 889cc0e..0dd4efa 100644 --- a/test/typy.test.js +++ b/test/typy.test.js @@ -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', () => { From 2baa8f7c4843a538158fd49c8b2cf9cf27bc0173 Mon Sep 17 00:00:00 2001 From: Abdul Rehman Date: Mon, 30 Sep 2019 19:13:45 +0530 Subject: [PATCH 2/3] Ensure bool type even if boolean is created using new Boolean() constructor --- examples/example.js | 6 +++++- src/typy.js | 7 ++++++- test/typy.test.js | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/examples/example.js b/examples/example.js index 0454e8f..3fcb3fd 100644 --- a/examples/example.js +++ b/examples/example.js @@ -95,6 +95,10 @@ console.log(`"bwahahaha" is a valid address - ${t(invalidAddress).isAddress}`); 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}`); diff --git a/src/typy.js b/src/typy.js index 5df9ae0..bdf9293 100644 --- a/src/typy.js +++ b/src/typy.js @@ -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; } diff --git a/test/typy.test.js b/test/typy.test.js index 0dd4efa..8a655e5 100644 --- a/test/typy.test.js +++ b/test/typy.test.js @@ -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(); From 3c6485f1d3adfc46c94cee19d8738074442e7351 Mon Sep 17 00:00:00 2001 From: Abdul Rehman Date: Mon, 30 Sep 2019 21:52:51 +0530 Subject: [PATCH 3/3] Ensure number type for numbers Infinity, -Infinity & numbers created using new Number() constructor --- examples/example.js | 5 +++++ src/typy.js | 3 ++- test/typy.test.js | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/example.js b/examples/example.js index 3fcb3fd..2035756 100644 --- a/examples/example.js +++ b/examples/example.js @@ -102,3 +102,8 @@ 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}`); diff --git a/src/typy.js b/src/typy.js index bdf9293..ac227bb 100644 --- a/src/typy.js +++ b/src/typy.js @@ -131,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; } diff --git a/test/typy.test.js b/test/typy.test.js index 8a655e5..ff0b6a9 100644 --- a/test/typy.test.js +++ b/test/typy.test.js @@ -140,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', () => {