@sindresorhus/is
Advanced tools
Comparing version 1.2.0 to 2.0.0
/// <reference types="node" /> | ||
/// <reference lib="esnext" /> | ||
/// <reference lib="es2018" /> | ||
/// <reference lib="dom" /> | ||
@@ -15,3 +15,5 @@ export declare type Class<T = unknown> = new (...args: any[]) => T; | ||
Generator = "Generator", | ||
AsyncGenerator = "AsyncGenerator", | ||
GeneratorFunction = "GeneratorFunction", | ||
AsyncGeneratorFunction = "AsyncGeneratorFunction", | ||
AsyncFunction = "AsyncFunction", | ||
@@ -62,9 +64,11 @@ Observable = "Observable", | ||
var object: (value: unknown) => value is object; | ||
var iterable: (value: unknown) => value is IterableIterator<unknown>; | ||
var asyncIterable: (value: unknown) => value is AsyncIterableIterator<unknown>; | ||
var iterable: <T = unknown>(value: unknown) => value is IterableIterator<T>; | ||
var asyncIterable: <T = unknown>(value: unknown) => value is AsyncIterableIterator<T>; | ||
var generator: (value: unknown) => value is Generator<unknown, any, unknown>; | ||
var nativePromise: (value: unknown) => value is Promise<unknown>; | ||
var promise: (value: unknown) => value is Promise<unknown>; | ||
var asyncGenerator: (value: unknown) => value is AsyncGenerator<unknown, any, unknown>; | ||
var nativePromise: <T = unknown>(value: unknown) => value is Promise<T>; | ||
var promise: <T = unknown>(value: unknown) => value is Promise<T>; | ||
var generatorFunction: (value: unknown) => value is GeneratorFunction; | ||
var asyncFunction: (value: unknown) => value is Function; | ||
var asyncGeneratorFunction: (value: unknown) => value is (...args: any[]) => Promise<unknown>; | ||
var asyncFunction: <T = unknown>(value: unknown) => value is (...args: any[]) => Promise<T>; | ||
var boundFunction: (value: unknown) => value is Function; | ||
@@ -74,5 +78,5 @@ var regExp: (value: unknown) => value is RegExp; | ||
var error: (value: unknown) => value is Error; | ||
var map: (value: unknown) => value is Map<unknown, unknown>; | ||
var set: (value: unknown) => value is Set<unknown>; | ||
var weakMap: (value: unknown) => value is WeakMap<object, unknown>; | ||
var map: <Key = unknown, Value = unknown>(value: unknown) => value is Map<Key, Value>; | ||
var set: <T = unknown>(value: unknown) => value is Set<T>; | ||
var weakMap: <Key extends object = object, Value = unknown>(value: unknown) => value is WeakMap<Key, Value>; | ||
var weakSet: (value: unknown) => value is WeakSet<object>; | ||
@@ -102,7 +106,5 @@ var int8Array: (value: unknown) => value is Int8Array; | ||
var safeInteger: (value: unknown) => value is number; | ||
var plainObject: (value: unknown) => value is { | ||
[key: string]: unknown; | ||
}; | ||
var plainObject: <Value = unknown>(value: unknown) => value is Record<string, Value>; | ||
var typedArray: (value: unknown) => value is TypedArray; | ||
var arrayLike: (value: unknown) => value is ArrayLike<unknown>; | ||
var arrayLike: <T = unknown>(value: unknown) => value is ArrayLike<T>; | ||
var inRange: (value: number, range: number | number[]) => value is number; | ||
@@ -120,12 +122,8 @@ var domElement: (value: unknown) => value is Element; | ||
var emptyStringOrWhitespace: (value: unknown) => value is string; | ||
var emptyObject: (value: unknown) => value is { | ||
[key: string]: never; | ||
}; | ||
var nonEmptyObject: (value: unknown) => value is { | ||
[key: string]: unknown; | ||
}; | ||
var emptyObject: <Key extends string | number | symbol = string>(value: unknown) => value is Record<Key, never>; | ||
var nonEmptyObject: <Key extends string | number | symbol = string, Value = unknown>(value: unknown) => value is Record<Key, Value>; | ||
var emptySet: (value: unknown) => value is Set<never>; | ||
var nonEmptySet: (value: unknown) => value is Set<unknown>; | ||
var nonEmptySet: <T = unknown>(value: unknown) => value is Set<T>; | ||
var emptyMap: (value: unknown) => value is Map<never, never>; | ||
var nonEmptyMap: (value: unknown) => value is Map<unknown, unknown>; | ||
var nonEmptyMap: <Key = unknown, Value = unknown>(value: unknown) => value is Map<Key, Value>; | ||
var any: (predicate: Predicate, ...values: unknown[]) => boolean; | ||
@@ -137,4 +135,4 @@ var all: (predicate: Predicate, ...values: unknown[]) => boolean; | ||
export interface ArrayLike<T> { | ||
readonly [index: number]: T; | ||
readonly length: number; | ||
readonly [index: number]: T; | ||
} | ||
@@ -145,6 +143,126 @@ export interface ObservableLike { | ||
} | ||
export declare type NodeStream = object & { | ||
readonly pipe: Function; | ||
}; | ||
export interface NodeStream extends NodeJS.EventEmitter { | ||
pipe<T extends NodeJS.WritableStream>(destination: T, options?: { | ||
end?: boolean; | ||
}): T; | ||
} | ||
export declare type Predicate = (value: unknown) => boolean; | ||
export declare const enum AssertionTypeDescription { | ||
class_ = "Class", | ||
numericString = "string with a number", | ||
nullOrUndefined = "null or undefined", | ||
iterable = "Iterable", | ||
asyncIterable = "AsyncIterable", | ||
nativePromise = "native Promise", | ||
urlString = "string with a URL", | ||
truthy = "truthy", | ||
falsy = "falsy", | ||
nan = "NaN", | ||
primitive = "primitive", | ||
integer = "integer", | ||
safeInteger = "integer", | ||
plainObject = "plain object", | ||
arrayLike = "array-like", | ||
typedArray = "TypedArray", | ||
domElement = "Element", | ||
nodeStream = "Node.js Stream", | ||
infinite = "infinite number", | ||
emptyArray = "empty array", | ||
nonEmptyArray = "non-empty array", | ||
emptyString = "empty string", | ||
nonEmptyString = "non-empty string", | ||
emptyStringOrWhitespace = "empty string or whitespace", | ||
emptyObject = "empty object", | ||
nonEmptyObject = "non-empty object", | ||
emptySet = "empty set", | ||
nonEmptySet = "non-empty set", | ||
emptyMap = "empty map", | ||
nonEmptyMap = "non-empty map", | ||
evenInteger = "even integer", | ||
oddInteger = "odd integer", | ||
directInstanceOf = "T", | ||
inRange = "in range", | ||
any = "predicate returns truthy for any value", | ||
all = "predicate returns truthy for all values" | ||
} | ||
interface Assert { | ||
undefined: (value: unknown) => asserts value is undefined; | ||
string: (value: unknown) => asserts value is string; | ||
number: (value: unknown) => asserts value is number; | ||
bigint: (value: unknown) => asserts value is bigint; | ||
function_: (value: unknown) => asserts value is Function; | ||
null_: (value: unknown) => asserts value is null; | ||
class_: (value: unknown) => asserts value is Class; | ||
boolean: (value: unknown) => asserts value is boolean; | ||
symbol: (value: unknown) => asserts value is symbol; | ||
numericString: (value: unknown) => asserts value is string; | ||
array: <T = unknown>(value: unknown) => asserts value is T[]; | ||
buffer: (value: unknown) => asserts value is Buffer; | ||
nullOrUndefined: (value: unknown) => asserts value is null | undefined; | ||
object: <Key extends keyof any = string, Value = unknown>(value: unknown) => asserts value is Record<Key, Value>; | ||
iterable: <T = unknown>(value: unknown) => asserts value is Iterable<T>; | ||
asyncIterable: <T = unknown>(value: unknown) => asserts value is AsyncIterable<T>; | ||
generator: (value: unknown) => asserts value is Generator; | ||
asyncGenerator: (value: unknown) => asserts value is AsyncGenerator; | ||
nativePromise: <T = unknown>(value: unknown) => asserts value is Promise<T>; | ||
promise: <T = unknown>(value: unknown) => asserts value is Promise<T>; | ||
generatorFunction: (value: unknown) => asserts value is GeneratorFunction; | ||
asyncGeneratorFunction: (value: unknown) => asserts value is AsyncGeneratorFunction; | ||
asyncFunction: (value: unknown) => asserts value is Function; | ||
boundFunction: (value: unknown) => asserts value is Function; | ||
regExp: (value: unknown) => asserts value is RegExp; | ||
date: (value: unknown) => asserts value is Date; | ||
error: (value: unknown) => asserts value is Error; | ||
map: <Key = unknown, Value = unknown>(value: unknown) => asserts value is Map<Key, Value>; | ||
set: <T = unknown>(value: unknown) => asserts value is Set<T>; | ||
weakMap: <Key extends object = object, Value = unknown>(value: unknown) => asserts value is WeakMap<Key, Value>; | ||
weakSet: <T extends object = object>(value: unknown) => asserts value is WeakSet<T>; | ||
int8Array: (value: unknown) => asserts value is Int8Array; | ||
uint8Array: (value: unknown) => asserts value is Uint8Array; | ||
uint8ClampedArray: (value: unknown) => asserts value is Uint8ClampedArray; | ||
int16Array: (value: unknown) => asserts value is Int16Array; | ||
uint16Array: (value: unknown) => asserts value is Uint16Array; | ||
int32Array: (value: unknown) => asserts value is Int32Array; | ||
uint32Array: (value: unknown) => asserts value is Uint32Array; | ||
float32Array: (value: unknown) => asserts value is Float32Array; | ||
float64Array: (value: unknown) => asserts value is Float64Array; | ||
bigInt64Array: (value: unknown) => asserts value is BigInt64Array; | ||
bigUint64Array: (value: unknown) => asserts value is BigUint64Array; | ||
arrayBuffer: (value: unknown) => asserts value is ArrayBuffer; | ||
sharedArrayBuffer: (value: unknown) => asserts value is SharedArrayBuffer; | ||
dataView: (value: unknown) => asserts value is DataView; | ||
urlInstance: (value: unknown) => asserts value is URL; | ||
urlString: (value: unknown) => asserts value is string; | ||
truthy: (value: unknown) => asserts value is unknown; | ||
falsy: (value: unknown) => asserts value is unknown; | ||
nan: (value: unknown) => asserts value is unknown; | ||
primitive: (value: unknown) => asserts value is Primitive; | ||
integer: (value: unknown) => asserts value is number; | ||
safeInteger: (value: unknown) => asserts value is number; | ||
plainObject: <Value = unknown>(value: unknown) => asserts value is Record<string, Value>; | ||
typedArray: (value: unknown) => asserts value is TypedArray; | ||
arrayLike: <T = unknown>(value: unknown) => asserts value is ArrayLike<T>; | ||
domElement: (value: unknown) => asserts value is Element; | ||
observable: (value: unknown) => asserts value is ObservableLike; | ||
nodeStream: (value: unknown) => asserts value is NodeStream; | ||
infinite: (value: unknown) => asserts value is number; | ||
emptyArray: (value: unknown) => asserts value is never[]; | ||
nonEmptyArray: (value: unknown) => asserts value is unknown[]; | ||
emptyString: (value: unknown) => asserts value is ''; | ||
nonEmptyString: (value: unknown) => asserts value is string; | ||
emptyStringOrWhitespace: (value: unknown) => asserts value is string; | ||
emptyObject: <Key extends keyof any = string>(value: unknown) => asserts value is Record<Key, never>; | ||
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(value: unknown) => asserts value is Record<Key, Value>; | ||
emptySet: (value: unknown) => asserts value is Set<never>; | ||
nonEmptySet: <T = unknown>(value: unknown) => asserts value is Set<T>; | ||
emptyMap: (value: unknown) => asserts value is Map<never, never>; | ||
nonEmptyMap: <Key = unknown, Value = unknown>(value: unknown) => asserts value is Map<Key, Value>; | ||
evenInteger: (value: number) => asserts value is number; | ||
oddInteger: (value: number) => asserts value is number; | ||
directInstanceOf: <T>(instance: unknown, class_: Class<T>) => asserts instance is T; | ||
inRange: (value: number, range: number | number[]) => asserts value is number; | ||
any: (predicate: Predicate, ...values: unknown[]) => void | never; | ||
all: (predicate: Predicate, ...values: unknown[]) => void | never; | ||
} | ||
export declare const assert: Assert; | ||
export default is; |
"use strict"; | ||
/// <reference lib="esnext"/> | ||
/// <reference lib="es2018"/> | ||
/// <reference lib="dom"/> | ||
/// <reference types="node"/> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// TODO: Use the `URL` global when targeting Node.js 10 | ||
const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; | ||
const { toString } = Object.prototype; | ||
@@ -79,2 +78,3 @@ const isOfType = (type) => (value) => typeof value === type; | ||
is.generator = (value) => is.iterable(value) && is.function_(value.next) && is.function_(value.throw); | ||
is.asyncGenerator = (value) => is.asyncIterable(value) && is.function_(value.next) && is.function_(value.throw); | ||
is.nativePromise = (value) => isObjectOfType("Promise" /* Promise */)(value); | ||
@@ -86,4 +86,4 @@ const hasPromiseAPI = (value) => is.object(value) && | ||
is.generatorFunction = isObjectOfType("GeneratorFunction" /* GeneratorFunction */); | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
is.asyncFunction = isObjectOfType("AsyncFunction" /* AsyncFunction */); | ||
is.asyncGeneratorFunction = (value) => getObjectType(value) === "AsyncGeneratorFunction" /* AsyncGeneratorFunction */; | ||
is.asyncFunction = (value) => getObjectType(value) === "AsyncFunction" /* AsyncFunction */; | ||
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types | ||
@@ -119,3 +119,3 @@ is.boundFunction = (value) => is.function_(value) && !value.hasOwnProperty('prototype'); | ||
try { | ||
new URLGlobal(value); // eslint-disable-line no-new | ||
new URL(value); // eslint-disable-line no-new | ||
return true; | ||
@@ -237,2 +237,93 @@ } | ||
is.all = (predicate, ...values) => predicateOnArray(Array.prototype.every, predicate, values); | ||
const assertType = (condition, description, value) => { | ||
if (!condition) { | ||
throw new TypeError(`Expected value which is \`${description}\`, received value of type \`${is(value)}\`.`); | ||
} | ||
}; | ||
exports.assert = { | ||
// Unknowns. | ||
undefined: (value) => assertType(is.undefined(value), "undefined" /* undefined */, value), | ||
string: (value) => assertType(is.string(value), "string" /* string */, value), | ||
number: (value) => assertType(is.number(value), "number" /* number */, value), | ||
bigint: (value) => assertType(is.bigint(value), "bigint" /* bigint */, value), | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
function_: (value) => assertType(is.function_(value), "Function" /* Function */, value), | ||
null_: (value) => assertType(is.null_(value), "null" /* null */, value), | ||
class_: (value) => assertType(is.class_(value), "Class" /* class_ */, value), | ||
boolean: (value) => assertType(is.boolean(value), "boolean" /* boolean */, value), | ||
symbol: (value) => assertType(is.symbol(value), "symbol" /* symbol */, value), | ||
numericString: (value) => assertType(is.numericString(value), "string with a number" /* numericString */, value), | ||
array: (value) => assertType(is.array(value), "Array" /* Array */, value), | ||
buffer: (value) => assertType(is.buffer(value), "Buffer" /* Buffer */, value), | ||
nullOrUndefined: (value) => assertType(is.nullOrUndefined(value), "null or undefined" /* nullOrUndefined */, value), | ||
object: (value) => assertType(is.object(value), "Object" /* Object */, value), | ||
iterable: (value) => assertType(is.iterable(value), "Iterable" /* iterable */, value), | ||
asyncIterable: (value) => assertType(is.asyncIterable(value), "AsyncIterable" /* asyncIterable */, value), | ||
generator: (value) => assertType(is.generator(value), "Generator" /* Generator */, value), | ||
asyncGenerator: (value) => assertType(is.asyncGenerator(value), "AsyncGenerator" /* AsyncGenerator */, value), | ||
nativePromise: (value) => assertType(is.nativePromise(value), "native Promise" /* nativePromise */, value), | ||
promise: (value) => assertType(is.promise(value), "Promise" /* Promise */, value), | ||
generatorFunction: (value) => assertType(is.generatorFunction(value), "GeneratorFunction" /* GeneratorFunction */, value), | ||
asyncGeneratorFunction: (value) => assertType(is.asyncGeneratorFunction(value), "AsyncGeneratorFunction" /* AsyncGeneratorFunction */, value), | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
asyncFunction: (value) => assertType(is.asyncFunction(value), "AsyncFunction" /* AsyncFunction */, value), | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
boundFunction: (value) => assertType(is.boundFunction(value), "Function" /* Function */, value), | ||
regExp: (value) => assertType(is.regExp(value), "RegExp" /* RegExp */, value), | ||
date: (value) => assertType(is.date(value), "Date" /* Date */, value), | ||
error: (value) => assertType(is.error(value), "Error" /* Error */, value), | ||
map: (value) => assertType(is.map(value), "Map" /* Map */, value), | ||
set: (value) => assertType(is.set(value), "Set" /* Set */, value), | ||
weakMap: (value) => assertType(is.weakMap(value), "WeakMap" /* WeakMap */, value), | ||
weakSet: (value) => assertType(is.weakSet(value), "WeakSet" /* WeakSet */, value), | ||
int8Array: (value) => assertType(is.int8Array(value), "Int8Array" /* Int8Array */, value), | ||
uint8Array: (value) => assertType(is.uint8Array(value), "Uint8Array" /* Uint8Array */, value), | ||
uint8ClampedArray: (value) => assertType(is.uint8ClampedArray(value), "Uint8ClampedArray" /* Uint8ClampedArray */, value), | ||
int16Array: (value) => assertType(is.int16Array(value), "Int16Array" /* Int16Array */, value), | ||
uint16Array: (value) => assertType(is.uint16Array(value), "Uint16Array" /* Uint16Array */, value), | ||
int32Array: (value) => assertType(is.int32Array(value), "Int32Array" /* Int32Array */, value), | ||
uint32Array: (value) => assertType(is.uint32Array(value), "Uint32Array" /* Uint32Array */, value), | ||
float32Array: (value) => assertType(is.float32Array(value), "Float32Array" /* Float32Array */, value), | ||
float64Array: (value) => assertType(is.float64Array(value), "Float64Array" /* Float64Array */, value), | ||
bigInt64Array: (value) => assertType(is.bigInt64Array(value), "BigInt64Array" /* BigInt64Array */, value), | ||
bigUint64Array: (value) => assertType(is.bigUint64Array(value), "BigUint64Array" /* BigUint64Array */, value), | ||
arrayBuffer: (value) => assertType(is.arrayBuffer(value), "ArrayBuffer" /* ArrayBuffer */, value), | ||
sharedArrayBuffer: (value) => assertType(is.sharedArrayBuffer(value), "SharedArrayBuffer" /* SharedArrayBuffer */, value), | ||
dataView: (value) => assertType(is.dataView(value), "DataView" /* DataView */, value), | ||
urlInstance: (value) => assertType(is.urlInstance(value), "URL" /* URL */, value), | ||
urlString: (value) => assertType(is.urlString(value), "string with a URL" /* urlString */, value), | ||
truthy: (value) => assertType(is.truthy(value), "truthy" /* truthy */, value), | ||
falsy: (value) => assertType(is.falsy(value), "falsy" /* falsy */, value), | ||
nan: (value) => assertType(is.nan(value), "NaN" /* nan */, value), | ||
primitive: (value) => assertType(is.primitive(value), "primitive" /* primitive */, value), | ||
integer: (value) => assertType(is.integer(value), "integer" /* integer */, value), | ||
safeInteger: (value) => assertType(is.safeInteger(value), "integer" /* safeInteger */, value), | ||
plainObject: (value) => assertType(is.plainObject(value), "plain object" /* plainObject */, value), | ||
typedArray: (value) => assertType(is.typedArray(value), "TypedArray" /* typedArray */, value), | ||
arrayLike: (value) => assertType(is.arrayLike(value), "array-like" /* arrayLike */, value), | ||
domElement: (value) => assertType(is.domElement(value), "Element" /* domElement */, value), | ||
observable: (value) => assertType(is.observable(value), "Observable" /* Observable */, value), | ||
nodeStream: (value) => assertType(is.nodeStream(value), "Node.js Stream" /* nodeStream */, value), | ||
infinite: (value) => assertType(is.infinite(value), "infinite number" /* infinite */, value), | ||
emptyArray: (value) => assertType(is.emptyArray(value), "empty array" /* emptyArray */, value), | ||
nonEmptyArray: (value) => assertType(is.nonEmptyArray(value), "non-empty array" /* nonEmptyArray */, value), | ||
emptyString: (value) => assertType(is.emptyString(value), "empty string" /* emptyString */, value), | ||
nonEmptyString: (value) => assertType(is.nonEmptyString(value), "non-empty string" /* nonEmptyString */, value), | ||
emptyStringOrWhitespace: (value) => assertType(is.emptyStringOrWhitespace(value), "empty string or whitespace" /* emptyStringOrWhitespace */, value), | ||
emptyObject: (value) => assertType(is.emptyObject(value), "empty object" /* emptyObject */, value), | ||
nonEmptyObject: (value) => assertType(is.nonEmptyObject(value), "non-empty object" /* nonEmptyObject */, value), | ||
emptySet: (value) => assertType(is.emptySet(value), "empty set" /* emptySet */, value), | ||
nonEmptySet: (value) => assertType(is.nonEmptySet(value), "non-empty set" /* nonEmptySet */, value), | ||
emptyMap: (value) => assertType(is.emptyMap(value), "empty map" /* emptyMap */, value), | ||
nonEmptyMap: (value) => assertType(is.nonEmptyMap(value), "non-empty map" /* nonEmptyMap */, value), | ||
// Numbers. | ||
evenInteger: (value) => assertType(is.evenInteger(value), "even integer" /* evenInteger */, value), | ||
oddInteger: (value) => assertType(is.oddInteger(value), "odd integer" /* oddInteger */, value), | ||
// Two arguments. | ||
directInstanceOf: (instance, class_) => assertType(is.directInstanceOf(instance, class_), "T" /* directInstanceOf */, instance), | ||
inRange: (value, range) => assertType(is.inRange(value, range), "in range" /* inRange */, value), | ||
// Variadic functions. | ||
any: (predicate, ...values) => assertType(is.any(predicate, ...values), "predicate returns truthy for any value" /* any */, values), | ||
all: (predicate, ...values) => assertType(is.all(predicate, ...values), "predicate returns truthy for all values" /* all */, values) | ||
}; | ||
// Some few keywords are reserved, but we'll populate them for Node.js users | ||
@@ -251,2 +342,13 @@ // See https://github.com/Microsoft/TypeScript/issues/2536 | ||
}); | ||
Object.defineProperties(exports.assert, { | ||
class: { | ||
value: exports.assert.class_ | ||
}, | ||
function: { | ||
value: exports.assert.function_ | ||
}, | ||
null: { | ||
value: exports.assert.null_ | ||
} | ||
}); | ||
exports.default = is; | ||
@@ -256,2 +358,2 @@ // For CommonJS default export support | ||
module.exports.default = is; | ||
//# sourceMappingURL=index.js.map | ||
module.exports.assert = exports.assert; |
{ | ||
"name": "@sindresorhus/is", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "Type check values", | ||
"license": "MIT", | ||
"repository": "sindresorhus/is", | ||
"funding": "https://github.com/sindresorhus/is?sponsor=1", | ||
"author": { | ||
@@ -14,3 +15,3 @@ "name": "Sindre Sorhus", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -50,17 +51,17 @@ "scripts": { | ||
"devDependencies": { | ||
"@sindresorhus/tsconfig": "^0.4.0", | ||
"@sindresorhus/tsconfig": "^0.7.0", | ||
"@types/jsdom": "^12.2.4", | ||
"@types/node": "^12.0.10", | ||
"@types/node": "^12.12.6", | ||
"@types/zen-observable": "^0.8.0", | ||
"@typescript-eslint/eslint-plugin": "^1.11.0", | ||
"@typescript-eslint/parser": "^1.11.0", | ||
"@typescript-eslint/eslint-plugin": "^2.18.0", | ||
"@typescript-eslint/parser": "^2.18.0", | ||
"ava": "^2.1.0", | ||
"del-cli": "^2.0.0", | ||
"eslint-config-xo-typescript": "^0.15.0", | ||
"jsdom": "^15.0.0", | ||
"eslint-config-xo-typescript": "^0.24.1", | ||
"jsdom": "^16.0.1", | ||
"rxjs": "^6.4.0", | ||
"tempy": "^0.3.0", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.4.1", | ||
"xo": "^0.24.0", | ||
"typescript": "^3.7.5", | ||
"xo": "^0.25.3", | ||
"zen-observable": "^0.8.8" | ||
@@ -85,2 +86,5 @@ }, | ||
], | ||
"parserOptions": { | ||
"project": "./tsconfig.xo.json" | ||
}, | ||
"globals": [ | ||
@@ -92,4 +96,2 @@ "BigInt", | ||
"rules": { | ||
"import/first": "off", | ||
"import/newline-after-import": "off", | ||
"@typescript-eslint/promise-function-async": "off", | ||
@@ -96,0 +98,0 @@ "@typescript-eslint/no-empty-function": "off", |
154
readme.md
@@ -9,3 +9,2 @@ # is [![Build Status](https://travis-ci.org/sindresorhus/is.svg?branch=master)](https://travis-ci.org/sindresorhus/is) | ||
## Highlights | ||
@@ -15,6 +14,7 @@ | ||
- [Extensive use of type guards](#type-guards) | ||
- [Supports type assertions](#type-assertions) | ||
- [Aware of generic type parameters](#generic-type-parameters) (use with caution) | ||
- Actively maintained | ||
- 2 million weekly downloads | ||
## Install | ||
@@ -26,3 +26,2 @@ | ||
## Usage | ||
@@ -43,3 +42,20 @@ | ||
[Assertions](#type-assertions) perform the same type checks, but throw an error if the type does not match. | ||
```js | ||
const {assert} = require('@sindresorhus/is'); | ||
assert.string(2); | ||
//=> Error: Expected value which is `string`, received value of type `number`. | ||
``` | ||
And with TypeScript: | ||
```ts | ||
import {assert} from '@sindresorhus/is'; | ||
assert.string(foo); | ||
// `foo` is now typed as a `string`. | ||
``` | ||
## API | ||
@@ -117,8 +133,40 @@ | ||
is.asyncFunction(async () => {}); | ||
// => true | ||
//=> true | ||
is.asyncFunction(() => {}); | ||
// => false | ||
//=> false | ||
``` | ||
##### .asyncGenerator(value) | ||
```js | ||
is.asyncGenerator( | ||
(async function * () { | ||
yield 4; | ||
})() | ||
); | ||
//=> true | ||
is.asyncGenerator( | ||
(function * () { | ||
yield 4; | ||
})() | ||
); | ||
//=> false | ||
``` | ||
##### .asyncGeneratorFunction(value) | ||
```js | ||
is.asyncGeneratorFunction(async function * () { | ||
yield 4; | ||
}); | ||
//=> true | ||
is.asyncGeneratorFunction(function * () { | ||
yield 4; | ||
}); | ||
//=> false | ||
``` | ||
##### .boundFunction(value) | ||
@@ -130,9 +178,9 @@ | ||
is.boundFunction(() => {}); | ||
// => true | ||
//=> true | ||
is.boundFunction(function () {}.bind(null)); | ||
// => true | ||
//=> true | ||
is.boundFunction(function () {}); | ||
// => false | ||
//=> false | ||
``` | ||
@@ -204,3 +252,3 @@ | ||
is.emptyObject(object1); | ||
// => true | ||
//=> true | ||
``` | ||
@@ -406,3 +454,2 @@ | ||
## Type guards | ||
@@ -436,3 +483,71 @@ | ||
## Type assertions | ||
The type guards are also available as [type assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions), which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern. | ||
```ts | ||
import {assert} from '@sindresorhus/is'; | ||
const handleMovieRatingApiResponse = (response: unknown) => { | ||
assert.plainObject(response); | ||
// `response` is now typed as a plain `object` with `unknown` properties. | ||
assert.number(response.rating); | ||
// `response.rating` is now typed as a `number`. | ||
assert.string(response.title); | ||
// `response.title` is now typed as a `string`. | ||
return `${response.title} (${response.rating * 10})`; | ||
}; | ||
handleMovieRatingApiResponse({rating: 0.87, title: 'The Matrix'}); | ||
//=> 'The Matrix (8.7)' | ||
// This throws an error. | ||
handleMovieRatingApiResponse({rating: '🦄'}); | ||
``` | ||
## Generic type parameters | ||
The type guards and type assertions are aware of [generic type parameters](https://www.typescriptlang.org/docs/handbook/generics.html), such as `Promise<T>` and `Map<Key, Value>`. The default is `unknown` for most cases, since `is` cannot check them at runtime. If the generic type is known at compile-time, either implicitly (inferred) or explicitly (provided), `is` propagates the type so it can be used later. | ||
Use generic type parameters with caution. They are only checked by the TypeScript compiler, and not checked by `is` at runtime. This can lead to unexpected behavior, where the generic type is _assumed_ at compile-time, but actually is something completely different at runtime. It is best to use `unknown` (default) and type-check the value of the generic type parameter at runtime with `is` or `assert`. | ||
```ts | ||
import {assert} from '@sindresorhus/is'; | ||
async function badNumberAssumption(input: unknown) { | ||
// Bad assumption about the generic type parameter fools the compile-time type system. | ||
assert.promise<number>(input); | ||
// `input` is a `Promise` but only assumed to be `Promise<number>`. | ||
const resolved = await input; | ||
// `resolved` is typed as `number` but was not actually checked at runtime. | ||
// Multiplication will return NaN if the input promise did not actually contain a number. | ||
return 2 * resolved; | ||
} | ||
async function goodNumberAssertion(input: unknown) { | ||
assert.promise(input); | ||
// `input` is typed as `Promise<unknown>` | ||
const resolved = await input; | ||
// `resolved` is typed as `unknown` | ||
assert.number(resolved); | ||
// `resolved` is typed as `number` | ||
// Uses runtime checks so only numbers will reach the multiplication. | ||
return 2 * resolved; | ||
} | ||
badNumberAssumption(Promise.resolve('An unexpected string')); | ||
//=> NaN | ||
// This correctly throws an error because of the unexpected string value. | ||
goodNumberAssertion(Promise.resolve('An unexpected string')); | ||
``` | ||
## FAQ | ||
@@ -455,3 +570,8 @@ | ||
## For enterprise | ||
Available as part of the Tidelift Subscription. | ||
The maintainers of @sindresorhus/is and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-sindresorhus-is?utm_source=npm-sindresorhus-is&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) | ||
## Related | ||
@@ -470,3 +590,2 @@ | ||
## Maintainers | ||
@@ -477,14 +596,1 @@ | ||
- [Brandon Smith](https://github.com/brandon93s) | ||
--- | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm--sindresorhus-is?utm_source=npm--sindresorhus-is&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
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
51609
614
585
5