https://github.com/dawsbot/object-types
Get JavaScript object types
https://github.com/dawsbot/object-types
Last synced: about 1 year ago
JSON representation
Get JavaScript object types
- Host: GitHub
- URL: https://github.com/dawsbot/object-types
- Owner: dawsbot
- License: mit
- Created: 2016-02-12T19:49:41.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-04-06T18:17:14.000Z (about 4 years ago)
- Last Synced: 2024-05-02T05:38:04.695Z (about 2 years ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 9
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Deprecated in favor of [to-type](https://github.com/dawsonbotsford/to-type)
# object-types
> Get Javascript Object types
## Install
```
npm install --save object-types
```
## Usage
```js
const objectTypes = require('object-types');
typeof [];
//=> 'object'
objectTypes([]);
//=> 'array'
typeof new Object(true);
//=> 'object'
objectTypes(new Object(true));
//=> 'boolean'
typeof {};
//=> 'object'
objectTypes({});
//=> '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:
```js
typeof 'example string';
//=> string
typeof new Object('example string');
//=> object
```
It seems like you can treat literal strings the same as object strings:
```js
const myArray = ['a string', new Object('my string object')];
myArray.map(elem => {
return elem.toUpperCase()
});
//=> ['A STRING', 'MY STRING OBJECT']
```
But not always. **Equality operators do not behave the same**.
```js
const myArray = ['a string', new Object('my string object')];
typeof myArray[0] === myArray[1]
//=> false
```
With object-types, you can stop getting `object` for all of your `typeof` calls.
```js
const myArray = [1,2,3];
const obj = new Object([1,2,3]);
typeof myArray === typeof obj;
//=> false
typeof myArray === objectTypes(obj);
//=> 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
* [deep-blue-string](https://github.com/dawsonbotsford/deep-blue-string)
## License
MIT © [dawsonbotsford](http://dawsonbotsford.com)