Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kindjs

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kindjs

Precise type-checker for JavaScript

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Kind

A more precise version of JavaScript's typeof

kind() returns a useful name for a variable's type. It breaks down objects into more precise terms: array, null, element, etc.

Examples:

Inputtypeofkind
[1, 2, 3]"object""array"
null"object""null"
document.getElementById('id')"object""element"
document.getElementsByTagName('div')"object""nodelist"
document.createTextNode('')"object""node"
new Date()"object""date"
{}"object""object" (if no special type was detected — see full list below)

Installation

npm: npm install kindjs

Bower: bower install kind

Or just download kind.js and reference it in your page with a <script> tag.

Usage

Available as a CommonJS module (e.g with RequireJS) or as a global method, kind().

Basic

// Objects that aren't *really* objects
kind(null);       // "null"
kind([1, 2, 3]);  // "array"
kind(arguments);  // "arraylike"
kind(new Date()); // "date"
kind(document.getElementById('id'));        // "element"
kind(document.getElementsByTagName('div')); // "nodelist"

// This one's actually just an object
kind({});         // "object"

// Also works on standard types
kind("text");     // "string"
kind(2);          // "number"
kind(alert);      // "function"

"Deep" option

You may add a second, boolean parameter to tell kind to perform a deeper test for some types. For example, instead of "number" it could return "integer" or "float".

kind(1);          // "number"
kind(1, true);    // "integer"

kind(1.21);       // "number"
kind(1.21, true); // "float"

kind(evt);        // "event"
kind(evt, true);  // "mouseevent" (i.e. if the event was a click)

A complete list is noted below

Supported types

  • All standard types normally returned by typeof:
    • function, undefined, boolean
    • string
      • Deep option returns either string or emptystring
    • number
      • Deep option returns either integer or float
  • array
  • arraylike
    • A non-array object with a .length property
  • null
  • element
  • node
    • Deep options from this list (e.g. text, comment)
  • nodelist
  • event
    • Deep options from this list (e.g. mouseevent, keyboardevent)
  • regexp
  • date
  • error
  • errorevent
  • math

Features

  • Works with any type, not just objects
  • Always returns a simple lowercase string, just like the native typeof
  • Handles undefined or undeclared variables
  • Optimized to check for the most common types first
  • Excellent browser support
    • IE 6+ (and maybe older)
    • Chrome
    • Firefox
    • Safari
    • Android 1+
    • Opera
    • Netscape 4 (in theory!)
    • Probably anything that runs JavaScript and supports regular expressions

Keywords

FAQs

Package last updated on 20 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc