@ibnlanre/typeof
v0.1.0
Published
A utility for retrieving the type of a JS value
Downloads
1
Maintainers
Readme
typeOf
A utility for retrieving the type of a JS value.
Install
npm i @ibnlanre/typeof
Import
// ES6 Import
import typeOf from "@ibnlanre/typeof"
// NodeJS Require
const typeOf = require("@ibnlanre/typeof");
Usage
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.
<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 compared with { quirkMode: true }
results.
Primitives
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
typeOf(Object) // function typeOf(new Function()) // function typeOf((function () { return arguments })()) // arguments typeOf(() => { }) // function
Control Abstractions
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
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
typeOf(globalThis) // global typeOf(globalThis, { quirksMode: true }) // object // NodeJS typeOf(global, { quirksMode: true }) // object typeOf(global), // global
Math
typeOf(Math) // math typeOf(Math, { quirksMode: true }) // object
JSON
typeOf(JSON, { quirksMode: true }) // object typeOf(JSON) // json
Error Objects
typeOf(new Error()) // error typeOf(new SyntaxError()) // error typeOf(new SyntaxError(), { quirksMode: true }) // syntaxerror
Array
typeOf([1, "two"]) // array typeOf(new Array()) // array
Structured Data
typeOf(new ArrayBuffer()) // arraybuffer typeOf(new DataView(new ArrayBuffer())) // dataview
Keyed Collections
typeOf(new Map()) // map typeOf(new WeakMap()) // weakmap typeOf(new Set()) // set typeOf(new WeakSet()) // weakset
Typed DataViews
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
// 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
const testObject = {};
testObject[Symbol.toStringTag] = "customType";
typeOf(testObject, { quirksMode: true }) // object
typeOf(testObject) // customtype