diff --git a/README.md b/README.md index dc9357b..bb29980 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,9 @@ npm install --save qc-type_of | Value | typeof | typeOf | | ---------- | ----------- | ----------- | | arguments | 'object' | 'arguments' | -| [] | 'object' | 'array' | | new Boolean(...) | 'object' | 'boolean' | -| new Date() | 'object' | 'date' | | Infinity | 'number' | 'infinity' | | NaN | 'number' | 'nan' | | null | 'object' | 'null' | | new Number(...) | 'object' | 'number' | -| /ab/i | 'object' | 'regexp' | | new String(...) | 'object' | 'string' | diff --git a/package.json b/package.json index b4a99c0..ce9276d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qc-type_of", - "version": "0.0.3", + "version": "1.0.0", "author": { "name": "Danny Hurlburt", "url": "https://github.com/dhurlburtusa" diff --git a/src/index.test.ts b/src/index.test.ts index 2c47085..3ee1045 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -14,14 +14,14 @@ describe('qc-type_of', () => { expect(result).toBe('arguments'); }); - it('called with an empty array should return `"array"`', () => { + it('called with an empty array should return `"object"`', () => { let result = typeOf([]); - expect(result).toBe('array'); + expect(result).toBe('object'); }); - it('called with `Array` object should return `"array"`', () => { + it('called with `Array` object should return `"object"`', () => { let result = typeOf(new Array('one', 'two', 'etc')); - expect(result).toBe('array'); + expect(result).toBe('object'); }); it('called with `false` should return `"boolean"`', () => { @@ -40,9 +40,9 @@ describe('qc-type_of', () => { expect(result).toBe('boolean'); }); - it('called with a date should return `"date"`', () => { + it('called with a date should return `"object"`', () => { let result = typeOf(new Date()); - expect(result).toBe('date'); + expect(result).toBe('object'); }); it('called with an `Error` object should return `"object"`', () => { @@ -157,14 +157,14 @@ describe('qc-type_of', () => { expect(result).toBe('number'); }); - it('called with a regular expression should return `"regexp"`', () => { + it('called with a regular expression should return `"object"`', () => { let result = typeOf(/typeof/); - expect(result).toBe('regexp'); + expect(result).toBe('object'); }); - it('called with `RegExp` object should return `"regexp"`', () => { + it('called with `RegExp` object should return `"object"`', () => { let result = typeOf(new RegExp('.*\\..*')); - expect(result).toBe('regexp'); + expect(result).toBe('object'); }); it('called with an empty string should return `"string"`', () => { @@ -212,4 +212,4 @@ describe('qc-type_of', () => { }); -}); \ No newline at end of file +}); diff --git a/src/index.ts b/src/index.ts index b0431bb..865f4bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,22 +4,21 @@ * Returns the type of the specified value as a string. The list of possible types are: * * - `'arguments'`: If the specified value is a function Arguments object. - * - `'array'`: If the specified value is an array. * - `'boolean'`: If the specified value is a boolean value. - * - `'date'`: If the specified value is a `Date` object. * - `'function'`: If the specified value is a function reference. This includes generator functions. * - `'infinity'`: If the specified value is `Infinity`, `Number.NEGATIVE_INFINITY`, or `Number.POSITIVE_INFINITY`. * - `'nan'`: If the specified value is `NaN`. * - `'null'`: If the specified value is `null`. * - `'number'`: If the specified value is a number. * - `'object'`: If the specified value is an object. - * - `'regexp'`: If the specified value is a regular expression. * - `'string'`: If the specified value is a string. * - `'symbol'`: If the specified value is a symbol (ES6). * - `'undefined'`: If the specified value is `undefined`. * - `'undetermined'`: If the specified value has an undetermined type. If this ever happens, then update this function * to appropriately handle the value. * + * If `'object'` is returned, then the `instanceof` operator can be used to narrow down the type. + * * **Example Usage:** * * ```js @@ -27,46 +26,53 @@ * typeOf(arguments); // 'arguments' * })(); * - * typeof []; // 'object' - * typeOf([]); // 'array' instead of 'object' + * typeOf([]); // 'object' + * + * typeof new Boolean(...); // 'object' + * typeOf(new Boolean(...)); // 'boolean' instead of 'object' * * typeOf(true); // 'boolean' * - * typeof new Date(); // 'object' - * typeOf(new Date()); // 'date' instead of 'object' + * typeOf(new Date()); // 'object' * * typeOf(function () {}); // 'function' * * typeof Infinity; // 'number' * typeOf(Infinity); // 'infinity' instead of 'number' * + * typeof Number.NEGATIVE_INFINITY; // 'number' + * typeOf(Number.NEGATIVE_INFINITY); // 'infinity' instead of 'number' + * * typeof NaN; // 'number' * typeOf(NaN); // 'nan' instead of 'number' * * typeof null; // 'object' - * typeOf(null); // 'null' + * typeOf(null); // 'null' instead of 'object' + * + * typeof new Number(...); // 'object' + * typeOf(new Number(...)); // 'number' instead of 'object' * * typeOf(1234); // 'number' * * typeOf({}); // 'object' * - * typeof /regexp/; // 'object' - * typeOf(/regexp/); // 'regexp' instead of 'object' + * typeof new String(...); // 'object' + * typeOf(new String(...)); // 'string' instead of 'object' * * typeOf(''); // 'string' * * typeOf(); // 'undefined' * ``` * + * P.S. + * + * Never use `new Boolean`, `new Number`, or `new String` in the first place. + * * @param {?*} value - The value to determine the type of. * * @return {string} The type of the specified value. */ function typeOf(value?: any): string { - /* - * Dependencies: - * - Core JS API. - */ let typeOfValue: string, typeToString: string; if (value === null) { @@ -84,10 +90,6 @@ function typeOf(value?: any): string { switch (typeToString) { case '[object Arguments]': return 'arguments'; - case '[object Array]': - return 'array'; - case '[object Date]': - return 'date'; case '[object Boolean]': return 'boolean'; case '[object Number]': @@ -100,8 +102,6 @@ function typeOf(value?: any): string { return 'nan'; } return 'number'; - case '[object RegExp]': - return 'regexp'; case '[object String]': return 'string'; }