avancement planning

This commit is contained in:
2026-05-26 11:58:39 +02:00
parent 619a2b240a
commit 150b97cd2e
4892 changed files with 99214 additions and 429382 deletions
-3
View File
@@ -1,3 +0,0 @@
{
"presets": ["env"]
}
-9
View File
@@ -1,9 +0,0 @@
# don't ever lint node_modules
node_modules
# don't lint build output (make sure it's set to your correct build folder name)
dist
# don't lint nyc coverage output
coverage
test
.eslintrc.js
-18
View File
@@ -1,18 +0,0 @@
// npm i -D @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint eslint-config-prettier eslint-plugin-tree-shaking
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'tree-shaking'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'tree-shaking/no-side-effects-in-initialization': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off'
},
}
-12
View File
@@ -1,12 +0,0 @@
# These are supported funding model platforms
github: mesqueeb
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
-9
View File
@@ -1,9 +0,0 @@
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"semi": false,
"bracketSpacing": true,
"quoteProps": "consistent"
}
-9
View File
@@ -1,9 +0,0 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.tabSize": 2, // make sure this is the same as .prettierrc
"editor.insertSpaces": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
}
+1 -1
View File
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
+136 -33
View File
@@ -1,6 +1,9 @@
# is What? 🙉
Very simple & small JS type check functions. It's fully TypeScript supported!
<a href="https://www.npmjs.com/package/is-what"><img src="https://img.shields.io/npm/v/is-what.svg" alt="Total Downloads"></a>
<a href="https://www.npmjs.com/package/is-what"><img src="https://img.shields.io/npm/dw/is-what.svg" alt="Latest Stable Version"></a>
Very simple & small JS type check functions. It's fully TypeScript supported!
```
npm i is-what
@@ -8,11 +11,14 @@ npm i is-what
Or for deno available at: `"deno.land/x/is_what"`
> Also check out [is-where 🙈](https://github.com/mesqueeb/is-where)
## Motivation
I built is-what because the existing solutions were all too complex or too poorly built.
I was looking for:
- A simple way to check any kind of type (including non-primitives)
- Be able to check if an object is a plain object `{}` or a special object (like a class instance) ‼️
- Let TypeScript automatically know what type a value is when checking
@@ -35,6 +41,12 @@ import { isString, isDate, isPlainObject } from 'is-what'
### Simple type check functions
```js
// basics
isBoolean(true) // true
isBoolean(false) // true
isUndefined(undefined) // true
isNull(null) // true
// strings
isString('') // true
isEmptyString('') // true
@@ -42,38 +54,86 @@ isFullString('') // false
// numbers
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
isDate(new Date()) // true
isDate(new Date('invalid date')) // false
// 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
isSymbol(Symbol()) // true
isBlob(new Blob()) // true
isFile(new File([''], '', { type: 'text/html' })) // true
isError(new Error('')) // true
isPromise(new Promise((resolve) => {})) // true
// primitives
isPrimitive('') // true
// true for any of: boolean, null, undefined, number, string, symbol
```
### 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
@@ -85,11 +145,11 @@ Checking for a JavaScript object can be really difficult. In JavaScript you can
```js
// define a plain object
const plainObject = {hello: 'I am a good old object.'}
const plainObject = { hello: 'I am a good old object.' }
// define a special object
class SpecialObject {
constructor (somethingSpecial) {
constructor(somethingSpecial) {
this.speciality = somethingSpecial
}
}
@@ -108,18 +168,42 @@ getType(specialObject) // returns 'Object'
> Please note that `isPlainObject` will only return `true` for normal plain JavaScript objects.
### 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
```
If you just want to make sure your object _inherits_ from a particular class or
`toStringTag` value, you can use `isInstanceOf()` like this:
```js
import { isInstanceOf } from 'is-what'
isInstanceOf(new XMLHttpRequest(), 'EventTarget')
// returns true
isInstanceOf(globalThis, ReadableStream)
// returns false
```
## TypeScript
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.
```ts
function isNumber (payload: any): payload is number {
function isNumber(payload: any): payload is number {
// return boolean
}
// As you can see above, all functions return a boolean for JavaScript, but pass the payload type to TypeScript.
// usage example:
function fn (payload: string | number): number {
function fn(payload: string | number): number {
if (isNumber(payload)) {
// ↑ TypeScript already knows payload is a number here!
return payload
@@ -131,8 +215,8 @@ function fn (payload: string | number): number {
`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:
```ts
function isPlainObject (payload: any): payload is {[key: string]: any}
function isAnyObject (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 (isPlainObject(payload) && payload.id) return payload.id
// if isPlainObject() would return `payload is object` then it would give an error at `payload.id`
@@ -140,52 +224,71 @@ if (isPlainObject(payload) && payload.id) return payload.id
### isObjectLike
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:
```ts
function isObjectLike<T extends object> (payload: any): payload is T {
function isObjectLike<T extends object>(payload: any): payload is T {
return isAnyObject(payload)
}
```
## Meet the family
## Meet the family (more tiny utils with TS support)
- [is-what 🙉](https://github.com/mesqueeb/is-what)
- [is-where 🙈](https://github.com/mesqueeb/is-where)
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
- [find-and-replace-anything 🎣](https://github.com/mesqueeb/find-and-replace-anything)
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
## Source code
It's litterally just these functions:
```js
function getType (payload) {
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1)
}
function isUndefined (payload) {
function isUndefined(payload) {
return getType(payload) === 'Undefined'
}
function isString (payload) {
function isString(payload) {
return getType(payload) === 'String'
}
function isAnyObject (payload) {
function isAnyObject(payload) {
return getType(payload) === 'Object'
}
// etc...
```
See the full source code [here](https://github.com/mesqueeb/is-what/blob/master/src/index.ts).
See the full source code [here](https://github.com/mesqueeb/is-what/blob/production/src/index.ts).
-60
View File
@@ -1,60 +0,0 @@
/* eslint-disable */
// npm install rollup-plugin-typescript2 typescript --save-dev
import typescript from 'rollup-plugin-typescript2'
// import { terser } from 'rollup-plugin-terser'
// import resolve from 'rollup-plugin-node-resolve'
// ------------------------------------------------------------------------------------------
// formats
// ------------------------------------------------------------------------------------------
// amd Asynchronous Module Definition, used with module loaders like RequireJS
// cjs CommonJS, suitable for Node and Browserify/Webpack
// esm Keep the bundle as an ES module file
// iife A self-executing function, suitable for inclusion as a <script> tag. (If you want to create a bundle for your application, you probably want to use this, because it leads to smaller file sizes.)
// umd Universal Module Definition, works as amd, cjs and iife all in one
// system Native format of the SystemJS loader
// ------------------------------------------------------------------------------------------
// setup
// ------------------------------------------------------------------------------------------
const pkg = require('./package.json')
const name = pkg.name
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
const external = Object.keys(pkg.dependencies || [])
const plugins = [
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }),
]
// ------------------------------------------------------------------------------------------
// Builds
// ------------------------------------------------------------------------------------------
function defaults (config) {
// defaults
const defaults = {
plugins,
external,
}
// defaults.output
config.output = config.output.map(output => {
return Object.assign(
{
sourcemap: false,
name: className,
exports: 'named',
},
output
)
})
return Object.assign(defaults, config)
}
export default [
defaults({
input: 'src/index.ts',
output: [
{ file: 'dist/index.cjs.js', format: 'cjs' },
{ file: 'dist/index.esm.js', format: 'esm' },
],
}),
]
-364
View File
@@ -1,364 +0,0 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1);
}
/**
* Returns whether the payload is undefined
*
* @param {*} payload
* @returns {payload is undefined}
*/
function isUndefined(payload) {
return getType(payload) === 'Undefined';
}
/**
* Returns whether the payload is null
*
* @param {*} payload
* @returns {payload is null}
*/
function isNull(payload) {
return getType(payload) === 'Null';
}
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isPlainObject(payload) {
if (getType(payload) !== 'Object')
return false;
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 PlainObject}
*/
function isObject(payload) {
return isPlainObject(payload);
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is { [K in any]: never }}
*/
function isEmptyObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length === 0;
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isFullObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length > 0;
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isAnyObject(payload) {
return getType(payload) === 'Object';
}
/**
* Returns whether the payload is an object like a type passed in < >
*
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
*
* @template T this must be passed in < >
* @param {*} payload
* @returns {payload is T}
*/
function isObjectLike(payload) {
return isAnyObject(payload);
}
/**
* Returns whether the payload is a function (regular or async)
*
* @param {*} payload
* @returns {payload is AnyFunction}
*/
function isFunction(payload) {
return typeof payload === 'function';
}
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
function isArray(payload) {
return getType(payload) === 'Array';
}
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {*} payload
* @returns {payload is any[]}
*/
function isFullArray(payload) {
return isArray(payload) && payload.length > 0;
}
/**
* Returns whether the payload is a an empty array
*
* @param {*} payload
* @returns {payload is []}
*/
function isEmptyArray(payload) {
return isArray(payload) && payload.length === 0;
}
/**
* Returns whether the payload is a string
*
* @param {*} payload
* @returns {payload is string}
*/
function isString(payload) {
return getType(payload) === 'String';
}
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isFullString(payload) {
return isString(payload) && payload !== '';
}
/**
* Returns whether the payload is ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isEmptyString(payload) {
return payload === '';
}
/**
* Returns whether the payload is a number (but not NaN)
*
* This will return `false` for `NaN`!!
*
* @param {*} payload
* @returns {payload is number}
*/
function isNumber(payload) {
return getType(payload) === 'Number' && !isNaN(payload);
}
/**
* Returns whether the payload is a boolean
*
* @param {*} payload
* @returns {payload is boolean}
*/
function isBoolean(payload) {
return getType(payload) === 'Boolean';
}
/**
* Returns whether the payload is a regular expression (RegExp)
*
* @param {*} payload
* @returns {payload is RegExp}
*/
function isRegExp(payload) {
return getType(payload) === 'RegExp';
}
/**
* Returns whether the payload is a Map
*
* @param {*} payload
* @returns {payload is Map<any, any>}
*/
function isMap(payload) {
return getType(payload) === 'Map';
}
/**
* Returns whether the payload is a WeakMap
*
* @param {*} payload
* @returns {payload is WeakMap<any, any>}
*/
function isWeakMap(payload) {
return getType(payload) === 'WeakMap';
}
/**
* Returns whether the payload is a Set
*
* @param {*} payload
* @returns {payload is Set<any>}
*/
function isSet(payload) {
return getType(payload) === 'Set';
}
/**
* Returns whether the payload is a WeakSet
*
* @param {*} payload
* @returns {payload is WeakSet<any>}
*/
function isWeakSet(payload) {
return getType(payload) === 'WeakSet';
}
/**
* Returns whether the payload is a Symbol
*
* @param {*} payload
* @returns {payload is symbol}
*/
function isSymbol(payload) {
return getType(payload) === 'Symbol';
}
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {*} payload
* @returns {payload is Date}
*/
function isDate(payload) {
return getType(payload) === 'Date' && !isNaN(payload);
}
/**
* Returns whether the payload is a Blob
*
* @param {*} payload
* @returns {payload is Blob}
*/
function isBlob(payload) {
return getType(payload) === 'Blob';
}
/**
* Returns whether the payload is a File
*
* @param {*} payload
* @returns {payload is File}
*/
function isFile(payload) {
return getType(payload) === 'File';
}
/**
* Returns whether the payload is a Promise
*
* @param {*} payload
* @returns {payload is Promise<any>}
*/
function isPromise(payload) {
return getType(payload) === 'Promise';
}
/**
* Returns whether the payload is an Error
*
* @param {*} payload
* @returns {payload is Error}
*/
function isError(payload) {
return getType(payload) === 'Error';
}
/**
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
*
* @param {*} payload
* @returns {payload is typeof NaN}
*/
function isNaNValue(payload) {
return getType(payload) === 'Number' && isNaN(payload);
}
/**
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
*
* @param {*} payload
* @returns {(payload is boolean | null | undefined | number | string | symbol)}
*/
function isPrimitive(payload) {
return (isBoolean(payload) ||
isNull(payload) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload));
}
/**
* Returns true whether the payload is null or undefined
*
* @param {*} payload
* @returns {(payload is null | undefined)}
*/
var isNullOrUndefined = isOneOf(isNull, isUndefined);
function isOneOf(a, b, c, d, e) {
return function (value) {
return a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
};
}
/**
* 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!);
* It will, however, differentiate between object and null
*
* @template T
* @param {*} payload
* @param {T} type
* @throws {TypeError} Will throw type error if type is an invalid type
* @returns {payload is T}
*/
function isType(payload, type) {
if (!(type instanceof Function)) {
throw new TypeError('Type must be a function');
}
if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
throw new TypeError('Type is not a class');
}
// Classes usually have names (as functions usually have names)
var name = type.name;
return getType(payload) === name || Boolean(payload && payload.constructor === type);
}
exports.getType = getType;
exports.isAnyObject = isAnyObject;
exports.isArray = isArray;
exports.isBlob = isBlob;
exports.isBoolean = isBoolean;
exports.isDate = isDate;
exports.isEmptyArray = isEmptyArray;
exports.isEmptyObject = isEmptyObject;
exports.isEmptyString = isEmptyString;
exports.isError = isError;
exports.isFile = isFile;
exports.isFullArray = isFullArray;
exports.isFullObject = isFullObject;
exports.isFullString = isFullString;
exports.isFunction = isFunction;
exports.isMap = isMap;
exports.isNaNValue = isNaNValue;
exports.isNull = isNull;
exports.isNullOrUndefined = isNullOrUndefined;
exports.isNumber = isNumber;
exports.isObject = isObject;
exports.isObjectLike = isObjectLike;
exports.isOneOf = isOneOf;
exports.isPlainObject = isPlainObject;
exports.isPrimitive = isPrimitive;
exports.isPromise = isPromise;
exports.isRegExp = isRegExp;
exports.isSet = isSet;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.isType = isType;
exports.isUndefined = isUndefined;
exports.isWeakMap = isWeakMap;
exports.isWeakSet = isWeakSet;
-327
View File
@@ -1,327 +0,0 @@
/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
function getType(payload) {
return Object.prototype.toString.call(payload).slice(8, -1);
}
/**
* Returns whether the payload is undefined
*
* @param {*} payload
* @returns {payload is undefined}
*/
function isUndefined(payload) {
return getType(payload) === 'Undefined';
}
/**
* Returns whether the payload is null
*
* @param {*} payload
* @returns {payload is null}
*/
function isNull(payload) {
return getType(payload) === 'Null';
}
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isPlainObject(payload) {
if (getType(payload) !== 'Object')
return false;
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 PlainObject}
*/
function isObject(payload) {
return isPlainObject(payload);
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is { [K in any]: never }}
*/
function isEmptyObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length === 0;
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isFullObject(payload) {
return isPlainObject(payload) && Object.keys(payload).length > 0;
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
function isAnyObject(payload) {
return getType(payload) === 'Object';
}
/**
* Returns whether the payload is an object like a type passed in < >
*
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
*
* @template T this must be passed in < >
* @param {*} payload
* @returns {payload is T}
*/
function isObjectLike(payload) {
return isAnyObject(payload);
}
/**
* Returns whether the payload is a function (regular or async)
*
* @param {*} payload
* @returns {payload is AnyFunction}
*/
function isFunction(payload) {
return typeof payload === 'function';
}
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
function isArray(payload) {
return getType(payload) === 'Array';
}
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {*} payload
* @returns {payload is any[]}
*/
function isFullArray(payload) {
return isArray(payload) && payload.length > 0;
}
/**
* Returns whether the payload is a an empty array
*
* @param {*} payload
* @returns {payload is []}
*/
function isEmptyArray(payload) {
return isArray(payload) && payload.length === 0;
}
/**
* Returns whether the payload is a string
*
* @param {*} payload
* @returns {payload is string}
*/
function isString(payload) {
return getType(payload) === 'String';
}
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isFullString(payload) {
return isString(payload) && payload !== '';
}
/**
* Returns whether the payload is ''
*
* @param {*} payload
* @returns {payload is string}
*/
function isEmptyString(payload) {
return payload === '';
}
/**
* Returns whether the payload is a number (but not NaN)
*
* This will return `false` for `NaN`!!
*
* @param {*} payload
* @returns {payload is number}
*/
function isNumber(payload) {
return getType(payload) === 'Number' && !isNaN(payload);
}
/**
* Returns whether the payload is a boolean
*
* @param {*} payload
* @returns {payload is boolean}
*/
function isBoolean(payload) {
return getType(payload) === 'Boolean';
}
/**
* Returns whether the payload is a regular expression (RegExp)
*
* @param {*} payload
* @returns {payload is RegExp}
*/
function isRegExp(payload) {
return getType(payload) === 'RegExp';
}
/**
* Returns whether the payload is a Map
*
* @param {*} payload
* @returns {payload is Map<any, any>}
*/
function isMap(payload) {
return getType(payload) === 'Map';
}
/**
* Returns whether the payload is a WeakMap
*
* @param {*} payload
* @returns {payload is WeakMap<any, any>}
*/
function isWeakMap(payload) {
return getType(payload) === 'WeakMap';
}
/**
* Returns whether the payload is a Set
*
* @param {*} payload
* @returns {payload is Set<any>}
*/
function isSet(payload) {
return getType(payload) === 'Set';
}
/**
* Returns whether the payload is a WeakSet
*
* @param {*} payload
* @returns {payload is WeakSet<any>}
*/
function isWeakSet(payload) {
return getType(payload) === 'WeakSet';
}
/**
* Returns whether the payload is a Symbol
*
* @param {*} payload
* @returns {payload is symbol}
*/
function isSymbol(payload) {
return getType(payload) === 'Symbol';
}
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {*} payload
* @returns {payload is Date}
*/
function isDate(payload) {
return getType(payload) === 'Date' && !isNaN(payload);
}
/**
* Returns whether the payload is a Blob
*
* @param {*} payload
* @returns {payload is Blob}
*/
function isBlob(payload) {
return getType(payload) === 'Blob';
}
/**
* Returns whether the payload is a File
*
* @param {*} payload
* @returns {payload is File}
*/
function isFile(payload) {
return getType(payload) === 'File';
}
/**
* Returns whether the payload is a Promise
*
* @param {*} payload
* @returns {payload is Promise<any>}
*/
function isPromise(payload) {
return getType(payload) === 'Promise';
}
/**
* Returns whether the payload is an Error
*
* @param {*} payload
* @returns {payload is Error}
*/
function isError(payload) {
return getType(payload) === 'Error';
}
/**
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
*
* @param {*} payload
* @returns {payload is typeof NaN}
*/
function isNaNValue(payload) {
return getType(payload) === 'Number' && isNaN(payload);
}
/**
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
*
* @param {*} payload
* @returns {(payload is boolean | null | undefined | number | string | symbol)}
*/
function isPrimitive(payload) {
return (isBoolean(payload) ||
isNull(payload) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload));
}
/**
* Returns true whether the payload is null or undefined
*
* @param {*} payload
* @returns {(payload is null | undefined)}
*/
var isNullOrUndefined = isOneOf(isNull, isUndefined);
function isOneOf(a, b, c, d, e) {
return function (value) {
return a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
};
}
/**
* 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!);
* It will, however, differentiate between object and null
*
* @template T
* @param {*} payload
* @param {T} type
* @throws {TypeError} Will throw type error if type is an invalid type
* @returns {payload is T}
*/
function isType(payload, type) {
if (!(type instanceof Function)) {
throw new TypeError('Type must be a function');
}
if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
throw new TypeError('Type is not a class');
}
// Classes usually have names (as functions usually have names)
var name = type.name;
return getType(payload) === name || Boolean(payload && payload.constructor === type);
}
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 };
+87 -45
View File
@@ -1,19 +1,36 @@
{
"name": "is-what",
"sideEffects": false,
"version": "3.14.1",
"version": "4.1.16",
"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",
"typings": "types/index.d.ts",
"type": "module",
"sideEffects": false,
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"main": "./dist/index.js",
"exports": {
".": {
"require": {
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
},
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"engines": {
"node": ">=12.13"
},
"scripts": {
"test": "ava",
"jest": "jest",
"jest-w": "jest --watchAll",
"lint": "tsc --noEmit src/index.ts && eslint . --ext .js,.jsx,.ts,.tsx",
"rollup": "rollup -c ./build.js",
"build": "rimraf types && rimraf dist && npm run lint && npm run rollup && npm run test && npm run jest",
"release": "npm run build && np"
"test": "vitest run",
"lint": "tsc --noEmit && eslint ./src --ext .ts",
"build": "rollup -c ./rollup.config.js",
"build:docs": "typedoc",
"release": "npm run lint && del dist && npm run build && np"
},
"repository": {
"type": "git",
@@ -41,46 +58,71 @@
"is-plain-object"
],
"author": "Luca Ban - Mesqueeb",
"funding": "https://github.com/sponsors/mesqueeb",
"license": "MIT",
"bugs": {
"url": "https://github.com/mesqueeb/is-what/issues"
},
"homepage": "https://github.com/mesqueeb/is-what#readme",
"devDependencies": {
"@babel/core": "^7.12.17",
"@types/babel-core": "^6.25.6",
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
"ava": "^3.15.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^26.6.3",
"babel-preset-env": "^1.7.0",
"eslint": "^7.20.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-tree-shaking": "^1.8.0",
"jest": "^26.6.3",
"np": "^7.4.0",
"prettier": "^2.2.1",
"regenerator-runtime": "^0.13.7",
"rimraf": "^3.0.2",
"rollup": "^2.39.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.5"
},
"ava": {
"extensions": [
"ts"
],
"require": [
"tsconfig-paths/register",
"ts-node/register"
]
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"del-cli": "^5.1.0",
"eslint-config-prettier": "^8.10.0",
"eslint-plugin-tree-shaking": "^1.10.1",
"eslint": "^8.52.0",
"np": "^8.0.4",
"prettier-plugin-jsdoc": "^0.4.2",
"prettier": "^2.8.8",
"rollup-plugin-dts": "^5.3.1",
"rollup-plugin-esbuild": "^5.0.0",
"rollup": "^3.29.4",
"typedoc": "^0.25.2",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
},
"np": {
"yarn": false,
"branch": "production"
"branch": "production",
"publish": false,
"yarn": false
},
"eslintConfig": {
"ignorePatterns": [
"node_modules",
"dist",
"scripts",
"test"
],
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"tree-shaking"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-ignore": "off",
"tree-shaking/no-side-effects-in-initialization": "error",
"@typescript-eslint/ban-ts-comment": "off"
}
},
"prettier": {
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "es5",
"semi": false,
"bracketSpacing": true,
"quoteProps": "consistent",
"plugins": [
"prettier-plugin-jsdoc"
]
}
}
-395
View File
@@ -1,395 +0,0 @@
export type AnyFunction = (...args: any[]) => any
export type AnyAsyncFunction = (...args: any[]) => Promise<any>
export type AnyClass = new (...args: any[]) => any
export type PlainObject = Record<string | number | symbol, any>
type TypeGuard<A, B extends A> = (payload: A) => payload is B
/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
export function getType(payload: any): string {
return Object.prototype.toString.call(payload).slice(8, -1)
}
/**
* Returns whether the payload is undefined
*
* @param {*} payload
* @returns {payload is undefined}
*/
export function isUndefined(payload: any): payload is undefined {
return getType(payload) === 'Undefined'
}
/**
* Returns whether the payload is null
*
* @param {*} payload
* @returns {payload is null}
*/
export function isNull(payload: any): payload is null {
return getType(payload) === 'Null'
}
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export function isPlainObject(payload: any): payload is PlainObject {
if (getType(payload) !== 'Object') return false
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 PlainObject}
*/
export function isObject(payload: any): payload is PlainObject {
return isPlainObject(payload)
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is { [K in any]: never }}
*/
export function isEmptyObject(payload: any): payload is { [K in any]: never } {
return isPlainObject(payload) && Object.keys(payload).length === 0
}
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export function isFullObject(payload: any): payload is PlainObject {
return isPlainObject(payload) && Object.keys(payload).length > 0
}
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export function isAnyObject(payload: any): payload is PlainObject {
return getType(payload) === 'Object'
}
/**
* Returns whether the payload is an object like a type passed in < >
*
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
*
* @template T this must be passed in < >
* @param {*} payload
* @returns {payload is T}
*/
export function isObjectLike<T extends PlainObject>(payload: any): payload is T {
return isAnyObject(payload)
}
/**
* Returns whether the payload is a function (regular or async)
*
* @param {*} payload
* @returns {payload is AnyFunction}
*/
export function isFunction(payload: any): payload is AnyFunction {
return typeof payload === 'function'
}
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
export function isArray(payload: any): payload is any[] {
return getType(payload) === 'Array'
}
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {*} payload
* @returns {payload is any[]}
*/
export function isFullArray(payload: any): payload is any[] {
return isArray(payload) && payload.length > 0
}
/**
* Returns whether the payload is a an empty array
*
* @param {*} payload
* @returns {payload is []}
*/
export function isEmptyArray(payload: any): payload is [] {
return isArray(payload) && payload.length === 0
}
/**
* Returns whether the payload is a string
*
* @param {*} payload
* @returns {payload is string}
*/
export function isString(payload: any): payload is string {
return getType(payload) === 'String'
}
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {*} payload
* @returns {payload is string}
*/
export function isFullString(payload: any): payload is string {
return isString(payload) && payload !== ''
}
/**
* Returns whether the payload is ''
*
* @param {*} payload
* @returns {payload is string}
*/
export function isEmptyString(payload: any): payload is string {
return payload === ''
}
/**
* Returns whether the payload is a number (but not NaN)
*
* This will return `false` for `NaN`!!
*
* @param {*} payload
* @returns {payload is number}
*/
export function isNumber(payload: any): payload is number {
return getType(payload) === 'Number' && !isNaN(payload)
}
/**
* Returns whether the payload is a boolean
*
* @param {*} payload
* @returns {payload is boolean}
*/
export function isBoolean(payload: any): payload is boolean {
return getType(payload) === 'Boolean'
}
/**
* Returns whether the payload is a regular expression (RegExp)
*
* @param {*} payload
* @returns {payload is RegExp}
*/
export function isRegExp(payload: any): payload is RegExp {
return getType(payload) === 'RegExp'
}
/**
* Returns whether the payload is a Map
*
* @param {*} payload
* @returns {payload is Map<any, any>}
*/
export function isMap(payload: any): payload is Map<any, any> {
return getType(payload) === 'Map'
}
/**
* Returns whether the payload is a WeakMap
*
* @param {*} payload
* @returns {payload is WeakMap<any, any>}
*/
export function isWeakMap(payload: any): payload is WeakMap<any, any> {
return getType(payload) === 'WeakMap'
}
/**
* Returns whether the payload is a Set
*
* @param {*} payload
* @returns {payload is Set<any>}
*/
export function isSet(payload: any): payload is Set<any> {
return getType(payload) === 'Set'
}
/**
* Returns whether the payload is a WeakSet
*
* @param {*} payload
* @returns {payload is WeakSet<any>}
*/
export function isWeakSet(payload: any): payload is WeakSet<any> {
return getType(payload) === 'WeakSet'
}
/**
* Returns whether the payload is a Symbol
*
* @param {*} payload
* @returns {payload is symbol}
*/
export function isSymbol(payload: any): payload is symbol {
return getType(payload) === 'Symbol'
}
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {*} payload
* @returns {payload is Date}
*/
export function isDate(payload: any): payload is Date {
return getType(payload) === 'Date' && !isNaN(payload)
}
/**
* Returns whether the payload is a Blob
*
* @param {*} payload
* @returns {payload is Blob}
*/
export function isBlob(payload: any): payload is Blob {
return getType(payload) === 'Blob'
}
/**
* Returns whether the payload is a File
*
* @param {*} payload
* @returns {payload is File}
*/
export function isFile(payload: any): payload is File {
return getType(payload) === 'File'
}
/**
* Returns whether the payload is a Promise
*
* @param {*} payload
* @returns {payload is Promise<any>}
*/
export function isPromise(payload: any): payload is Promise<any> {
return getType(payload) === 'Promise'
}
/**
* Returns whether the payload is an Error
*
* @param {*} payload
* @returns {payload is Error}
*/
export function isError(payload: any): payload is Error {
return getType(payload) === 'Error'
}
/**
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
*
* @param {*} payload
* @returns {payload is typeof NaN}
*/
export function isNaNValue(payload: any): payload is typeof NaN {
return getType(payload) === 'Number' && isNaN(payload)
}
/**
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
*
* @param {*} payload
* @returns {(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) ||
isUndefined(payload) ||
isNumber(payload) ||
isString(payload) ||
isSymbol(payload)
)
}
/**
* Returns true whether the payload is null or undefined
*
* @param {*} payload
* @returns {(payload is null | undefined)}
*/
export const isNullOrUndefined = isOneOf(isNull, isUndefined)
export function isOneOf<A, B extends A, C extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>
): TypeGuard<A, B | C>
export function isOneOf<A, B extends A, C extends A, D extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>
): TypeGuard<A, B | C | D>
export function isOneOf<A, B extends A, C extends A, D extends A, E extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>,
d: TypeGuard<A, E>
): TypeGuard<A, B | C | D | E>
export function isOneOf<A, B extends A, C extends A, D extends A, E extends A, F extends A>(
a: TypeGuard<A, B>,
b: TypeGuard<A, C>,
c: TypeGuard<A, D>,
d: TypeGuard<A, E>,
e: TypeGuard<A, F>
): TypeGuard<A, B | C | D | E | F>
export function isOneOf(
a: AnyFunction,
b: AnyFunction,
c?: AnyFunction,
d?: AnyFunction,
e?: AnyFunction
): (value: unknown) => boolean {
return (value) =>
a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value))
}
/**
* 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!);
* It will, however, differentiate between object and null
*
* @template T
* @param {*} payload
* @param {T} type
* @throws {TypeError} Will throw type error if type is an invalid type
* @returns {payload is T}
*/
export function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T {
if (!(type instanceof Function)) {
throw new TypeError('Type must be a function')
}
if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
throw new TypeError('Type is not a class')
}
// Classes usually have names (as functions usually have names)
const name: string | undefined | null = (type as any).name
return getType(payload) === name || Boolean(payload && payload.constructor === type)
}
-376
View File
@@ -1,376 +0,0 @@
import test from 'ava'
import {
isError,
isEmptyArray,
isObject,
isPlainObject,
isAnyObject,
isUndefined,
isNull,
isNullOrUndefined,
isFunction,
isArray,
isString,
isEmptyString,
isFullString,
isBoolean,
isRegExp,
isNumber,
isDate,
isSymbol,
isPrimitive,
isType,
isMap,
isWeakMap,
isSet,
isWeakSet,
isFullArray,
// isBlob,
// isFile,
isPromise,
isNaNValue,
isEmptyObject,
isOneOf,
isFullObject,
} from '../src/index'
// const blob = Buffer.from([])
test('Basic true tests', t => {
t.is(isError(new Error('')), true)
t.is(isUndefined(undefined), true)
t.is(isNull(null), true)
t.is(isNullOrUndefined(null), true)
t.is(isNullOrUndefined(undefined), true)
t.is(isObject({}), true)
t.is(isEmptyObject({}), true)
t.is(isFullObject({0: ''}), true)
t.is(isFullObject({'': ''}), true)
t.is(isObject(new Object()), true)
t.is(isArray([]), true)
t.is(isEmptyArray([]), true)
t.is(isFullArray(['']), true)
t.is(isArray(new Array()), true)
t.is(isString(''), true)
t.is(isString('_'), true)
t.is(isEmptyString(''), true)
t.is(isFullString(' '), true)
t.is(isBoolean(true), true)
t.is(isBoolean(false), true)
t.is(isRegExp(/./), true)
t.is(isRegExp(/./gi), true)
t.is(isNumber(0), true)
t.is(isNumber(1), true)
t.is(isDate(new Date()), true)
t.is(isSymbol(Symbol()), true)
t.is(isMap(new Map()), true)
t.is(isWeakMap(new WeakMap()), true)
t.is(isSet(new Set()), true)
t.is(isWeakSet(new WeakSet()), true)
// t.is(isBlob(blob), true)
// t.is(isFile(new File([''], '', { type: 'text/html' })), true)
t.is(isPromise(new Promise((resolve, reject) => {})), true)
})
test('Basic false tests', t => {
t.is(isError({}), false)
t.is(isNumber(NaN), false)
t.is(isDate(new Date('_')), false)
t.is(isDate(NaN), false)
t.is(isUndefined(NaN), false)
t.is(isNull(NaN), false)
t.is(isObject(NaN), false)
t.is(isArray(NaN), false)
t.is(isString(NaN), false)
t.is(isEmptyString(' '), false)
t.is(isFullString(''), false)
t.is(isBoolean(NaN), false)
t.is(isRegExp(NaN), false)
t.is(isSymbol(NaN), false)
t.is(isMap(new WeakMap()), false)
t.is(isWeakMap(new Map()), false)
t.is(isSet(new WeakSet()), false)
t.is(isWeakSet(new Set()), false)
t.is(isNullOrUndefined(NaN), false)
})
test('isFunction', t => {
t.is(isFunction(NaN), false)
t.is(isFunction(() => {}), true)
t.is(isFunction(function () {}), true)
t.is(isFunction(async () => {}), true)
t.is(isFunction(async function () {}), true)
t.is(isFunction(function * () {}), true)
t.is(isFunction(async function * () {}), true)
const _ = { fn: () => {}, method () {} }
t.is(isFunction(_.fn), true)
t.is(isFunction(_.method), true)
})
test('isEmptyObject', t => {
t.is(isEmptyObject({}), true)
t.is(isEmptyObject(new Object()), true)
t.is(isEmptyObject('{}'), false)
t.is(isEmptyObject('{}'), false)
t.is(isEmptyObject(null), false)
t.is(isEmptyObject(new Date()), false)
t.is(isEmptyObject(new Error('')), false)
t.is(isEmptyObject(new Date()), false)
t.is(isEmptyObject(Symbol()), false)
t.is(isEmptyObject(new Map()), false)
t.is(isEmptyObject(new WeakMap()), false)
t.is(isEmptyObject(new Set()), false)
t.is(isEmptyObject(new WeakSet()), false)
})
test('isEmptyArray', t => {
t.is(isEmptyArray([]), true)
t.is(isEmptyArray(new Array()), true)
t.is(isEmptyArray(new Array(0)), true)
t.is(isEmptyArray(new Array(1)), false)
t.is(isEmptyArray([undefined]), false)
t.is(isEmptyArray(null), false)
t.is(isEmptyArray(new Date()), false)
t.is(isEmptyArray(new Error('')), false)
t.is(isEmptyArray(new Date()), false)
t.is(isEmptyArray(Symbol()), false)
t.is(isEmptyArray(new Map()), false)
t.is(isEmptyArray(new WeakMap()), false)
t.is(isEmptyArray(new Set()), false)
t.is(isEmptyArray(new WeakSet()), false)
})
test('isFullArray', t => {
t.is(isFullArray(new Array(1)), true)
t.is(isFullArray([undefined]), true)
t.is(isFullArray([null]), true)
t.is(isFullArray(['']), true)
t.is(isFullArray([]), false)
t.is(isFullArray(new Array()), false)
t.is(isFullArray(new Array(0)), false)
t.is(isFullArray(null), false)
t.is(isFullArray(new Date()), false)
t.is(isFullArray(new Error('')), false)
t.is(isFullArray(new Date()), false)
t.is(isFullArray(Symbol()), false)
t.is(isFullArray(new Map()), false)
t.is(isFullArray(new WeakMap()), false)
t.is(isFullArray(new Set()), false)
t.is(isFullArray(new WeakSet()), false)
})
test('NaN tests', t => {
t.is(isNaNValue(NaN), true)
t.is(isNaNValue(new Error('')), false)
t.is(isNaNValue(undefined), false)
t.is(isNaNValue(null), false)
t.is(isNaNValue(undefined), false)
t.is(isNaNValue({}), false)
t.is(isNaNValue(new Object()), false)
t.is(
isNaNValue(() => {}),
false
)
t.is(isNaNValue([]), false)
t.is(isNaNValue(new Array()), false)
t.is(isNaNValue(''), false)
t.is(isNaNValue('_'), false)
t.is(isNaNValue(''), false)
t.is(isNaNValue(' '), false)
t.is(isNaNValue(true), false)
t.is(isNaNValue(false), false)
t.is(isNaNValue(/./), false)
t.is(isNaNValue(/./gi), false)
t.is(isNaNValue(0), false)
t.is(isNaNValue(1), false)
t.is(isNaNValue(new Date()), false)
t.is(isNaNValue(Symbol()), false)
t.is(isNaNValue(new Map()), false)
t.is(isNaNValue(new WeakMap()), false)
t.is(isNaNValue(new Set()), false)
t.is(isNaNValue(new WeakSet()), false)
t.is(isNaNValue(new Promise((resolve, reject) => {})), false)
})
test('Primitive tests', t => {
// true
t.is(isPrimitive(0), true)
t.is(isPrimitive(''), true)
t.is(isPrimitive('str'), true)
t.is(isPrimitive(Symbol()), true)
t.is(isPrimitive(true), true)
t.is(isPrimitive(false), true)
t.is(isPrimitive(null), true)
t.is(isPrimitive(undefined), true)
// false
t.is(isPrimitive(NaN), false)
t.is(isPrimitive([]), false)
t.is(isPrimitive(new Array()), false)
t.is(isPrimitive({}), false)
t.is(isPrimitive(new Object()), false)
t.is(isPrimitive(new Date()), false)
t.is(
isPrimitive(() => {}),
false
)
})
test('Date exception', t => {
t.is(isDate(new Date('_')), false)
})
test('Generic isType', t => {
// -----------------------------
// This is correct old fashion syntax for classes, if this is missing
function MyClass () {}
MyClass.prototype.constructor = MyClass
// @ts-ignore
const myClass = new MyClass()
// -----------------------------
class MyOtherClass { constructor() {} }
// this is expected behaviour
t.is(isType('', String), true)
t.is(isType('_', String), true)
t.is(isType('Hello World', String), true)
t.is(isType(NaN, Number), true)
t.is(isType(0, Number), true)
t.is(isType(1, Number), true)
t.is(isType({}, Object), true)
t.is(isType(new Object(), Object), true)
t.is(isType([], Array), true)
t.is(isType(new Array(), Array), true)
t.is(
isType(() => {}, Function),
true
)
t.is(isType(true, Boolean), true)
t.is(isType(false, Boolean), true)
t.is(isType(new Date('_'), Date), true)
t.is(isType(new Date(), Date), true)
t.is(isType(/./, RegExp), true)
t.is(isType(/./gi, RegExp), true)
t.is(isType(myClass, MyClass), true)
t.is(isType(new MyOtherClass(), MyOtherClass), true)
t.is(isType(myClass, MyOtherClass), false)
t.is(isType(Symbol(), Symbol), true)
// t.is(isType(null, Null), true)
// t.is(isType(undefined, Undefined), true)
// It SHOULD fail
t.is(isType(5, String), false)
t.is(isType(null, Object), false)
// Not sure if this would be the expected behaviour but everything is an object
// so I would say so
t.is(isType(myClass, Object), true)
})
test('isObject vs isAnyObject', t => {
// -----------------------------
// This is correct old fashion syntax for classes, if this is missing
function MyClass () {}
MyClass.prototype.constructor = MyClass
// @ts-ignore
const myClass = new MyClass()
// -----------------------------
class MyClass2 { constructor() {} }
const myClass2 = new MyClass2()
const mySpecialObject = {}
Object.setPrototypeOf(mySpecialObject, {
toDate: function () {
return new Date()
},
})
// IS OBJECT
// plain object
t.is(isObject({}), true)
t.is(isObject(new Object()), true)
t.is(isPlainObject({}), true)
t.is(isPlainObject(new Object()), true)
// classes & prototypes
t.is(isObject(myClass), false)
t.is(isObject(myClass2), false)
t.is(isObject(mySpecialObject), false)
t.is(isPlainObject(myClass), false)
t.is(isPlainObject(myClass2), false)
t.is(isPlainObject(mySpecialObject), false)
// arrays and dates
t.is(isObject([]), false)
t.is(isObject(new Array()), false)
t.is(isObject(new Date('_')), false)
t.is(isObject(new Date()), false)
t.is(isPlainObject([]), false)
t.is(isPlainObject(new Array()), false)
t.is(isPlainObject(new Date('_')), false)
t.is(isPlainObject(new Date()), false)
// IS ANY OBJECT
// plain object
t.is(isAnyObject({}), true)
t.is(isAnyObject(new Object()), true)
// classes & prototypes
t.is(isAnyObject(myClass), true)
t.is(isAnyObject(myClass2), true)
t.is(isAnyObject(mySpecialObject), true)
// arrays and dates
t.is(isAnyObject([]), false)
t.is(isAnyObject(new Array()), false)
t.is(isAnyObject(new Date('_')), false)
t.is(isAnyObject(new Date()), false)
})
test('isOneOf', t => {
t.is(isOneOf(isString, isNumber)('_'), true)
t.is(isOneOf(isString, isNumber)(1), true)
t.is(isOneOf(isString, isNumber)(undefined), false)
t.is(isOneOf(isString, isNumber, isBoolean)('_'), true)
t.is(isOneOf(isString, isNumber, isBoolean)(1), true)
t.is(isOneOf(isString, isNumber, isBoolean)(true), true)
t.is(isOneOf(isString, isNumber, isBoolean)(undefined), false)
t.is(isOneOf(isString, isNumber, isBoolean, isArray)('_'), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray)(1), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray)(true), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray)([]), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray)(undefined), false)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)('_'), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(1), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(true), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)([]), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)({}), true)
t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(undefined), false)
})
test('type related tests', t => {
t.pass()
// const fn: string | ((k: number) => string) = (p) => 'a'
// if (!isFunction(fn)) {
// fn
// }
// const a: Record<string, number> = {}
// a[fn(1)] = fn(2)
// const myArray: string | string[] = ['a', 'b']
// if (!isArray(myArray)) {
// myArray
// }
// const a: Record<string, number> = {}
// a[myArray[1]] = myArray[0]
// const myArray: string | any[] = [1, 2, 'a', 'b']
// if (!isArray(myArray)) {
// myArray
// }
// const a: Record<string, number> = {}
// a[myArray[1]] = myArray[0]
})
-15
View File
@@ -1,15 +0,0 @@
// @ts-check
import {
isBlob,
isFile,
} from '../dist/index.cjs'
test('isBlob', () => {
expect(isBlob(Blob)).toBe(false)
expect(isBlob(new Blob())).toBe(true)
})
test('isFile', () => {
expect(isFile(File)).toBe(false)
expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true)
})
-11
View File
@@ -1,11 +0,0 @@
{
"compilerOptions": {
"baseUrl": ".",
"strict": true,
"isolatedModules": true,
"esModuleInterop": true,
"declaration": true,
"declarationDir": "./types/"
},
"include": ["src/**/*", "test/**/*"]
}
-253
View File
@@ -1,253 +0,0 @@
export declare type AnyFunction = (...args: any[]) => any;
export declare type AnyAsyncFunction = (...args: any[]) => Promise<any>;
export declare type AnyClass = new (...args: any[]) => any;
export declare type PlainObject = Record<string | number | symbol, any>;
declare type TypeGuard<A, B extends A> = (payload: A) => payload is B;
/**
* Returns the object type of the given payload
*
* @param {*} payload
* @returns {string}
*/
export declare function getType(payload: any): string;
/**
* Returns whether the payload is undefined
*
* @param {*} payload
* @returns {payload is undefined}
*/
export declare function isUndefined(payload: any): payload is undefined;
/**
* Returns whether the payload is null
*
* @param {*} payload
* @returns {payload is null}
*/
export declare function isNull(payload: any): payload is null;
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export declare function isPlainObject(payload: any): payload is PlainObject;
/**
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export declare function isObject(payload: any): payload is PlainObject;
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is { [K in any]: never }}
*/
export declare function isEmptyObject(payload: any): payload is {
[K in any]: never;
};
/**
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export declare function isFullObject(payload: any): payload is PlainObject;
/**
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
*
* @param {*} payload
* @returns {payload is PlainObject}
*/
export declare function isAnyObject(payload: any): payload is PlainObject;
/**
* Returns whether the payload is an object like a type passed in < >
*
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
*
* @template T this must be passed in < >
* @param {*} payload
* @returns {payload is T}
*/
export declare function isObjectLike<T extends PlainObject>(payload: any): payload is T;
/**
* Returns whether the payload is a function (regular or async)
*
* @param {*} payload
* @returns {payload is AnyFunction}
*/
export declare function isFunction(payload: any): payload is AnyFunction;
/**
* Returns whether the payload is an array
*
* @param {any} payload
* @returns {payload is any[]}
*/
export declare function isArray(payload: any): payload is any[];
/**
* Returns whether the payload is a an array with at least 1 item
*
* @param {*} payload
* @returns {payload is any[]}
*/
export declare function isFullArray(payload: any): payload is any[];
/**
* Returns whether the payload is a an empty array
*
* @param {*} payload
* @returns {payload is []}
*/
export declare function isEmptyArray(payload: any): payload is [];
/**
* Returns whether the payload is a string
*
* @param {*} payload
* @returns {payload is string}
*/
export declare function isString(payload: any): payload is string;
/**
* Returns whether the payload is a string, BUT returns false for ''
*
* @param {*} payload
* @returns {payload is string}
*/
export declare function isFullString(payload: any): payload is string;
/**
* Returns whether the payload is ''
*
* @param {*} payload
* @returns {payload is string}
*/
export declare function isEmptyString(payload: any): payload is string;
/**
* Returns whether the payload is a number (but not NaN)
*
* This will return `false` for `NaN`!!
*
* @param {*} payload
* @returns {payload is number}
*/
export declare function isNumber(payload: any): payload is number;
/**
* Returns whether the payload is a boolean
*
* @param {*} payload
* @returns {payload is boolean}
*/
export declare function isBoolean(payload: any): payload is boolean;
/**
* Returns whether the payload is a regular expression (RegExp)
*
* @param {*} payload
* @returns {payload is RegExp}
*/
export declare function isRegExp(payload: any): payload is RegExp;
/**
* Returns whether the payload is a Map
*
* @param {*} payload
* @returns {payload is Map<any, any>}
*/
export declare function isMap(payload: any): payload is Map<any, any>;
/**
* Returns whether the payload is a WeakMap
*
* @param {*} payload
* @returns {payload is WeakMap<any, any>}
*/
export declare function isWeakMap(payload: any): payload is WeakMap<any, any>;
/**
* Returns whether the payload is a Set
*
* @param {*} payload
* @returns {payload is Set<any>}
*/
export declare function isSet(payload: any): payload is Set<any>;
/**
* Returns whether the payload is a WeakSet
*
* @param {*} payload
* @returns {payload is WeakSet<any>}
*/
export declare function isWeakSet(payload: any): payload is WeakSet<any>;
/**
* Returns whether the payload is a Symbol
*
* @param {*} payload
* @returns {payload is symbol}
*/
export declare function isSymbol(payload: any): payload is symbol;
/**
* Returns whether the payload is a Date, and that the date is valid
*
* @param {*} payload
* @returns {payload is Date}
*/
export declare function isDate(payload: any): payload is Date;
/**
* Returns whether the payload is a Blob
*
* @param {*} payload
* @returns {payload is Blob}
*/
export declare function isBlob(payload: any): payload is Blob;
/**
* Returns whether the payload is a File
*
* @param {*} payload
* @returns {payload is File}
*/
export declare function isFile(payload: any): payload is File;
/**
* Returns whether the payload is a Promise
*
* @param {*} payload
* @returns {payload is Promise<any>}
*/
export declare function isPromise(payload: any): payload is Promise<any>;
/**
* Returns whether the payload is an Error
*
* @param {*} payload
* @returns {payload is Error}
*/
export declare function isError(payload: any): payload is Error;
/**
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
*
* @param {*} payload
* @returns {payload is typeof NaN}
*/
export declare function isNaNValue(payload: any): payload is typeof NaN;
/**
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
*
* @param {*} payload
* @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 const isNullOrUndefined: TypeGuard<any, null | undefined>;
export declare function isOneOf<A, B extends A, C extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>): TypeGuard<A, B | C>;
export declare function isOneOf<A, B extends A, C extends A, D extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>): TypeGuard<A, B | C | D>;
export declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>): TypeGuard<A, B | C | D | E>;
export declare function isOneOf<A, B extends A, C extends A, D extends A, E extends A, F extends A>(a: TypeGuard<A, B>, b: TypeGuard<A, C>, c: TypeGuard<A, D>, d: TypeGuard<A, E>, e: TypeGuard<A, F>): TypeGuard<A, B | C | D | E | F>;
/**
* 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!);
* It will, however, differentiate between object and null
*
* @template T
* @param {*} payload
* @param {T} type
* @throws {TypeError} Will throw type error if type is an invalid type
* @returns {payload is T}
*/
export declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
export {};