better-typeof
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -8,13 +8,8 @@ const { toString } = {}; | ||
* @param {*} value Value to test | ||
* @param {boolean} [specific] Set to `true` for more specific results | ||
* @return {string} Type of `value` | ||
*/ | ||
const betterTypeof = (value, specific = false) => { | ||
const betterTypeof = (value) => { | ||
const type = typeof value; | ||
if (type === 'number' && Number.isNaN(value)) { | ||
return 'nan'; | ||
} | ||
if (type === 'function' && !specific) { | ||
if (type !== 'object') { | ||
return type; | ||
@@ -21,0 +16,0 @@ } |
@@ -16,3 +16,2 @@ /* Note, there are some environment-dependant quirks when using | ||
*/ | ||
const each = require('lodash/each'); | ||
@@ -22,11 +21,2 @@ const isPlainObject = require('lodash/isPlainObject'); | ||
class Foo { | ||
} | ||
class Bar { | ||
toString() { | ||
return 'Bar'; | ||
} | ||
} | ||
const expectedResults = { | ||
@@ -65,3 +55,4 @@ 'array': { | ||
'octal': 0x10, | ||
'exponential': 1e10 | ||
'exponential': 1e10, | ||
'NaN': NaN, | ||
}, | ||
@@ -71,4 +62,17 @@ 'object': { | ||
'built with constructor': new Object(), | ||
'class instances': new Foo(), | ||
'class instances with a custom "toString" method': new Bar() | ||
'class instances': (() => { | ||
class Foo { | ||
} | ||
return new Foo(); | ||
})(), | ||
'class instances with a custom "toString" method': (() => { | ||
class Foo { | ||
toString() { | ||
return 'Bar'; | ||
} | ||
} | ||
return new Foo(); | ||
})(), | ||
}, | ||
@@ -98,3 +102,2 @@ 'error': { | ||
'promise': Promise.resolve(), | ||
'nan': NaN, | ||
'undefined': undefined, | ||
@@ -110,27 +113,4 @@ 'null': null, | ||
const expectedResultsSpecific = { | ||
...expectedResults, | ||
// Overwrite existing function property | ||
'function': { | ||
'anonymous functions': function () { | ||
}, | ||
'named functions': function foo() { | ||
}, | ||
'arrow functions': () => { | ||
}, | ||
}, | ||
'asyncfunction': { | ||
'async functions': async function () { | ||
}, | ||
'async arrow functions': async () => { | ||
}, | ||
}, | ||
'generatorfunction': { | ||
'generator functions': function*() { | ||
}, | ||
} | ||
}; | ||
const testTypes = (types, fn) => { | ||
each(types, (valuesToTest, expectedType) => { | ||
describe('betterTypeof()', () => { | ||
each(expectedResults, (valuesToTest, expectedType) => { | ||
if (isPlainObject(valuesToTest)) { | ||
@@ -140,3 +120,3 @@ describe(`returns "${expectedType}" for ${expectedType}s that are`, () => { | ||
test(description, () => { | ||
expect(fn(valueToTest)).toBe(expectedType); | ||
expect(betterTypeof(valueToTest)).toBe(expectedType); | ||
}); | ||
@@ -147,15 +127,6 @@ }); | ||
test(`returns "${expectedType}" for ${expectedType} values`, () => { | ||
expect(fn(valuesToTest)).toBe(expectedType); | ||
expect(betterTypeof(valuesToTest)).toBe(expectedType); | ||
}); | ||
} | ||
}); | ||
} | ||
describe('betterTypeof()', () => { | ||
describe('default behaviour', () => { | ||
testTypes(expectedResults, betterTypeof); | ||
}); | ||
describe('with `specific` flag enabled', () => { | ||
testTypes(expectedResultsSpecific, value => betterTypeof(value, true)); | ||
}); | ||
}); |
{ | ||
"name": "better-typeof", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Like typeof, but not awful.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
## What | ||
`better-typeof` is a single JavaScript function that returns the type of a value. | ||
`better-typeof` is a single JavaScript function that returns the type of a value. This is mainly a learning exercise. You should probably use [kind-of](https://www.npmjs.com/package/kind-of) instead. | ||
## Why | ||
The native JavaScript `typeof` operator is just terrible. | ||
The native JavaScript `typeof` operator is drunk and will shout `"object"` at almost any value you give it. | ||
```javascript | ||
typeof {}; // object | ||
typeof /a/; // object | ||
typeof null; // object | ||
typeof []; // object | ||
typeof new String('foo'); // object | ||
typeof {}; // object | ||
typeof /a/; // object | ||
typeof null; // object | ||
typeof []; // object | ||
typeof new String('foo'); // object | ||
``` | ||
@@ -23,7 +24,7 @@ | ||
betterTypeof({}); // object | ||
betterTypeof(/a/); // regexp | ||
betterTypeof(null); // null | ||
betterTypeof([]); // array | ||
betterTypeof({}); // object | ||
betterTypeof(/a/); // regexp | ||
betterTypeof(null); // null | ||
betterTypeof([]); // array | ||
betterTypeof(new String('foo')); // string | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30
4533
135