Skip to content

Commit

Permalink
Merge branch 'release-1.0.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Hurlburt committed May 8, 2017
2 parents 6a0675d + ab612f5 commit 3be1aab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
22 changes: 11 additions & 11 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`', () => {
Expand All @@ -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"`', () => {
Expand Down Expand Up @@ -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"`', () => {
Expand Down Expand Up @@ -212,4 +212,4 @@ describe('qc-type_of', () => {

});

});
});
40 changes: 20 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,75 @@
* 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
* (function () {
* 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) {
Expand All @@ -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]':
Expand All @@ -100,8 +102,6 @@ function typeOf(value?: any): string {
return 'nan';
}
return 'number';
case '[object RegExp]':
return 'regexp';
case '[object String]':
return 'string';
}
Expand Down

0 comments on commit 3be1aab

Please sign in to comment.