Comparing version 2.0.0-beta.0 to 2.0.0-beta.1
@@ -0,1 +1,2 @@ | ||
export { hasLength } from './predicates/hasLength'; | ||
export { isAny } from './predicates/isAny'; | ||
@@ -43,2 +44,2 @@ export { isArray } from './predicates/isArray'; | ||
export { isWeakMap } from './predicates/isWeakMap'; | ||
export { hasLength as isWithLength } from './predicates/isWithLength'; | ||
export { isWithLength } from './predicates/isWithLength'; |
@@ -0,1 +1,2 @@ | ||
export { hasLength } from './predicates/hasLength'; | ||
export { isAny } from './predicates/isAny'; | ||
@@ -43,3 +44,3 @@ export { isArray } from './predicates/isArray'; | ||
export { isWeakMap } from './predicates/isWeakMap'; | ||
export { hasLength as isWithLength } from './predicates/isWithLength'; | ||
export { isWithLength } from './predicates/isWithLength'; | ||
//# sourceMappingURL=predicates.js.map |
import { and } from '../generics/and'; | ||
import { at } from '../generics/at'; | ||
import { isFunction } from './isFunction'; | ||
import { isLength } from './isLength'; | ||
export const isArrayLike = and(at('length', isLength), (value) => !isFunction(value)); | ||
import { hasLength } from './hasLength'; | ||
export const isArrayLike = and(hasLength, (value) => !isFunction(value)); | ||
//# sourceMappingURL=isArrayLike.js.map |
@@ -1,3 +0,4 @@ | ||
export declare const hasLength: import("../types").Predicate<{ | ||
/** @deprecated */ | ||
export declare const isWithLength: import("../types").Predicate<{ | ||
length: number; | ||
}>; |
import { at } from '../generics/at'; | ||
import { isLength } from './isLength'; | ||
export const hasLength = at('length', isLength); | ||
/** @deprecated */ | ||
export const isWithLength = at('length', isLength); | ||
//# sourceMappingURL=isWithLength.js.map |
@@ -1,10 +0,1 @@ | ||
export declare type InferredPartial<T extends {}> = { | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? K : never; | ||
}[keyof T]]: InferredPartial<T[P]>; | ||
} & Partial<{ | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? never : K; | ||
}[keyof T]]: InferredPartial<T[P]>; | ||
}>; | ||
export declare type Predicate<T> = <U>(value: U) => value is Extract<U, T>; | ||
@@ -26,5 +17,3 @@ export declare type Static<T extends Predicate<any>> = T extends Predicate<infer R> ? R : never; | ||
}; | ||
export declare type ObjectLike = { | ||
[P in PropertyKey]: unknown; | ||
}; | ||
export declare type ObjectLike = Exclude<Some, boolean>; | ||
export declare type Primitive = SerializablePrimitive | undefined | symbol | bigint; | ||
@@ -31,0 +20,0 @@ export declare type SerializableArray = Array<Serializable> | ReadonlyArray<Serializable>; |
@@ -0,1 +1,2 @@ | ||
export { hasLength } from './predicates/hasLength'; | ||
export { isAny } from './predicates/isAny'; | ||
@@ -43,2 +44,2 @@ export { isArray } from './predicates/isArray'; | ||
export { isWeakMap } from './predicates/isWeakMap'; | ||
export { hasLength as isWithLength } from './predicates/isWithLength'; | ||
export { isWithLength } from './predicates/isWithLength'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isWithLength = exports.isWeakMap = exports.isUndefined = exports.isUint32 = exports.isUint16 = exports.isUint8 = exports.isUint = exports.isTrue = exports.isSymbol = exports.isString = exports.isSome = exports.isSet = exports.isSerializablePrimitive = exports.isSerializableObject = exports.isSerializableNumber = exports.isSerializableArray = exports.isSerializable = exports.isRegExp = exports.isPrimitive = exports.isPositive = exports.isPlainObject = exports.isObjectLike = exports.isObject = exports.isNumber = exports.isNull = exports.isNone = exports.isNever = exports.isNegative = exports.isMap = exports.isLength = exports.isInt32 = exports.isInt16 = exports.isInt8 = exports.isInt = exports.isFunction = exports.isFalse = exports.isDictionary = exports.isDate = exports.isBoolean = exports.isBigInt = exports.isArrayLike = exports.isArray = exports.isAny = void 0; | ||
exports.isWithLength = exports.isWeakMap = exports.isUndefined = exports.isUint32 = exports.isUint16 = exports.isUint8 = exports.isUint = exports.isTrue = exports.isSymbol = exports.isString = exports.isSome = exports.isSet = exports.isSerializablePrimitive = exports.isSerializableObject = exports.isSerializableNumber = exports.isSerializableArray = exports.isSerializable = exports.isRegExp = exports.isPrimitive = exports.isPositive = exports.isPlainObject = exports.isObjectLike = exports.isObject = exports.isNumber = exports.isNull = exports.isNone = exports.isNever = exports.isNegative = exports.isMap = exports.isLength = exports.isInt32 = exports.isInt16 = exports.isInt8 = exports.isInt = exports.isFunction = exports.isFalse = exports.isDictionary = exports.isDate = exports.isBoolean = exports.isBigInt = exports.isArrayLike = exports.isArray = exports.isAny = exports.hasLength = void 0; | ||
var hasLength_1 = require("./predicates/hasLength"); | ||
Object.defineProperty(exports, "hasLength", { enumerable: true, get: function () { return hasLength_1.hasLength; } }); | ||
var isAny_1 = require("./predicates/isAny"); | ||
@@ -89,3 +91,3 @@ Object.defineProperty(exports, "isAny", { enumerable: true, get: function () { return isAny_1.isAny; } }); | ||
var isWithLength_1 = require("./predicates/isWithLength"); | ||
Object.defineProperty(exports, "isWithLength", { enumerable: true, get: function () { return isWithLength_1.hasLength; } }); | ||
Object.defineProperty(exports, "isWithLength", { enumerable: true, get: function () { return isWithLength_1.isWithLength; } }); | ||
//# sourceMappingURL=predicates.js.map |
@@ -5,6 +5,5 @@ "use strict"; | ||
const and_1 = require("../generics/and"); | ||
const at_1 = require("../generics/at"); | ||
const isFunction_1 = require("./isFunction"); | ||
const isLength_1 = require("./isLength"); | ||
exports.isArrayLike = and_1.and(at_1.at('length', isLength_1.isLength), (value) => !isFunction_1.isFunction(value)); | ||
const hasLength_1 = require("./hasLength"); | ||
exports.isArrayLike = and_1.and(hasLength_1.hasLength, (value) => !isFunction_1.isFunction(value)); | ||
//# sourceMappingURL=isArrayLike.js.map |
@@ -1,3 +0,4 @@ | ||
export declare const hasLength: import("../types").Predicate<{ | ||
/** @deprecated */ | ||
export declare const isWithLength: import("../types").Predicate<{ | ||
length: number; | ||
}>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hasLength = void 0; | ||
exports.isWithLength = void 0; | ||
const at_1 = require("../generics/at"); | ||
const isLength_1 = require("./isLength"); | ||
exports.hasLength = at_1.at('length', isLength_1.isLength); | ||
/** @deprecated */ | ||
exports.isWithLength = at_1.at('length', isLength_1.isLength); | ||
//# sourceMappingURL=isWithLength.js.map |
@@ -1,10 +0,1 @@ | ||
export declare type InferredPartial<T extends {}> = { | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? K : never; | ||
}[keyof T]]: InferredPartial<T[P]>; | ||
} & Partial<{ | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? never : K; | ||
}[keyof T]]: InferredPartial<T[P]>; | ||
}>; | ||
export declare type Predicate<T> = <U>(value: U) => value is Extract<U, T>; | ||
@@ -26,5 +17,3 @@ export declare type Static<T extends Predicate<any>> = T extends Predicate<infer R> ? R : never; | ||
}; | ||
export declare type ObjectLike = { | ||
[P in PropertyKey]: unknown; | ||
}; | ||
export declare type ObjectLike = Exclude<Some, boolean>; | ||
export declare type Primitive = SerializablePrimitive | undefined | symbol | bigint; | ||
@@ -31,0 +20,0 @@ export declare type SerializableArray = Array<Serializable> | ReadonlyArray<Serializable>; |
{ | ||
"name": "isntnt", | ||
"version": "2.0.0-beta.0", | ||
"version": "2.0.0-beta.1", | ||
"description": "A collection of composable JavaScript runtime type predicates with TypeScript type guard declarations", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
682
README.md
Isntnt is a collection of composable JavaScript runtime type predicates with TypeScript type guard declarations. Supports generics including union and intersection types. | ||
# Generics | ||
# Predicates | ||
## above | ||
`(floor: number) => (value: unknown) => value is number` | ||
A predicate is a function that checks whether a value matches a certain type, it always returns a boolean. Well known examples of JavaScript predicates are `Array.isArray`, `isNan`, and `Number.isNaN`. With `isntnt` you add a whole slew of additional predicates to your toolbox, they are listed below. | ||
```typescript | ||
const isAboveZero = above(0) | ||
``` | ||
## hasLength | ||
## and | ||
`<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Predicate<Intersect<Static<T[number]>>>` | ||
Returns `true` when you pass an object with a length property that is a valid [`length`](#isLength). | ||
```typescript | ||
const isBetween0And21 = and(above(0), below(21)) | ||
const isUser = shape({ name: isString }) | ||
const hasEmailAddress = at('email', isString) | ||
const isUserWithEmail = and(isUser, hasEmailAddress) // (value: unknown) => { name: string } & { email: string } | ||
isWithLength(value) | ||
``` | ||
## array | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is Array<T>` | ||
```typescript | ||
const isAnyArray = array(isAny) // (value: unknown) => value is Array<any> | ||
``` | ||
## at | ||
`<T extends PropertyKey, U>(key: T, predicate: Predicate<U>) => (value: unknown) => value is { [P in T]: U }` | ||
```typescript | ||
const isAnyAtFoo = at('foo', isAny) // (value: unknown) => value is { foo: any } | ||
``` | ||
## below | ||
`(max: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isBelow21 = below(21) | ||
``` | ||
## either | ||
`<T extends Array<Primitive>>(...literalValues: T) => (value: unknown) => value is T[number]` | ||
```typescript | ||
const isFooOrBar = either('foo', 'bar') // (value: unknown) => value is 'foo' | 'bar' | ||
``` | ||
## has | ||
`<T extends PropertyKey>(key: T) => (value: unknown) => value is { [P in T]: unknown }` | ||
```typescript | ||
const hasFoo = has('foo') // (value: unknown) => value is { 'foo': unknown } | ||
``` | ||
## instance | ||
`<T extends Constructor<any, any>>(constructor: T) => (value: unknown) => value is InstanceType<T>` | ||
```typescript | ||
const isInstanceofString = instance(String) // (value: unknown) => value is String | ||
``` | ||
## literal | ||
`<T extends Primitive>(literalValue: T) => (value: unknown) => value is T` | ||
```typescript | ||
const is42 = literal(42) // (value: unknown) => value is 42 | ||
``` | ||
## max | ||
`<T extends number>(max: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isMax255 = max(255) | ||
``` | ||
## maybe | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null | undefined` | ||
```typescript | ||
const isMaybeString = maybe(isString) // (value: unknown) => value is string | null | undefined | ||
``` | ||
## min | ||
`(min: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isMin18 = min(18) | ||
``` | ||
## noneable | ||
Aliases [`maybe`](#maybe) | ||
## nullable | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null` | ||
```typescript | ||
const isNullableString = nullable(isString) // (value: unknown) => value is string | null | ||
``` | ||
## object | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is Record<any, T>` | ||
```typescript | ||
const isEnum = object(isUint) // (value: unknown) => value is Record<any, number> | ||
``` | ||
## optional | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | undefined` | ||
```typescript | ||
const isOptionalString = optional(isString) // (value: unknown) => value is string | undefined | ||
``` | ||
## or | ||
`<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Static<T[number]>` | ||
```typescript | ||
const isStringOrNumber = or(isString, isNumber) // (value: unknown) => value is string | number | ||
``` | ||
## record | ||
`<T extends PropertyKey, U>(keyPredicate: Predicate<T>, valuePredicate: Predicate<U>) => (value: unknown) => value is Record<T, U>` | ||
```typescript | ||
const isDictionary = record(isString, isInt) // (value: unknown) => value is Record<string, number> | ||
``` | ||
## shape | ||
`<T extends Record<PropertyKey, Predicate<any>>>(predicates: T) => (value: unknown) => value is { [P in keyof T]: Static<T[P]> }` | ||
> Note: Actual signature also considers optional members (`{ name?: T }`) in its `Predicate` type | ||
```typescript | ||
const isCoordinate = shape({ x: isNumber, y: isNumber }) // (value: unknown) => value is { x: number, y: number } | ||
``` | ||
## test | ||
`(expression: RegExp) => (value: unknown) => value is string` | ||
```typescript | ||
const isSlug = test(/^[\w-]+$/) | ||
``` | ||
## tuple | ||
`<T extends Array<any>>(...predicates: { [K in keyof T]: Predicate<T[K]> }) => (value: unknown) => value is T` | ||
```typescript | ||
const isPoint = tuple(isNumber, isNumber) // (value: unknown) => value is [number, number] | ||
``` | ||
# Predicates | ||
## isAny | ||
`(value: unknown) => value is any` | ||
@@ -168,4 +24,5 @@ Always returns `true`. | ||
## isArray | ||
`(value: unknown) => value is Array<unknown>` | ||
Returns `true` when you pass an array. | ||
```typescript | ||
@@ -176,4 +33,5 @@ isArray(value) | ||
## isArrayLike | ||
`(value: unknown) => value is Record<number, unknown>` | ||
Returns `true` when you pass a object that is not a function with a length property that is a valid [`length`](#isLength). | ||
```typescript | ||
@@ -184,4 +42,5 @@ isArrayLike(value) | ||
## isBigInt | ||
`(value: unknown) => value is bigint` | ||
Returns `true` when you pass a bigint. | ||
```typescript | ||
@@ -192,4 +51,5 @@ isBigInt(value) | ||
## isBoolean | ||
`(value: unknown) => value is boolean` | ||
Returns `true` when you pass a boolean. | ||
```typescript | ||
@@ -200,4 +60,5 @@ isBoolean(value) | ||
## isDate | ||
`(value: unknown) => value is Date` | ||
Returns `true` when you pass a Date object. | ||
```typescript | ||
@@ -208,4 +69,5 @@ isDate(value) | ||
## isDictionary | ||
`(value: unknown) => value is Record<any, string>` | ||
Returns `true` when you pass an object of which each property name, and each property value are a string. | ||
```typescript | ||
@@ -216,4 +78,5 @@ isDictionary(value) | ||
## isFalse | ||
`(value: unknown) => value is false` | ||
Returns `true` when you pass `false`. | ||
```typescript | ||
@@ -224,4 +87,5 @@ isFalse(value) | ||
## isFunction | ||
`(value: unknown) => value is Function` | ||
Returns `true` when you pass a function. | ||
```typescript | ||
@@ -232,4 +96,5 @@ isFunction(value) | ||
## isInt | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an integer (a whole number). | ||
```typescript | ||
@@ -240,4 +105,5 @@ isInt(value) | ||
## isInt8 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [integer](#isInt) ranging from -128 to 127. | ||
```typescript | ||
@@ -248,4 +114,5 @@ isInt8(value) | ||
## isInt16 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [integer](#isInt) ranging from -32,768 to 32,767. | ||
```typescript | ||
@@ -256,4 +123,5 @@ isInt16(value) | ||
## isInt32 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [integer](#isInt) ranging from -2,147,483,648 to 2,147,483,647. | ||
```typescript | ||
@@ -264,4 +132,5 @@ isInt32(value) | ||
## isLength | ||
`(value: unknown) => value is number` | ||
Alias to [`isUint32`](#isUint32). | ||
```typescript | ||
@@ -272,4 +141,5 @@ isLength(value) | ||
## isMap | ||
`(value: unknown) => value is Map<any, unknown>` | ||
Returns `true` when you pas a Map object. | ||
```typescript | ||
@@ -280,4 +150,5 @@ isMap(value) | ||
## isNegative | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pas a negative number (including -0). | ||
```typescript | ||
@@ -288,5 +159,4 @@ isNegative(value) | ||
## isNever | ||
`(value: unknown) => value is never` | ||
Always returns `false`; | ||
Always returns `false`. | ||
@@ -298,4 +168,5 @@ ```typescript | ||
## isNone | ||
`(value: unknown) => value is null | undefined` | ||
Returns `true` when you pass `null` or `undefined`. | ||
```typescript | ||
@@ -306,4 +177,5 @@ isNone(value) | ||
## isNull | ||
`(value: unknown) => value is null` | ||
Returns `true` when you pass `null`. | ||
```typescript | ||
@@ -314,4 +186,5 @@ isNull(value) | ||
## isNumber | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass a number that is not `NaN`. | ||
```typescript | ||
@@ -322,4 +195,5 @@ isNumber(value) | ||
## isObject | ||
`(value: unknown) => value is object` | ||
Returns `true` when you pass an object that is not `null`. | ||
```typescript | ||
@@ -330,4 +204,5 @@ isObject(value) | ||
## isObjectLike | ||
`(value: unknown) => value is ObjectLike` | ||
Returns `true` when you pass an value that is not a boolean, `null`, or `undefined`. | ||
```typescript | ||
@@ -338,4 +213,5 @@ isObjectLike(value) | ||
## isPlainObject | ||
`(value: unknown) => value is {}` | ||
Returns `true` when you pass an object that was constructed by `Object`. | ||
```typescript | ||
@@ -346,4 +222,5 @@ isPlainObject(value) | ||
## isPositive | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass a positive number. | ||
```typescript | ||
@@ -354,4 +231,5 @@ isPositive(value) | ||
## isPrimitive | ||
`(value: unknown) => value is Primitive` | ||
Returns `true` when you pass `null`, `undefined`, a boolean, bigint, number, symbol, or string. | ||
```typescript | ||
@@ -361,6 +239,6 @@ isPrimitive(value) | ||
## isRegExp | ||
`(value: unknown) => value is RegExp` | ||
Returns `true` when you pass a RegExp object. | ||
```typescript | ||
@@ -371,4 +249,5 @@ isRegExp(value) | ||
## isSerializable | ||
`(value: unknown) => value is Serializable` | ||
Returns `true` when you pass a [serializable primitive](#isSerializablePrimitive), a [serializable array](#isSerializableArray), or [serializable object](#isSerializableObject). | ||
```typescript | ||
@@ -379,4 +258,5 @@ isSerializable(value) | ||
## isSerializableArray | ||
`(value: unknown) => value is Array<Serializable>` | ||
Returns `true` when you pass an array where every element is [serializable](#isSerializable). | ||
```typescript | ||
@@ -387,4 +267,5 @@ isSerializableArray(value) | ||
## isSerializableNumber | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass a number that is not `NaN`, `Infinity`, or `-Infinity`. | ||
```typescript | ||
@@ -395,4 +276,5 @@ isSerializableNumber(value) | ||
## isSerializableObject | ||
`(value: unknown) => value is Record<string, Serializable>` | ||
Returns `true` when you pass an object where every property is [serializable](#isSerializable). | ||
```typescript | ||
@@ -403,4 +285,5 @@ isSerializableObject(value) | ||
## isSerializablePrimitive | ||
`(value: unknown) => value is SerializablePrimitive` | ||
Returns `true` when you pass `null`, a [serializable number](#isSerializableNumber), a boolean, bigint, symbol, or string. | ||
```typescript | ||
@@ -411,4 +294,5 @@ isSerializablePrimitive(value) | ||
## isSet | ||
`(value: unknown) => value is Set<unknown>` | ||
Returns `true` when you pas a Set object. | ||
```typescript | ||
@@ -419,4 +303,5 @@ isSet(value) | ||
## isSome | ||
`(value: unknown) => value is Some` | ||
Returns `true` when you pas a value other than `null` or `undefined`. | ||
```typescript | ||
@@ -427,4 +312,5 @@ isSome(value) | ||
## isString | ||
`(value: unknown) => value is string` | ||
Returns `true` when you pas a string. | ||
```typescript | ||
@@ -435,4 +321,5 @@ isString(value) | ||
## isSymbol | ||
`(value: unknown) => value is symbol` | ||
Returns `true` when you pas a symbol. | ||
```typescript | ||
@@ -443,4 +330,5 @@ isSymbol(value) | ||
## isTrue | ||
`(value: unknown) => value is true` | ||
Returns `true` when you pas `true`. | ||
```typescript | ||
@@ -451,4 +339,5 @@ isTrue(value) | ||
## isUint | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass a positive [integer](#isInteger). | ||
```typescript | ||
@@ -459,4 +348,5 @@ isUint(value) | ||
## isUint8 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [unsigned integer](#isUint) ranging from 0 to 255. | ||
```typescript | ||
@@ -467,4 +357,5 @@ isUint8(value) | ||
## isUint16 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [unsigned integer](#isUint) ranging from 0 to 65,535. | ||
```typescript | ||
@@ -475,4 +366,5 @@ isUint16(value) | ||
## isUint32 | ||
`(value: unknown) => value is number` | ||
Returns `true` when you pass an [unsigned integer](#isUint) ranging from 0 to 4,294,967,295. | ||
```typescript | ||
@@ -483,4 +375,5 @@ isUint32(value) | ||
## isUndefined | ||
`(value: unknown) => value is undefined` | ||
Returns `true` when you pass `undefined`. | ||
```typescript | ||
@@ -491,4 +384,5 @@ isUndefined(value) | ||
## isWeakMap | ||
`(value: unknown) => value is WeakMap<any, unknown>` | ||
Returns `true` when you pass a `WeakMap` object. | ||
```typescript | ||
@@ -498,9 +392,285 @@ isWeakMap(value) | ||
## isWithLength | ||
`(value: unknown) => value is { length: number }` | ||
# Predicate factories | ||
Predicate factories is where the real fun begins, they allow you to create and combine predicates of your own to create new predicates. By composing predicates you can describe objects, union types, number ranges, and so on. | ||
## above | ||
Create a predicate based on a floor value, it checks if a [number](#isNumber) exceeds the floor value it’s given. This is an analogue to the `>` operator. | ||
```typescript | ||
isWithLength(value) | ||
const isAboveZero = above(0) | ||
isAboveZero(-1) // false | ||
isAboveZero(12) // true | ||
``` | ||
The `isAboveZero` predicate above returns `true` when you pass a [number](#isNumber) that is greater than `0`. | ||
## and | ||
Create a predicate based on several other predicates, it returns `true` if every predicate that is passed also returns `true`. This is an analogue to the `&&` operator. | ||
```typescript | ||
const adultAge = 18 | ||
const isMinorAge = and(isUint, below(adultAge)) | ||
isMinorAge(12) // true | ||
isMinorAge(33) // false | ||
``` | ||
The `isMinorAge` predicate above returns `true` when you pass an [unsigned integer](#isUint) that is below 18. | ||
```typescript | ||
const isUser = shape({ name: isString }) | ||
const hasEmailAddress = shape({ email: isString }) | ||
const isUserWithEmail = and(isUser, hasEmailAddress) | ||
``` | ||
The `isUserWithEmail` predicate above returns `true` when you pass an object with a `name` and an `email` property, both of which are a string. | ||
## array | ||
Create a predicate that checks every array element, it returns `true` when its predicate returns `true` for every element in an array. | ||
```typescript | ||
const isNumberArray = array(isNumber) | ||
``` | ||
The `isNumberArray` predicate above returns `true` when you pass an array of which every element is a [number](#isNumber). | ||
## at | ||
Create a predicate based on a property key and another predicate, it returns `true` when its predicate returns `true` for the value of an object at its given key. | ||
```typescript | ||
const isStringAtName = at('name', isString) | ||
isStringAtName({ name: 'Jane' }) // true | ||
isStringAtName({ name: null }) // false | ||
``` | ||
The `isStringAtName` predicate above returns `true` when you pass an object of which the `name` property is a string. | ||
## below | ||
Create a predicate based on a ceiling value, it checks if a [number](#isNumber) is below the ceiling value it’s given. This is an analogue to the `<` operator. | ||
```typescript | ||
const isBelow100 = below(100) | ||
isBelow100(124) // false | ||
isBelow100(32) // true | ||
``` | ||
The `isBelow100` predicate above returns `true` when you pass a [number](#isNumber) that is less than `100`. | ||
## either | ||
Create a predicate based on several [literal](#literal) values, it returns `true` if a value matches either of the primitive values it’s given. | ||
```typescript | ||
const isAOrB = either('a', 'b') | ||
isAOrB('a') // true | ||
isAOrB('c') // false | ||
``` | ||
The `isAOrB` predicate above returns `true` when you pass either `a`, or `b`. | ||
## has | ||
Create a predicate based on a property key, it returns `true` if an object has a property of the key it’s given. | ||
```typescript | ||
const hasName = has('name') | ||
hasName({ name: null }) // true | ||
hasName({ age: 36 }) // false | ||
``` | ||
The `hasName` predicate above returns `true` when you pass an object that has a `name` property. | ||
## instance | ||
Create a predicate based on a constructor, it returns `true` if an object is an instance of the constructor key it’s given. | ||
```typescript | ||
const isInstanceofString = instance(String) | ||
isInstanceofString(new String('foo')) // true | ||
isInstanceofString('foo') // false | ||
``` | ||
The `isInstanceofString` predicate above returns `true` when you pass a String object. | ||
## literal | ||
Create a predicate based a [primitive](#isPrimitive) value, it returns `true` if a value equals the primitive value it’s given. | ||
```typescript | ||
const is42 = literal(42) | ||
is42(42) // true | ||
is42(43) // false | ||
``` | ||
The `is42` predicate above returns `true` when you pass `42`. | ||
## max | ||
Create a predicate based on a max. value, it checks if a [number](#isNumber) is below or equal to the max. value it’s given. This is an analogue to the `<=` operator. | ||
```typescript | ||
const isMax100 = max(100) | ||
isMax100(100) // true | ||
isMax100(101) // false | ||
``` | ||
The `isMax100` predicate above returns `true` when you pass a [number](#isNumber) that is below or equal to `100`. | ||
## maybe | ||
Create a predicate based on another predicate, it checks if a value matches it’s given predicate, or equals `null` or `undefined`. | ||
```typescript | ||
const isMaybeString = maybe(isString) | ||
isMaybeString(null) // true | ||
isMaybeString(12) // false | ||
``` | ||
The `isMaybeString` predicate above returns `true` when you pass a string, `null`, or `undefined`. | ||
## min | ||
Create a predicate based on a min. value, it checks if a [number](#isNumber) is above or equal to the min. value it’s given. This is an analogue to the `>=` operator. | ||
```typescript | ||
const isMin100 = min(100) | ||
isMin100(100) // true | ||
isMin100(99) // false | ||
``` | ||
The `isMin0` predicate above returns `true` when you pass a [number](#isNumber) that is above or equal to `100`. | ||
## noneable | ||
Aliases [`maybe`](#maybe). | ||
## nullable | ||
Create a predicate based on another predicate, it checks if a value matches it’s given predicate, or equals `null`. | ||
```typescript | ||
const isNullableString = nullable(isString) | ||
isNullableString(null) // true | ||
isNullableString(12) // false | ||
``` | ||
The `isNullableString` predicate above returns `true` when you pass a string or `null`. | ||
## object | ||
Create a predicate that checks every object property, it returns `true` when its predicate returns `true` for every object value. | ||
```typescript | ||
const isEnumerable = object(isUint) | ||
``` | ||
The `isEnumerable` predicate above returns `true` when you pass an object of which every element is an [unsigned integer](#isUint). | ||
## optional | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | undefined` | ||
```typescript | ||
const isOptionalString = optional(isString) | ||
isOptionalString(undefined) // true | ||
isOptionalString(12) // false | ||
``` | ||
The `isOptionalString` predicate above returns `true` when you pass a string or `undefined`. | ||
## or | ||
Create a predicate based on several other predicates, it returns `true` if some predicate that is passed also returns `true`. This is an analogue to the `||` operator. | ||
```typescript | ||
const isContent = or(isString, isSerializableNumber) | ||
isContent(12) // true | ||
isContent(null) // false | ||
``` | ||
The `isContent` predicate above returns `true` when you pass a string or a [primitive number](#isPrimitiveNumber). | ||
```typescript | ||
const isStringOrNumber = or(isString, isNumber) | ||
isStringOrNumber(8) // true | ||
isStringOrNumber([]) // false | ||
``` | ||
## record | ||
Create a predicate based on a key predicate and a value predicate, it returns `true` if each property key of an object returns `true` for its key predicate, and each value returns `true` for its value predicate. | ||
```typescript | ||
const isValidKey = either('a', 'b') | ||
const isKeyValueMap = record(isValidKey, isInt) | ||
isKeyValueMap({ a: 2 }) // true | ||
isKeyValueMap({ a: null }) // false | ||
isKeyValueMap({ c: 2 }) // false | ||
``` | ||
The `isKeyValueMap` predicate above returns `true` when you pass an object where each key is either `a` or `b`, and each property value is an [integer](#isInt). | ||
## shape | ||
Create a predicate based on a predicate object, it returns `true` if each property value of an object returns `true` for its corresponding predicate. | ||
```typescript | ||
const isPoint = shape({ x: isNumber, y: isNumber }) | ||
isPoint({ x: 12, y: 36 }) // true | ||
isPoint({ x: 12 }) // false | ||
``` | ||
The `isPoint` predicate above returns `true` when you an pass an object where both `x` and `y` properties are a [number](#isNumber). | ||
## test | ||
```typescript | ||
const isSlug = test(/^[A-Z0-9-]+$/i) | ||
isSlug('foo-bar') // true | ||
isSlug('!#$') // false | ||
``` | ||
The `isSlug` predicate above returns `true` when you an pass a string that matches `/^[A-Z0-9-]+$/i`. | ||
## tuple | ||
Create a predicate based on index-based predicates, it returns `true` if the element at each index of an array with a fixed length returns `true` for its corresponding predicate. | ||
```typescript | ||
const isLatitude = and(min(-90), max(90)) | ||
const isLongitude = and(min(-180), max(180)) | ||
const isLatLong = tuple(isLatitude, isLongitude) | ||
isLatLong(45, -120) // true | ||
isLatLong(120, 200) // false | ||
``` | ||
The `isLatLong` predicate above returns `true` when you an pass an array with a length of 2 where index 0 is a latitude, and index 1 is a longitude value. | ||
# Types | ||
@@ -511,13 +681,27 @@ | ||
```typescript | ||
Intersect<A | B> // A & B | ||
type User = Intersect<{ name: string } | { email: string }> | ||
``` | ||
equals | ||
```typescript | ||
type User = { | ||
name: string | ||
} & { | ||
email: string | ||
} | ||
``` | ||
## Maybe | ||
Unions a type with `null` and `undefined`. | ||
```typescript | ||
Maybe<T> // T | null | undefined | ||
type MaybeString = Maybe<string> | ||
``` | ||
equals | ||
```typescript | ||
type MaybeString = Maybe<string> // string | null | undefined | ||
type MaybeString = string | null | undefined | ||
``` | ||
@@ -527,14 +711,26 @@ | ||
Union of `null` and `undefined`. | ||
```typescript | ||
None // null | undefined | ||
type NoneType = None | ||
``` | ||
equals | ||
```typescript | ||
type NoneType = null | undefined | ||
``` | ||
## Nullable | ||
Unions a type with `null`. | ||
```typescript | ||
Nullable<T> // T | null | ||
type NullableString = Nullable<string> | ||
``` | ||
equals | ||
```typescript | ||
type NullableString = Nullable<string> // string | null | ||
type NullableString = string | null | ||
``` | ||
@@ -544,8 +740,12 @@ | ||
Unions a type with `undefined`. | ||
```typescript | ||
Optional<T> // T | undefined | ||
type OptionalString = Optional<string> | ||
``` | ||
equals | ||
```typescript | ||
type OptionalString = Optional<string> // string | undefined | ||
type OptionalString = string | undefined | ||
``` | ||
@@ -555,50 +755,90 @@ | ||
Describes a predicate function signature | ||
```typescript | ||
Predicate<T> // (value: unknown, ...rest: Array<unknown>) => value is T | ||
type IsString = Predicate<string> | ||
``` | ||
equals | ||
```typescript | ||
type IsString = <T>(value: T) => value is Extract<T, string> | ||
``` | ||
## Primitive | ||
Union of all primitive types. | ||
```typescript | ||
Primitive // null | undefined | boolean | number | string | symbol | bigint | ||
type PrimitiveType = Primitive | ||
``` | ||
equals | ||
```typescript | ||
type PrimitiveType = null | undefined | boolean | number | string | symbol | bigint | ||
``` | ||
## Serializable | ||
```typescript | ||
Serializable // SerializableArray | SerializableObject | SerializablePrimitive | ||
type SerializableType = Serializable | ||
``` | ||
equals | ||
```typescript | ||
type SerializableType = SerializableArray | SerializableObject | SerializableObject | ||
``` | ||
## SerializableArray | ||
```typescript | ||
SerializableArray // Array<Serializable> | ||
type SerializableArrayType = SerializableArray | ||
``` | ||
equals | ||
```typescript | ||
type SerializableArrayType = Array<Serializable> | ReadonlyArray<Serializable> | ||
``` | ||
## SerializableObject | ||
```typescript | ||
SerializableObject // Partial<{ [key: string]: Serializable }> | ||
type SerializableObjectType = SerializableObject | ||
``` | ||
equals | ||
```typescript | ||
type SerializableObjectType = { [key: string]: Serializable | undefined } | ||
``` | ||
## SerializablePrimitive | ||
```typescript | ||
SerializablePrimitive // null | boolean | number | string | ||
type SerializablePrimitiveType = SerializablePrimitive | ||
``` | ||
equals | ||
```typescript | ||
type SerializablePrimitiveType = null | boolean | number | string | ||
``` | ||
## Some | ||
Excludes `null` and `undefined` from type. | ||
```typescript | ||
Some // Function | boolean | bigint | number | string | symbol | object | ||
Some<T> // Exclude<T, undefined | null> | ||
type SomeType = Some | ||
type SomeString = Some<string | null> | ||
``` | ||
equals | ||
```typescript | ||
// Make sure `T` is not `null` or `undefined` | ||
type Option<T extends Some, E extends Error> = T | E | ||
// Remove `null` or `undefined` from a type | ||
type MaybeString = Optional<string> // string | null | undefined | ||
type SomeString = Some<MaybeString> // string | ||
type SomeType = Function | boolean | bigint | number | string | symbol | object | ||
type SomeString = string | ||
``` | ||
@@ -608,8 +848,12 @@ | ||
Extracts type parameter from Predicate type. This is useful to extract TypeScript types from (complex) predicates. | ||
```typescript | ||
Static<Predicate<T>> // T | ||
type PredicateType = Static<Predicate<string>>> | ||
``` | ||
equals | ||
```typescript | ||
type True = Static<typeof isTrue> // true | ||
type PredicateType = string | ||
``` |
@@ -0,1 +1,2 @@ | ||
export { hasLength } from './predicates/hasLength' | ||
export { isAny } from './predicates/isAny' | ||
@@ -43,2 +44,2 @@ export { isArray } from './predicates/isArray' | ||
export { isWeakMap } from './predicates/isWeakMap' | ||
export { hasLength as isWithLength } from './predicates/isWithLength' | ||
export { isWithLength } from './predicates/isWithLength' |
import { and } from '../generics/and' | ||
import { at } from '../generics/at' | ||
import { isFunction } from './isFunction' | ||
import { isLength } from './isLength' | ||
import { ArrayLike, Predicate } from '../types' | ||
import { hasLength } from './hasLength' | ||
export const isArrayLike = and( | ||
at('length', isLength), | ||
hasLength, | ||
(value: unknown): value is any => !isFunction(value), | ||
) as Predicate<ArrayLike> |
import { at } from '../generics/at' | ||
import { isLength } from './isLength' | ||
export const hasLength = at('length', isLength) | ||
/** @deprecated */ | ||
export const isWithLength = at('length', isLength) |
@@ -1,14 +0,1 @@ | ||
export type InferredPartial<T extends {}> = { | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? K : never | ||
}[keyof T]]: InferredPartial<T[P]> | ||
} & | ||
Partial< | ||
{ | ||
[P in { | ||
[K in keyof T]: Extract<T[K], undefined> extends never ? never : K | ||
}[keyof T]]: InferredPartial<T[P]> | ||
} | ||
> | ||
export type Predicate<T> = <U>(value: U) => value is Extract<U, T> | ||
@@ -42,5 +29,3 @@ | ||
export type ObjectLike = { | ||
[P in PropertyKey]: unknown | ||
} | ||
export type ObjectLike = Exclude<Some, boolean> | ||
@@ -47,0 +32,0 @@ export type Primitive = SerializablePrimitive | undefined | symbol | bigint |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
246337
540
813
3721