Socket
Socket
Sign inDemoInstall

is-what

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-what - npm Package Compare versions

Comparing version 3.0.1 to 3.1.0

17

dist/index.cjs.js

@@ -33,3 +33,3 @@ 'use strict';

/**
* Returns whether the payload is an plain JavaScript object (excluding special classes or objects with other prototypes)
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*

@@ -39,5 +39,4 @@ * @param {*} payload

*/
function isObject(payload) {
var isObject = getType(payload) === 'Object';
if (!isObject)
function isPlainObject(payload) {
if (getType(payload) !== 'Object')
return false;

@@ -47,2 +46,11 @@ return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype);

/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
function isObject(payload) {
return isPlainObject(payload);
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)

@@ -182,2 +190,3 @@ *

exports.isNull = isNull;
exports.isPlainObject = isPlainObject;
exports.isObject = isObject;

@@ -184,0 +193,0 @@ exports.isAnyObject = isAnyObject;

@@ -29,3 +29,3 @@ /**

/**
* Returns whether the payload is an plain JavaScript object (excluding special classes or objects with other prototypes)
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*

@@ -35,5 +35,4 @@ * @param {*} payload

*/
function isObject(payload) {
var isObject = getType(payload) === 'Object';
if (!isObject)
function isPlainObject(payload) {
if (getType(payload) !== 'Object')
return false;

@@ -43,2 +42,11 @@ return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype);

/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
function isObject(payload) {
return isPlainObject(payload);
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)

@@ -175,2 +183,2 @@ *

export { getType, isUndefined, isNull, isObject, isAnyObject, isObjectLike, isFunction, isArray, isString, isNumber, isBoolean, isRegExp, isDate, isSymbol, isPrimitive, isType };
export { getType, isUndefined, isNull, isPlainObject, isObject, isAnyObject, isObjectLike, isFunction, isArray, isString, isNumber, isBoolean, isRegExp, isDate, isSymbol, isPrimitive, isType };
{
"name": "is-what",
"version": "3.0.1",
"version": "3.1.0",
"description": "JS type check (TypeScript supported) functions like `isObject() isArray()` etc. A simple & small integration.",

@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js",

@@ -13,3 +13,3 @@ # is What? 🙉

isString, isNumber, isBoolean, isObject, isFunction, isArray, isUndefined, isNull, isRegExp, isDate, isSymbol, isPrimitive, isType, getType
isPlainObject, isAnyObject, isString, isNumber, isBoolean, isFunction, isArray, isUndefined, isNull, isRegExp, isDate, isSymbol, isPrimitive, isType, getType

@@ -28,5 +28,7 @@ ### Usage

#### isObject vs isAnyObject
#### isPlainObject vs isAnyObject
Checking for a JavaScript object can be really difficult. In JavaScript you can create classes that will behave just like JavaScript objects but might have completely different prototypes. With is-what I opted for a **strict** check on isObject only returning `true` on plain JavaScript objects and not on classes or others. `isAnyObject` will be more loose and return `true` even if the object is not a regular JavaScript object.
Checking for a JavaScript object can be really difficult. In JavaScript you can create classes that will behave just like JavaScript objects but might have completely different prototypes. With is-what I went for this classification:
- `isPlainObject` will only return `true` on plain JavaScript objects and not on classes or others
- `isAnyObject` will be more loose and return `true` on regular objects, classes, etc.

@@ -43,9 +45,9 @@ ```js

// let's check:
import { isObject, isAnyObject, getType } from 'is-what'
import { isPlainObject, isAnyObject, getType } from 'is-what'
// plainObject
isObject(plainObject) // returns true
isPlainObject(plainObject) // returns true
isAnyObject(plainObject) // returns true
getType(plainObject) // returns 'Object'
// specialObject
isObject(specialObject) // returns false !!!!!!!!!
isPlainObject(specialObject) // returns false !!!!!!!!!
isAnyObject(specialObject) // returns true

@@ -55,3 +57,3 @@ getType(specialObject) // returns 'Object'

> Please note that `isObject` will only return `true` for normal plain JavaScript object.
> Please note that `isPlainObject` will only return `true` for normal plain JavaScript object.

@@ -86,10 +88,10 @@ #### Useful number & date exception:

`isObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:
`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:
```TypeScript
function isObject (payload: any): payload is {[key: string]: any}
function isPlainObject (payload: any): payload is {[key: string]: any}
function isAnyObject (payload: any): payload is {[key: string]: any}
// The reason to return `{[key: string]: any}` is to be able to do
if (isObject(payload) && payload.id) return payload.id
// if isObject() would return `payload is object` then it would give an error at `payload.id`
if (isPlainObject(payload) && payload.id) return payload.id
// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
```

@@ -96,0 +98,0 @@

@@ -32,3 +32,3 @@ /**

/**
* Returns whether the payload is an plain JavaScript object (excluding special classes or objects with other prototypes)
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*

@@ -38,5 +38,4 @@ * @param {*} payload

*/
export function isObject (payload: any): payload is {[key: string]: any} {
const isObject = getType(payload) === 'Object'
if (!isObject) return false
export function isPlainObject (payload: any): payload is {[key: string]: any} {
if (getType(payload) !== 'Object') return false
return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype)

@@ -46,2 +45,12 @@ }

/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
export function isObject (payload: any): payload is {[key: string]: any} {
return isPlainObject(payload)
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)

@@ -48,0 +57,0 @@ *

import {
isObject,
isPlainObject,
isAnyObject,
isUndefined,
isNull,
isObject,
isFunction,

@@ -113,1 +115,47 @@ isArray,

})
test('isObject vs isAnyObject', () => {
function MyClass () {}
// This is correct old fashion syntax for classes, if this is missing
MyClass.prototype.constructor = MyClass
const myClass = new MyClass()
class MyClass2 {}
const myClass2 = new MyClass()
const mySpecialObject = {}
Object.setPrototypeOf(mySpecialObject, {toDate: function () { return new Date() }})
// IS OBJECT
// plain object
expect(isObject({})).toBe(true)
expect(isObject(new Object())).toBe(true)
expect(isPlainObject({})).toBe(true)
expect(isPlainObject(new Object())).toBe(true)
// classes & prototypes
expect(isObject(myClass)).toBe(false)
expect(isObject(myClass2)).toBe(false)
expect(isObject(mySpecialObject)).toBe(false)
expect(isPlainObject(myClass)).toBe(false)
expect(isPlainObject(myClass2)).toBe(false)
expect(isPlainObject(mySpecialObject)).toBe(false)
// arrays and dates
expect(isObject([])).toBe(false)
expect(isObject(new Array())).toBe(false)
expect(isObject(new Date('_'))).toBe(false)
expect(isObject(new Date())).toBe(false)
expect(isPlainObject([])).toBe(false)
expect(isPlainObject(new Array())).toBe(false)
expect(isPlainObject(new Date('_'))).toBe(false)
expect(isPlainObject(new Date())).toBe(false)
// IS ANY OBJECT
// plain object
expect(isAnyObject({})).toBe(true)
expect(isAnyObject(new Object())).toBe(true)
// classes & prototypes
expect(isAnyObject(myClass)).toBe(true)
expect(isAnyObject(myClass2)).toBe(true)
expect(isAnyObject(mySpecialObject)).toBe(true)
// arrays and dates
expect(isAnyObject([])).toBe(false)
expect(isAnyObject(new Array())).toBe(false)
expect(isAnyObject(new Date('_'))).toBe(false)
expect(isAnyObject(new Date())).toBe(false)
})

@@ -23,3 +23,3 @@ /**

/**
* Returns whether the payload is an plain JavaScript object (excluding special classes or objects with other prototypes)
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*

@@ -29,2 +29,11 @@ * @param {*} payload

*/
export declare function isPlainObject(payload: any): payload is {
[key: string]: any;
};
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is {[key: string]: any}}
*/
export declare function isObject(payload: any): payload is {

@@ -31,0 +40,0 @@ [key: string]: any;

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