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

typcheckjs

v1.1.4

Published

Data type checks in Javascript.

Downloads

7

Readme

typcheckjs

Data type checks in Javascript.

npm version Coverage Status Build Status

Introduction

To check what data type something has in javascript is not always the easiest. The language itself provides an operator called typeof for that which works in a straightforward way. Typeof returns a string of what a values data type is, so for an object "object" is returned and for a string "string".

Installation

 npm install --save typcheckjs

Example

const Typcheckjs = require('typcheckjs');

// isString()
const isStringTrue = Typcheckjs.isString('hello');
// isStringTrue === true -> true

// isString()
const isStringFalse = Typcheckjs.isString(123);
// isStringFalse === true -> false

Usage

const Typcheckjs = require('typcheckjs');

// Returns true if a value is a string.
// A string is always a string so this one is easy.
// Except if called with new (new String) typeof will instead return "object".
// So to also include those strings instanceof can be used.
Typcheckjs.isString(value);

// Returns true if a value is really a number.
// From typeof more things than just an ordinary number will return "number" like NaN and Infinity. 
// To know if a value really is a number the function isFinite is also required.
Typcheckjs.isNumber(value);

// Returns true if a value is an array.
// In javascript arrays are not true arrays like in java and in other languages.
// They're actually objects so typeof will return "object" for them.
// To know if something's really an array its constructor can be compared to Array.
Typcheckjs.isArray(value);

// Returns true if a value is a function.
// Functions are functions so here just typeof is enough.
Typcheckjs.isFunction(value);

// Returns true if a value is an object.
// Many things are objects in javascript.
// To know if a value is an object that can have properties and be looped through,
// its constructor can be compared to Object. It doesn't work for objects created from classes,
// then the instanceof operator can be used instead.
Typcheckjs.isObject(value);

// Returns true if a value is null.
// Most times you don't need to check explicitly for null and
// undefined since they're both falsy values.
// However to do it below functions does the trick.
Typcheckjs.isNull(value);

// Returns true if a value is undefined.
// Most times you don't need to check explicitly for null and undefined since they're both falsy values.
Typcheckjs.isUndefined(value);

// Returns true if a value is a boolean.
// For booleans typeof is enough since it returns "boolean" for both true and false.
Typcheckjs.isBoolean(value);

// Returns true if a value is a regexp.
// RegExp's are objects so the only thing needed to check is if the constructor is RegExp.
Typcheckjs.isRegExp(value);

// Returns true if value is an error object.
// Errors in javascript are the same as "exceptions" in many other programming languages.
// They come in a couple different forms like for instance Error, TypeError and RangeError.
// An instanceof statement is enough for them all,
// but just to be extra sure we also check for the "message" property that errors have.
Typcheckjs.isError(value);

// Returns true if value is a date object.
// Date isn't really a data type in javascript.
// But to know if something's a Date object it can be checked with instanceof.
Typcheckjs.isDate(value);

// Returns true if a Symbol.
// In ES6 the new datatype Symbol was added.
// Nicely enough typeof returns "symbol" for it so no more logic is required.
Typcheckjs.isSymbol(value);

Tests

jay@ThinkPad  /usr/share/nginx/html/typcheck   master  npm test

> [email protected] test /usr/share/nginx/html/typcheck
> jest --coverage

 PASS  __tests__/index.test.js
  ✓ isString() should return true (4ms)
  ✓ isString() should return false
  ✓ isNumber() should return true (1ms)
  ✓ isNumber() should return false
  ✓ isArray() should return true
  ✓ isArray() should return false
  ✓ isFunction() should return true (1ms)
  ✓ isFunction() should return false
  ✓ isObject() should return true
  ✓ isObject() should return false
  ✓ isNull() should return true (1ms)
  ✓ isNull() should return false
  ✓ isUndefined() should return true
  ✓ isUndefined() should return false
  ✓ isBoolean() should return true (1ms)
  ✓ isBoolean() should return true
  ✓ isRegExp() should return true
  ✓ isRegExp() should return false
  ✓ isError() should return true (1ms)
  ✓ isError() should return false
  ✓ isDate() should return true
  ✓ isDate() should return false
  ✓ isSymbol() should return true (1ms)
  ✓ isSymbol() should return false

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.js |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       24 passed, 24 total
Snapshots:   0 total
Time:        1.276s
Ran all test suites.

Reference

https://webbjocke.com/javascript-check-data-types/