TypeTags
String Representations of Node.js Built-in Objects
This package contains a list of default string representations of built-in objects and types of Node.js — see the full list here.
import { TypeTags } from 'typetags'
TypeTags.Array// → [object Array]
TypeTags.Function// → [object Function]
TypeTags.Float64Array// → [object Float64Array]
TypeTags
can be used with any object to get its class or retrieve its type tag.
TypeTags.get('Hey')// → [object String]
It also has predicates to help us identify an object's type by its type tag.
TypeTags.isArray('yo')// → false
View on GitHub
Installation
NPM
npm install typetags
Yarn
yarn add typetags
API Reference
The TypeTags
object contains a set of properties, methods and predicates, each representing an object type. We can use it to check if an object's type tag matches the default tag of a data type.
import { TypeTags } from 'typetags'
const o = { foo: 'bar' }
if (o.toString() !== TypeTags.String) { doSomething()}
Methods
TypeTags
can help us interact with Node.js built-in types and assign tags to our own objects.
Its .has
method allows us to check if an object has a valid toString
method.
import { TypeTags } from 'typetags'
const proto = {}
TypeTags.has(proto)// → true
proto.toString()// → [object Object]
We can also use both .get
and .of
to retrieve an object's type tag.
const { TypeTags } = require('typetags')
let o = { foo: 'bar' }
const before = TypeTags.of(o)// → '[object Object]'
TypeTags.assign(o, 'Bazzinga')
const after = TypeTags.get(o)// → '[object Bazzinga]'
Predicates
TypeTags
has a predicate for each of Node.js built-in types. There are three generic predicates to help us identify an object by its type group.
Predicate methods are named by adding is
in front of a type's name.
declare type PredicateNames = `is${Types | NestedPredicates}`
To learn more about each predicate visit pages Types
and Predicates
or use the search menu.
Using TypeTags
to detect Object Class
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}
Bundles
esm
import { TypeTags } from 'typetags'
cjs
const { isDefaultTag } = require('typetags')
umd
<script src="dist/umd/typetags.min.js"></script>
Tests
jest --coverage
------------------|---------|----------|---------|---------|-------------------File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s------------------|---------|----------|---------|---------|-------------------All files | 96.02 | 65 | 83.7 | 100 | lib | 98.21 | 53.85 | 57.69 | 100 | assign-tag.js | 100 | 100 | 100 | 100 | constants.js | 100 | 100 | 100 | 100 | get-tag.js | 100 | 100 | 100 | 100 | initializers.js | 100 | 50 | 0 | 100 | 3-142 utils.js | 97.3 | 100 | 91.67 | 100 | lib/resources | 95.17 | 85.71 | 93.94 | 100 | TType.js | 90.79 | 81.82 | 100 | 100 | 73,98-105,129-146 TTypeUtils.js | 100 | 66.67 | 60 | 100 | 52,59,78-80 TextUtils.js | 100 | 100 | 100 | 100 | TypeTags.js | 100 | 100 | 100 | 100 |------------------|---------|----------|---------|---------|-------------------
Test Suites: 66 passed, 66 totalTests: 2 skipped, 417 passed, 419 totalSnapshots: 0 totalTime: 5.342 sRan all test suites.✨ Done in 6.72s.
TypeScript
Using TypeScript? We've got you covered. Check out our d.ts
declaration files. If you'd like to help us improve our type definitions, submit a new issue. Contributions are always welcome. ❤️
< >
with ☕️ by Moa Torres — ⚡️ Powered by Vercel