New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

type-of-is

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

type-of-is

Determine and test types using constructor.name {}.toString

0.0.2
Source
npm
Version published
Weekly downloads
141K
6.98%
Maintainers
1
Weekly downloads
 
Created
Source

Description

Determine and test types using constructor or Object.toString

Latest Version

0.0.2

Installation

npm install type-of-is

or in package.json

{
  ...
  "dependencies": {
    "type-of-is": "~0.0.2"
  }
}

Usage

var type = require('type-of-is');

type.of(obj)        provides constructor type of an object
type.ofs(obj)       provides type as String from {}.toString
type.is(obj, type)  tests whether obj is of type (constructor or String)
var type = require('type-of-is');

console.log(type.of('hi there ok'));  // [Function: String]
console.log(type.of(342));            // [Function: Number]
console.log(type.of({}));             // [Function: Object]
console.log(type.of([1, 2, 3]));      // [Function: Array]
console.log(type.of(null));           // null
console.log(type.of(undefined));      // undefined
console.log(type.of(true));           // [Function: Boolean]
console.log(type.of(function () {})); // [Function: Function]
console.log(type.of(/abcd/));         // [Function: RegExp]
console.log(type.of(new Date()));     // [Function: Date]
console.log(type.of(new Error()));    // [Function: Error]

console.log(type.ofs('hi there ok'));  // "String"
console.log(type.ofs(342));            // "Number"
console.log(type.ofs({}));             // "Object"
console.log(type.ofs([1, 2, 3]));      // "Array"
console.log(type.ofs(null));           // "Null"
console.log(type.ofs(undefined));      // "Undefined"
console.log(type.ofs(true));           // "Boolean"
console.log(type.ofs(function () {})); // "Function"
console.log(type.ofs(/abcd/));         // "RegExp"
console.log(type.ofs(new Date()));     // "Date"
console.log(type.ofs(new Error()));    // "Error"

console.log(type.is(true, Boolean));      // true
console.log(type.is("1231", Number));     // false
console.log(type.is("1231", String));     // true
console.log(type.is("1231", "String"));   // true
console.log(type.is("1231", Object));     // false
console.log(type.is([], Object));         // false
console.log(type.is({}, Object));         // true
console.log(type.is([], Array));          // true
console.log(type.is(new Date(), Date));   // true
console.log(type.is(new Date(), Object)); // false

var s = "hihihi";
var Stringy = type.of(s);
var t = new Stringy("hihihi");
console.log((s == t));                    // true
console.log((s === t));                   // false

Keywords

type

FAQs

Package last updated on 30 Jul 2012

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