This package contains a list of default string representations of built-in objects and types of Node.js — see the full list here.
- Simple API
- Comprehensive documentation and test set
- No dependencies
- Includes TypeScript
d.ts
files
NPM
npm install typetags
Yarn
yarn add typetags
In the examples below console.log
and toString
calls are not shown.
The TypeTags
object contains a set of properties, methods and predicates, each representing an object type.
The library exports a single object TypeTags
.
import { TypeTags } from 'typetags'
const o = { foo: 'bar' }
if (TypeTags.get(o) !== TypeTags.Object) {
doSomething()
}
We can use it to check if an object's type tag matches the default tag of a data type.
TypeTags.get({})
// → [object Object]
TypeTags.has(new Map())
// → true
TypeTags.of([1, '2', [3]])
// → [ '[object Number]', '[object String]', '[object Array]' ]
esm
import { TypeTags } from 'typetags'
cjs
const { TypeTags } = require('typetags')
umd
<script src="dist/umd/typetags.min.js"></script>
Every object has a toString()
method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected.
By default, the toString()
method is inherited by every object descended from Object
. If this method is not overridden in a custom object, toString()
returns "[object type]
", where type
is the object type. The following code illustrates this:
const o = new Object()
o.toString() // → [object Object]
Although using toString()
in this way is unreliable, as objects can change the behavior of Object.prototype.toString()
this library might be helpful when you need to compare objects' tags inside a procedure call or if you just need a quick reference.
const { TypeTags } = require('typetags')
function isArray(value) {
return typeof value === 'object' && TypeTags.get(value) === TypeTags.Array
}
jest --coverage
Test Suites: 66 passed, 66 total
Tests: 2 skipped, 417 passed, 419 total
Snapshots: 0 total
Time: 5.342 s
Ran all test suites.
✨ Done in 6.72s.
Using TypeScript? We've got you covered. Check out our d.ts
declaration files.
⚡️ Powered by Moka Tecnologia — < >
with ☕️ by Moa Torres