npm package: @ibnlanre/typeof
Description: A utility for retrieving the type of a JS value
Author: Ridwan Olanrewaju
Latest version: 0.1.0

# typeOf

A utility for retrieving the type of a JS value.

## Install

```bash
npm i @ibnlanre/typeof
```

## Import

```javascript
// ES6 Import
import typeOf from "@ibnlanre/typeof"

// NodeJS Require
const typeOf = require("@ibnlanre/typeof");
```

## Usage

```javascript
typeOf([1, "two", 3]) // array
typeOf(JSON, { quirksMode: true }) // object
typeOf(async() => {}) // function
typeOf(new Set([0])[Symbol.iterator]()) // setiterator
```

## Browser

You can also use `typeOf` within the browser, but only as a module.

```html
<script src="http://unpkg.com/@ibnlanre/typeof" type="module">
  // CDN Service
</script>

<script src="./node_modules/@ibnlanre/typeof" type="module">
  // PKG manager
  console.log(typeOf(null)) // null
</script>
```

## Reference

Some [JavaScript Standard Built-ins][js_standard_types] compared with `{ quirkMode: true }` results.

- ### [Primitives][primitive]

  ```javascript
  typeOf(NaN) // number
  typeOf(-Infinity) // number
  typeOf(0) // number
  typeOf(new Number(1)) // number
  typeOf(2n) // bigint
  typeOf(BigInt(3)) // bigint
  typeOf("type") // string
  typeOf(new String("of")) // string
  typeOf(false) // boolean
  typeOf(new Boolean(true)) // boolean
  typeOf(new Object(1 > 2)) // boolean
  typeOf(Symbol("bar")) // symbol
  typeOf(undefined) // undefined
  typeOf(null) // null
  ```

- ### [Functions][function]

  ```javascript
  typeOf(Object) // function
  typeOf(new Function()) // function
  typeOf((function () { return arguments })()) // arguments
  typeOf(() => { }) // function
  ```

- ### [Control Abstractions][control_abstractions]

  ```javascript
  typeOf(function*() {}) // generatorfunction
  typeOf((function*(x) { yield x })(0)) // generator
  typeOf(new Set([0, 9, 8])[Symbol.iterator]()) // setiterator
  typeOf(async() => { await 0 }) // asyncfunction
  ```

- ### [Objects][object]

  ```javascript
  typeOf({ a: "apple", b: "ball" }) // object
  typeOf(new Object()) // object
  typeOf(new (class Library {})(), { quirksMode: true }) // library
  typeOf(new (class Library {})()) // object
  typeOf(/regEx/m) // regexp
  typeOf(new RegExp(/u/, "ig")) // regexp
  typeOf(new Date()) // date
  ```

- ### [Global Properties][global_props]

  ```javascript
  typeOf(globalThis) // global
  typeOf(globalThis, { quirksMode: true }) // object

  // NodeJS
  typeOf(global, { quirksMode: true }) // object
  typeOf(global), // global
  ```

- ### [Math][math]

  ```javascript
  typeOf(Math) // math
  typeOf(Math, { quirksMode: true }) // object
  ```

- ### [JSON][json]

  ```javascript
  typeOf(JSON, { quirksMode: true }) // object
  typeOf(JSON) // json
  ```

- ### [Error Objects][error_objects]

  ```javascript
  typeOf(new Error()) // error
  typeOf(new SyntaxError()) // error
  typeOf(new SyntaxError(), { quirksMode: true }) // syntaxerror
  ```

- ### [Array][array]

  ```javascript
  typeOf([1, "two"]) // array
  typeOf(new Array()) // array
  ```

- ### [Structured Data][structured_data]

  ```javascript
  typeOf(new ArrayBuffer()) // arraybuffer
  typeOf(new DataView(new ArrayBuffer())) // dataview
  ```

- ### [Keyed Collections][keyed_collections]

  ```javascript
  typeOf(new Map()) // map
  typeOf(new WeakMap()) // weakmap
  typeOf(new Set()) // set
  typeOf(new WeakSet()) // weakset
  ```

- ### [Typed DataViews][typed_arrays]

  ```javascript
  typeOf(new Int8Array()) //  int8array
  typeOf(new Uint8Array()) // uint8array
  typeOf(new Uint8ClampedArray()) // uint8clampedarray
  typeOf(new Int16Array()) // int16array
  typeOf(new Uint16Array()) // uint16array
  typeOf(new Int32Array()) // int32array
  typeOf(new Uint32Array()) // uint32array
  typeOf(new Float32Array()) // float32array
  typeOf(new Float64Array()) // float64array
  typeOf(new BigInt64Array()) // bigint64array
  typeOf(new BigUint64Array()) // biguint64array
  ```

- ### [Buffer][buffer]

  ```javascript
  // NodeJS
  typeOf(Buffer.from("abc"), { quirksMode: true }) // buffer
  typeOf(Buffer.from("abc")) // uint8array
  ```

## Custom Type

You may also use `Symbol.toStringTag` to change an object's return value

```js
const testObject = {};
testObject[Symbol.toStringTag] = "customType";
typeOf(testObject, { quirksMode: true }) // object
typeOf(testObject) // customtype
```

[js_standard_types]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
[global_props]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#value_properties
[primitive]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
[control_abstractions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#indexed_collections
[object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[math]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math
[json]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
[error_objects]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#error_objects
[array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[structured_data]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#structured_data
[keyed_collections]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections
[typed_arrays]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays
[buffer]: https://nodejs.org/api/buffer.html