Comparing version 4.0.0 to 4.1.0
@@ -162,2 +162,20 @@ /** | ||
/** | ||
* Returns whether the payload is a positive number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
function isPositiveNumber(payload) { | ||
return isNumber(payload) && payload > 0; | ||
} | ||
/** | ||
* Returns whether the payload is a negative number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
function isNegativeNumber(payload) { | ||
return isNumber(payload) && payload < 0; | ||
} | ||
/** | ||
* Returns whether the payload is a boolean | ||
@@ -326,2 +344,2 @@ * | ||
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isMap, isNaNValue, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; | ||
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet }; |
{ | ||
"name": "is-what", | ||
"sideEffects": false, | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"typings": "./types/index.d.ts", | ||
"types": "./types/index.d.ts", | ||
"scripts": { | ||
@@ -48,19 +48,19 @@ "test": "ava", | ||
"devDependencies": { | ||
"@babel/core": "^7.13.8", | ||
"@types/babel-core": "^6.25.6", | ||
"@typescript-eslint/eslint-plugin": "^4.16.1", | ||
"@typescript-eslint/parser": "^4.16.1", | ||
"@babel/core": "^7.15.8", | ||
"@types/babel-core": "^6.25.7", | ||
"@typescript-eslint/eslint-plugin": "^5.0.0", | ||
"@typescript-eslint/parser": "^5.0.0", | ||
"ava": "^3.15.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^7.21.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-tree-shaking": "^1.8.0", | ||
"np": "^7.4.0", | ||
"prettier": "^2.2.1", | ||
"eslint": "^8.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-tree-shaking": "^1.9.2", | ||
"np": "^7.5.0", | ||
"prettier": "^2.4.1", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.40.0", | ||
"rollup": "^2.58.0", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.2.3" | ||
"ts-node": "^10.3.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -67,0 +67,0 @@ "engines": { |
114
README.md
@@ -39,2 +39,8 @@ # is What? 🙉 | ||
```js | ||
// basics | ||
isBoolean(true) // true | ||
isBoolean(false) // true | ||
isUndefined(undefined) // true | ||
isNull(null) // true | ||
// strings | ||
@@ -47,4 +53,23 @@ isString('') // true | ||
isNumber(0) // true | ||
isNumber(NaN) // false | ||
isNumber('0') // false | ||
isNumber(NaN) // false * | ||
isPositiveNumber(1) // true | ||
isNegativeNumber(-1) // true | ||
// * see below for special NaN use cases! | ||
// arrays | ||
isArray([]) // true | ||
isEmptyArray([]) // true | ||
isFullArray([1]) // true | ||
// objects | ||
isPlainObject({}) // true * | ||
isEmptyObject({}) // true | ||
isFullObject({ a: 1 }) // true | ||
// * see below for special object (& class instance) use cases! | ||
// functions | ||
isFunction(function () {}) // true | ||
isFunction(() => {}) // true | ||
// dates | ||
@@ -54,8 +79,9 @@ isDate(new Date()) // true | ||
// maps & sets | ||
isMap(new Map()) // true | ||
isSet(new Set()) // true | ||
isWeakMap(new WeakMap()) // true | ||
isWeakSet(new WeakSet()) // true | ||
// others | ||
isBoolean(false) // true | ||
isFunction(function () {}) // true | ||
isArray([]) // true | ||
isUndefined(undefined) // true | ||
isNull(null) // true | ||
isRegExp(/\s/gi) // true | ||
@@ -65,2 +91,4 @@ isSymbol(Symbol()) // true | ||
isFile(new File([''], '', { type: 'text/html' })) // true | ||
isError(new Error('')) // true | ||
isPromise(new Promise((resolve) => {})) // true | ||
@@ -72,14 +100,40 @@ // primitives | ||
### Getting and checking for specific types | ||
### Let's talk about NaN | ||
You can check for specific types with `getType` and `isType`: | ||
`isNaN` is a built-in JS Function but it really makes no sense: | ||
```js | ||
import { getType, isType } from 'is-what' | ||
// 1) | ||
typeof NaN === 'number' // true | ||
// 🤔 ("not a number" is a "number"...) | ||
getType('') // returns 'String' | ||
// pass a Type as second param: | ||
isType('', String) // returns true | ||
// 2) | ||
isNaN('1') // false | ||
// 🤔 the string '1' is not-"not a number"... so it's a number?? | ||
// 3) | ||
isNaN('one') // true | ||
// 🤔 'one' is NaN but `NaN === 'one'` is false... | ||
``` | ||
With is-what the way we treat NaN makes a little bit more sense: | ||
```js | ||
import { isNumber, isNaNValue } from 'is-what' | ||
// 1) | ||
isNumber(NaN) // false! | ||
// let's not treat NaN as a number | ||
// 2) | ||
isNaNValue('1') // false | ||
// if it's not NaN, it's not NaN!! | ||
// 3) | ||
isNaNValue('one') // false | ||
// if it's not NaN, it's not NaN!! | ||
isNaNValue(NaN) // true | ||
``` | ||
### isPlainObject vs isAnyObject | ||
@@ -117,2 +171,14 @@ | ||
### Getting and checking for specific types | ||
You can check for specific types with `getType` and `isType`: | ||
```js | ||
import { getType, isType } from 'is-what' | ||
getType('') // returns 'String' | ||
// pass a Type as second param: | ||
isType('', String) // returns true | ||
``` | ||
## TypeScript | ||
@@ -150,12 +216,26 @@ | ||
If you want more control over which kind of objects are allowed you can use `isObjectLike<T>`: | ||
If you want more control over what kind of interface/type is casted when checking for objects. | ||
To cast to a specific type while checking for `isAnyObject`, can use `isObjectLike<T>`: | ||
```ts | ||
import { isObjectLike } from 'is-what' | ||
// usage examples: | ||
isObjectLike<{ specificKey: string }>(payload) | ||
isObjectLike<object>(payload) | ||
// you can pass a specific type for TS to check on. | ||
const payload = { name: 'Mesqueeb' } // current type: `{ name: string }` | ||
// Without casting: | ||
if (isAnyObject(payload)) { | ||
// in here `payload` is casted to: `Record<string | number | symbol, any>` | ||
// WE LOOSE THE TYPE! | ||
} | ||
// With casting: | ||
// you can pass a specific type for TS that will be casted when the function returns | ||
if (isObjectLike<{ name: string }>(payload)) { | ||
// in here `payload` is casted to: `{ name: string }` | ||
} | ||
``` | ||
Please note: this library will not actually check the shape of the object, you need to do that yourself. | ||
`isObjectLike<T>` works like this under the hood: | ||
@@ -162,0 +242,0 @@ |
@@ -185,2 +185,22 @@ export type AnyFunction = (...args: any[]) => any | ||
/** | ||
* Returns whether the payload is a positive number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
export function isPositiveNumber(payload: any): payload is number { | ||
return isNumber(payload) && payload > 0 | ||
} | ||
/** | ||
* Returns whether the payload is a negative number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
export function isNegativeNumber(payload: any): payload is number { | ||
return isNumber(payload) && payload < 0 | ||
} | ||
/** | ||
* Returns whether the payload is a boolean | ||
@@ -187,0 +207,0 @@ * |
@@ -20,2 +20,4 @@ import test from 'ava' | ||
isNumber, | ||
isPositiveNumber, | ||
isNegativeNumber, | ||
isDate, | ||
@@ -196,2 +198,11 @@ isSymbol, | ||
test('isPositiveNumber / isNegativeNumber', (t: any) => { | ||
t.is(isPositiveNumber(-1), false) | ||
t.is(isPositiveNumber(0), false) | ||
t.is(isPositiveNumber(1), true) | ||
t.is(isNegativeNumber(-1), true) | ||
t.is(isNegativeNumber(0), false) | ||
t.is(isNegativeNumber(1), false) | ||
}) | ||
test('NaN tests', (t: any) => { | ||
@@ -198,0 +209,0 @@ t.is(isNaNValue(NaN), true) |
@@ -133,2 +133,16 @@ export declare type AnyFunction = (...args: any[]) => any; | ||
/** | ||
* Returns whether the payload is a positive number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
export declare function isPositiveNumber(payload: any): payload is number; | ||
/** | ||
* Returns whether the payload is a negative number (but not 0) | ||
* | ||
* @param {*} payload | ||
* @returns {payload is number} | ||
*/ | ||
export declare function isNegativeNumber(payload: any): payload is number; | ||
/** | ||
* Returns whether the payload is a boolean | ||
@@ -135,0 +149,0 @@ * |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55602
1435
273