npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

typesnitch

v0.3.3

Published

A Simple Type Sniffer for JS

Readme

Type Snitch: A Simple Type Sniffer for JS

Have you ever wondered why typeof [] returns 'object' ? I did, and that is why I started the development of typesnitch . Now snitch.type([]) will return 'Array' – isn't that something? Basically, all standard JS prototypes are supported and returned as a string value. Furthermore, the Number prototype is more differentiated (see examples below).

If you find any bugs or have suggestions feel free to help and fork the package.

Methods & Modules

Methods

  • type : Returns the prototype of the value
  • unveil : Tries to convert the value, or 'unveil' it, e.g., numbers in disguise :-)
  • unveilType : combination of type and unveil
  • isType: Check if a value has a specific type

Have a look at the tests for more usage information.

You can use type() to get the prototype of a given input value. The method has a second parameter that can be used to get a more detailed prototype. The detailed parameter is set to true per default.

With unveil() a type conversion will be tried. At the moment it only works with integers, strings, objects and flat arrays that include number or string values. This will, hopefully, change in the future.

Basic Usage

const snitch = require('typesnitch')

// revealing the prototype of the input
const x = '[1, 2, 3]'
snitch.type(x) // 'String'

// trying to convert the input with unveil
const y = snitch.unveil(x)
snitch.type(y) // 'Array'

Handling Objects

Using unveil() at objects is tricky, and will improve in the future. Here is what you can do at the moment:

const z = '{a: 1, 1: "b"}'
snitch.unveil(z) // { a: 1, "1": "b" }

// you can do this
snitch.unveil('{a: 1, 1: "b", c: [1, 2, 3]}')
// returns: { '1': 'b', a: 1, c: [ 1, 2, 3 ] }

// but, you can't do this at the moment
snitch.unveil('{a: 1, 1: "b", c: [1, 2, 3], d: {e: 1, f: 2}}')
// returns: '{a: 1, 1: "b", c: [1, 2, 3], d: {e: 1, f: 2}'

Type Checking

You can use typesnitch for type checking like so:

const { type, unveil, unveilType, isType } = require('typesnitch')

const x = [1, 2, 3]
const y = '[1,2,3]'

type(x) === type(y)
// false

type(x) === type(unveil(y))
// true

// or use the combination
type(x) === unveilType(y)
// true

const z = "hello, world"
isType(z, 'string')
// true

More Examples

snitch.type(1, true) // 'Integer'
snitch.type(1, false) // 'Number'
snitch.type(1.1) // 'Float'
snitch.type(Number.Nan) // 'NaN'
snitch.type(1 / 'a') // 'NaN'
snitch.type(1 / 'a', false) // 'Number'
snitch.type(1 / 0) // 'Infinity'
snitch.type(-1 / 0) // '-Infinity'
snitch.type('hello world') // 'String'
snitch.type([1, 2, 3]) // 'Array'
snitch.type({ a: 1, b: 2 }) // 'Object'
...

Modules

  • convert : Convert data to strings, numbers, arrays, or objects
snitch.convert.toString(1)
// '1'

snitch.convert.toNumber('1')
// 1

snitch.convert.toArray('hello; world', { delimiter: ';' })
// ['hello', 'world']

const x = { a: 1, b: 2 }
snitch.convert.toArray(x, { objectKeys: false })
 // [1, 2]

snitch.convert.toArray(x, { objectKeys: true })
// ['a', 'b']

snitch.convert.toObject(['a', 'b'])
// {'0': 'a', '1': 'b'}
  • detect : Helper functions for single type detection
snitch.detect.isString('1')  // true
snitch.detect.isNumber(1)    // true
snitch.detect.isInteger(1)   // true
snitch.detect.isInteger(1.1) // false
snitch.detect.isFloat(1.1)   // true
snitch.detect.isFloat(1)     // false
...