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

@jsweb/truetype

v4.0.6

Published

Simple JS module to check types consistently and concisely

Downloads

9

Readme

@jsweb/truetype

Simple JS module to check types consistently and concisely.

Tests coverage at https://truetype.jsweb.app

package-npm module-es tests-ava

Why?

Checking types in JavaScript is not so easy...

The builtin operators typeof, instanceof and other methods are not precise enough to report the exact type of a value.

So, this module aims to check types of variables with more useful returns.

New in v4.0.0

Now, its a full ES module, there is no UMD or CommonJS version.

ES modules are the new pattern in modern JS development, already supported in newer versions of Node.js and modern borwsers natively.

Backward compatibility is not a concern here. If you use a module bundler (like Webpack or Rollup) to transpile your code, the result will be compatible according to your setup.


Installation

You can install with NPM, Yarn or Unpkg CDN:

npm i -S @jsweb/truetype

yarn add @jsweb/truetype

pnpm add @jsweb/truetype

Usage

ES6

Tree shaking (since v3.0.0):

import { isInteger, isDate, isNotNull } from '@jsweb/truetype'

From CDN (installation not required)

<script type="module">
  import { isNumber } from 'https://unpkg.com/@jsweb/truetype'

  const number = isNumber(1)
</script>

Methods

isDefined(value: any): boolean

Check if a value is not undefined.

isNull(value: any): boolean

Check if value is null.

isNotNull(value: any): boolean

Check if value is not null.

isValid(value: any): boolean

Check if value is "valid" (is not null or undefined).

instance(value: any): string

Get the constructor name of the value.

Returns a string with a type name like Object, Array, String, Number, ...

Can be a native or custom constructor name.

instance(1) // returns Number

class Foo {
  constructor(x) {
    this.x = x
  }
}

const bar = new Foo(1)

instance(bar) // returns Foo

is(value: any, type: string): boolean

Check if value is of type.

Can be a native or custom constructor name.

is({}, 'Object') // returns true
is([], 'Array') // returns true
is('foo', 'String') // returns true
is(false, 'Boolean') // returns true
// ...

class Foo {
  constructor(x) {
    this.x = x
  }
}

const bar = new Foo(1)

is(bar, 'Foo') // returns true

isBoolean(value: any): boolean

Check if value is a boolean.

isString(value: any): boolean

Check if value is a string.

isNumber(value: any): boolean

Check if value is a number.

isInteger(value: any): boolean

Check if value is an integer number.

isFloat(value: any): boolean

Check if value is a float point number.

isObject(value: any): boolean

Check if value is an object.

isArray(value: any): boolean

Check if value is an Array.

isDate(value: any): boolean

Check if value is a Date object.

isRegExp(value: any): boolean

Check if value is a Regular Expression.

isFunction(value: any): boolean

Check if value is a function.

isEmpty(value: string | array | object): boolean

This is a bonus utility.

Check if value is empty.

Only for string, array and objects. Any other type will return false.