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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@berish/typeof

v1.0.0

Published

Library for information about the type of any object or primitive

Downloads

9

Readme

@berish/typeof

Library for information about the type of any object or primitive

Installation

$ npm install @berish/typeof --save

or

$ yarn add @berish/typeof

Supports typescript

Before use

import type, { ITypeofHandler, TypeofResult } from '../';

Check Types (Default)

string

console.log(type('')); // === 'string'
console.log(type('hello')); // === 'string'
console.log(type(new String('helloo'))); // === 'string'

number

console.log(type(0)); // === 'number'
console.log(type(-0)); // === 'number'
console.log(type(0xff)); // === 'number'
console.log(type(-3.142)); // === 'number'
console.log(type(Infinity)); // === 'number'
console.log(type(-Infinity)); // === 'number'
console.log(type(NaN)); // === 'number'
console.log(type(Number(53))); // === 'number'
console.log(type(new Number(53))); // === 'number'

boolean

console.log(type(true)); // === 'boolean'
console.log(type(false)); // === 'boolean'
console.log(type(new Boolean(true))); // === 'boolean'

undefined

console.log(type(undefined)); // === 'undefined'

null

console.log(type(null)); // === 'null'

symbol

console.log(type(Symbol())); // === 'symbol'
console.log(type(Symbol.species)); // === 'symbol'

arguments

console.log(
  (function() {
    return type(arguments);
  })(),
); // === 'arguments'

function

console.log(type(function() {})); // === 'function'
console.log(type(new Function())); // === 'function'
console.log(type(class {})); // === 'function'

regexp

console.log(type(/^(.+)$/)); // === 'regexp'
console.log(type(new RegExp('^(.+)$'))); // === 'regexp'

date

console.log(type(new Date())); // === 'date'

set

console.log(type(new Set())); // === 'set'

map

console.log(type(new Map())); // === 'map'

weakset

console.log(type(new WeakSet())); // === 'weakset'

weakmap

console.log(type(new WeakMap())); // === 'weakmap'

array

console.log(type([])); // === 'array'
console.log(type(Array(5))); // === 'array'

object

console.log(type({})); // === 'object'
console.log(type(new Object())); // === 'object'
console.log(type(Object())); // === 'object'
console.log(type(new (class A {})())); // === 'object'

Custom check type (with handler)


class A {
  constructor() {
    this.name = 'Hello';
  }
}
console.log(type(new A())); // === 'object';


type MyType = TypeofResult | 'classAInstance';

const handlerBefore: ITypeofHandler<MyType> = {
  before: true,
  handler: (value, preview) => {
    console.log(value); // === { name: 'Hello' } (as instance of A class);
    console.log(preview); // === 'undefined'
    return value instanceof A;
  },
  typeName: 'classAInstance',
};

console.log(type<MyType>(new A(), [handlerBefore])); // === 'classAInstance'

const handlerAfter: ITypeofHandler<MyType> = {
  handler: (value, preview) => {
    console.log(value); // === { name: 'Hello' } (as instance of A class);
    console.log(preview); // === 'object'
    return value instanceof A;
  },
  typeName: 'classAInstance',
};

console.log(type<MyType>(new A(), [handlerAfter])); // === 'classAInstance'

The difference between handlers before and after

The handler with before: true is executed before the function logic passes, so in the field preview we see the value undefined. But the handler with before: false or before: undefined is executed after the logic of the function passes, so in the field preview we see the value that would be received by default (as if without a handler)