Huge News!Announcing our $40M Series B led by Abstract Ventures.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 - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

21

index.js

@@ -1,6 +0,6 @@

function ofs(obj) {
function string(obj) {
return {}.toString.call(obj).slice(1, -1).split(' ').pop();
}
function of(obj) {
function construct(obj) {
if ((obj === null) || (obj === undefined)) {

@@ -14,9 +14,16 @@ return obj;

function is(obj, type) {
var typer = (of(type) === String) ? ofs : of
var typer = (construct(type) === String) ? string : construct
return (typer(obj) === type);
};
module.exports = of;
module.exports.of = of;
module.exports.ofs = ofs;
module.exports.is = is;
module.exports = function (obj, type) {
if (arguments.length == 1) {
return construct(obj);
} else {
return is(obj, type);
}
}
module.exports.string = string;
module.exports.is = is;
module.exports.of = construct;
{
"name": "type-of-is",
"version": "1.1.0",
"version": "2.0.0",
"description": "Determine and test types using constructor or {}.toString",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -7,3 +7,3 @@ # Description

1.0.0
2.0.0

@@ -21,3 +21,3 @@ # Installation

"dependencies": {
"type-of-is": "~1.0.0"
"type-of-is": "2.0.0"
}

@@ -29,49 +29,50 @@ }

```
var type = require('type-of-is');
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)
Type(obj) provides constructor type of an object
Type.string(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');
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('hi there ok')); // [Function: String]
console.log(Type(342)); // [Function: Number]
console.log(Type(342)); // [Function: Number]
console.log(Type({})); // [Function: Object]
console.log(Type([1, 2, 3])); // [Function: Array]
console.log(Type(null)); // null
console.log(Type(undefined)); // undefined
console.log(Type(true)); // [Function: Boolean]
console.log(Type(function () {})); // [Function: Function]
console.log(Type(/abcd/)); // [Function: RegExp]
console.log(Type(new Date())); // [Function: Date]
console.log(Type(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.string('hi there ok')); // "String"
console.log(Type.string(342)); // "Number"
console.log(Type.string({})); // "Object"
console.log(Type.string([1, 2, 3])); // "Array"
console.log(Type.string(null)); // "Null"
console.log(Type.string(undefined)); // "Undefined"
console.log(Type.string(true)); // "Boolean"
console.log(Type.string(function () {})); // "Function"
console.log(Type.string(/abcd/)); // "RegExp"
console.log(Type.string(new Date())); // "Date"
console.log(Type.string(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
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 Stringy = Type(s);
var t = new Stringy("hihihi");

@@ -78,0 +79,0 @@ console.log((s == t)); // true

@@ -8,3 +8,3 @@ var Path = require('path');

Asserts({
"Type.of and Type.ofs should properly find types": function () {
"Type, Type.of, and Type.string should properly find types": function () {
var of_expectations = [

@@ -25,9 +25,9 @@ ["hi", String, "String"],

of_expectations.forEach(function(expectation) {
Assert.strictEqual(Type(expectation[0]), expectation[1], "testing of(" + expectation[0] + ")");
Assert.strictEqual(Type.of(expectation[0]), expectation[1], "testing of(" + expectation[0] + ")");
Assert.strictEqual(Type.ofs(expectation[0]), expectation[2], "testing ofs(" + expectation[0] + ")");
Assert.strictEqual(Type(expectation[0]), expectation[1], "testing Type(" + expectation[0] + ")");
Assert.strictEqual(Type.of(expectation[0]), expectation[1], "testing Type.of(" + expectation[0] + ")");
Assert.strictEqual(Type.string(expectation[0]), expectation[2], "testing Type.string(" + expectation[0] + ")");
});
},
"Type.is should properly check types": function () {
"Type and Type.is should properly check types": function () {
var types = [

@@ -102,3 +102,4 @@ String, "String",

group[1].forEach(function(true_type) {
Assert.equal(Type.is(obj, true_type), true, "testing " + obj + ' is ' + true_type);
Assert(Type(obj, true_type), "testing " + obj + ' is ' + true_type);
Assert(Type.is(obj, true_type), "testing " + obj + ' is ' + true_type);
});

@@ -108,2 +109,3 @@

if (group[1].indexOf(false_type) == -1) {
Assert.strictEqual(Type(obj, false_type), false, "testing " + obj + ' is ' + false_type);
Assert.strictEqual(Type.is(obj, false_type), false, "testing " + obj + ' is ' + false_type);

@@ -110,0 +112,0 @@ }

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