diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..5e13045 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,9 @@ +{ + "printWidth": 100, + "tabWidth": 2, + "singleQuote": true, + "trailingComma": "es5", + "semi": false, + "bracketSpacing": true, + "quoteProps": "consistent" +} diff --git a/dist/index.cjs.js b/dist/index.cjs.js index 07c60a4..16b3b43 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -38,7 +38,7 @@ function isNull(payload) { function isPlainObject(payload) { if (getType(payload) !== 'Object') return false; - return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype); + 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) @@ -124,7 +124,7 @@ function isEmptyString(payload) { * @returns {payload is number} */ function isNumber(payload) { - return (getType(payload) === 'Number' && !isNaN(payload)); + return getType(payload) === 'Number' && !isNaN(payload); } /** * Returns whether the payload is a boolean @@ -151,7 +151,7 @@ function isRegExp(payload) { * @returns {payload is symbol} */ function isSymbol(payload) { - return (getType(payload) === 'Symbol'); + return getType(payload) === 'Symbol'; } /** * Returns whether the payload is a date, and that the date is Valid @@ -160,7 +160,7 @@ function isSymbol(payload) { * @returns {payload is Date} */ function isDate(payload) { - return (getType(payload) === 'Date' && !isNaN(payload)); + return getType(payload) === 'Date' && !isNaN(payload); } /** * Returns whether the payload is a blob @@ -194,6 +194,15 @@ function isPrimitive(payload) { isString(payload) || isSymbol(payload)); } +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +function isNullOrUndefined(payload) { + return isNull(payload) || isUndefined(payload); +} /** * Does a generic check to check that the given payload is of a given type. * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); @@ -214,7 +223,7 @@ function isType(payload, type) { } // Classes usually have names (as functions usually have names) var name = type.name; - return (getType(payload) === name) || Boolean(payload && (payload.constructor === type)); + return getType(payload) === name || Boolean(payload && payload.constructor === type); } exports.getType = getType; @@ -228,6 +237,7 @@ exports.isFile = isFile; exports.isFullString = isFullString; exports.isFunction = isFunction; exports.isNull = isNull; +exports.isNullOrUndefined = isNullOrUndefined; exports.isNumber = isNumber; exports.isObject = isObject; exports.isObjectLike = isObjectLike; diff --git a/dist/index.esm.js b/dist/index.esm.js index 55bd53f..a9decb2 100644 --- a/dist/index.esm.js +++ b/dist/index.esm.js @@ -34,7 +34,7 @@ function isNull(payload) { function isPlainObject(payload) { if (getType(payload) !== 'Object') return false; - return (payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype); + 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) @@ -120,7 +120,7 @@ function isEmptyString(payload) { * @returns {payload is number} */ function isNumber(payload) { - return (getType(payload) === 'Number' && !isNaN(payload)); + return getType(payload) === 'Number' && !isNaN(payload); } /** * Returns whether the payload is a boolean @@ -147,7 +147,7 @@ function isRegExp(payload) { * @returns {payload is symbol} */ function isSymbol(payload) { - return (getType(payload) === 'Symbol'); + return getType(payload) === 'Symbol'; } /** * Returns whether the payload is a date, and that the date is Valid @@ -156,7 +156,7 @@ function isSymbol(payload) { * @returns {payload is Date} */ function isDate(payload) { - return (getType(payload) === 'Date' && !isNaN(payload)); + return getType(payload) === 'Date' && !isNaN(payload); } /** * Returns whether the payload is a blob @@ -190,6 +190,15 @@ function isPrimitive(payload) { isString(payload) || isSymbol(payload)); } +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +function isNullOrUndefined(payload) { + return isNull(payload) || isUndefined(payload); +} /** * Does a generic check to check that the given payload is of a given type. * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); @@ -210,7 +219,7 @@ function isType(payload, type) { } // Classes usually have names (as functions usually have names) var name = type.name; - return (getType(payload) === name) || Boolean(payload && (payload.constructor === type)); + return getType(payload) === name || Boolean(payload && payload.constructor === type); } -export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyString, isFile, isFullString, isFunction, isNull, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isType, isUndefined }; +export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyString, isFile, isFullString, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isType, isUndefined }; diff --git a/package.json b/package.json index ed266ab..237fce5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "is-what", - "version": "3.4.0", + "version": "3.5.0", "description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/src/index.ts b/src/index.ts index f20a8bc..c8c5925 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,9 +34,9 @@ export function isNull (payload: any): payload is null { * @param {*} payload * @returns {payload is {[key: string]: any}} */ -export function isPlainObject (payload: any): payload is {[key: string]: any} { +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) + return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype } /** @@ -45,7 +45,7 @@ export function isPlainObject (payload: any): payload is {[key: string]: any} { * @param {*} payload * @returns {payload is {[key: string]: any}} */ -export function isObject (payload: any): payload is {[key: string]: any} { +export function isObject (payload: any): payload is { [key: string]: any } { return isPlainObject(payload) } @@ -55,7 +55,7 @@ export function isObject (payload: any): payload is {[key: string]: any} { * @param {*} payload * @returns {payload is {[key: string]: any}} */ -export function isAnyObject (payload: any): payload is {[key: string]: any} { +export function isAnyObject (payload: any): payload is { [key: string]: any } { return getType(payload) === 'Object' } @@ -131,7 +131,7 @@ export function isEmptyString (payload: any): payload is string { * @returns {payload is number} */ export function isNumber (payload: any): payload is number { - return (getType(payload) === 'Number' && !isNaN(payload)) + return getType(payload) === 'Number' && !isNaN(payload) } /** @@ -161,7 +161,7 @@ export function isRegExp (payload: any): payload is RegExp { * @returns {payload is symbol} */ export function isSymbol (payload: any): payload is symbol { - return (getType(payload) === 'Symbol') + return getType(payload) === 'Symbol' } /** @@ -171,7 +171,7 @@ export function isSymbol (payload: any): payload is symbol { * @returns {payload is Date} */ export function isDate (payload: any): payload is Date { - return (getType(payload) === 'Date' && !isNaN(payload)) + return getType(payload) === 'Date' && !isNaN(payload) } /** @@ -200,7 +200,9 @@ export function isFile (payload: any): payload is File { * @param {*} payload * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ -export function isPrimitive (payload: any): payload is boolean | null | undefined | number | string | symbol { +export function isPrimitive ( + payload: any +): payload is boolean | null | undefined | number | string | symbol { return ( isBoolean(payload) || isNull(payload) || @@ -211,6 +213,16 @@ export function isPrimitive (payload: any): payload is boolean | null | undefine ) } +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +export function isNullOrUndefined (payload: any): payload is null | undefined { + return isNull(payload) || isUndefined(payload) +} + /** * Does a generic check to check that the given payload is of a given type. * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!); @@ -231,5 +243,5 @@ export function isType (payload: any, type: T): payload is T } // Classes usually have names (as functions usually have names) const name: string | undefined | null = (type).name - return (getType(payload) === name) || Boolean(payload && (payload.constructor === type)) + return getType(payload) === name || Boolean(payload && payload.constructor === type) } diff --git a/test/index.test.js b/test/index.test.js index 0da602d..848cd9e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,6 +4,7 @@ import { isAnyObject, isUndefined, isNull, + isNullOrUndefined, isFunction, isArray, isString, @@ -23,6 +24,8 @@ import { test('Basic true tests', () => { expect(isUndefined(undefined)).toBe(true) expect(isNull(null)).toBe(true) + expect(isNullOrUndefined(null)).toBe(true) + expect(isNullOrUndefined(undefined)).toBe(true) expect(isObject({})).toBe(true) expect(isObject(new Object())).toBe(true) expect(isFunction(_ => {})).toBe(true) @@ -59,6 +62,7 @@ test('Basic false tests', () => { expect(isBoolean(NaN)).toBe(false) expect(isRegExp(NaN)).toBe(false) expect(isSymbol(NaN)).toBe(false) + expect(isNullOrUndefined(NaN)).toBe(false) }) test('Primitive tests', () => { @@ -131,7 +135,11 @@ test('isObject vs isAnyObject', () => { class MyClass2 {} const myClass2 = new MyClass() const mySpecialObject = {} - Object.setPrototypeOf(mySpecialObject, {toDate: function () { return new Date() }}) + Object.setPrototypeOf(mySpecialObject, { + toDate: function () { + return new Date() + }, + }) // IS OBJECT // plain object expect(isObject({})).toBe(true) diff --git a/types/index.d.ts b/types/index.d.ts index 39822ce..82a9770 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -149,6 +149,13 @@ export declare function isFile(payload: any): payload is File; * @returns {(payload is boolean | null | undefined | number | string | symbol)} */ export declare function isPrimitive(payload: any): payload is boolean | null | undefined | number | string | symbol; +/** + * Returns true whether the payload is null or undefined + * + * @param {*} payload + * @returns {(payload is null | undefined)} + */ +export declare function isNullOrUndefined(payload: any): payload is null | undefined; /** * Does a generic check to check that the given payload is of a given type. * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);