to-type
Deprecated in favor ofobject-types
Get Javascript Object types
Install
npm install --save object-types
Usage
const objectTypes = ; typeof ;//=> 'object';//=> 'array' typeof true;//=> 'object';//=> 'boolean' typeof {};//=> 'object';//=> 'object'
The Problem
JavaScript objects can be of various object types. As an example, a string can be a string or a string can be an object string:
typeof 'example string';//=> string typeof 'example string';//=> object
It seems like you can treat literal strings the same as object strings:
const myArray = 'a string' 'my string object'; myArray;//=> ['A STRING', 'MY STRING OBJECT']
But not always. Equality operators do not behave the same.
const myArray = 'a string' 'my string object'; typeof myArray0 === myArray1//=> false
With object-types, you can stop getting object
for all of your typeof
calls.
const myArray = 123;const obj = 123; typeof myArray === typeof obj;//=> false typeof myArray === ;//=> true
API
objectTypes(obj)
obj
Type: object
Returns the typeof object as a string:
- 'object'
- 'string'
- 'number'
- 'boolean'
- 'array'
- 'date'
- 'regexp'
- throws
TypeError
if unknown object type is inputted
Related
License
MIT © dawsonbotsford