Comparing version 1.4.1 to 1.4.2
@@ -1,2 +0,2 @@ | ||
import { Static, Predicate } from '../types'; | ||
export declare const object: <T extends Predicate<any>>(predicate: T) => (value: any) => value is Record<any, Static<T>>; | ||
import { Predicate } from '../types'; | ||
export declare const object: <T>(predicate: Predicate<T>) => (value: any) => value is Record<any, T>; |
@@ -1,2 +0,2 @@ | ||
import { Predicate, Static } from '../types'; | ||
export declare const record: <K extends Predicate<string | symbol>, T extends Predicate<any>>(keyPredicate: K, valuePredicate: T) => (value: unknown) => value is Record<Static<K>, Static<T>>; | ||
import { Predicate } from '../types'; | ||
export declare const record: <K extends string | number | symbol, T>(keyPredicate: Predicate<K>, valuePredicate: Predicate<T>) => (value: unknown) => value is Record<K, T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const predicates_1 = require("../predicates"); | ||
exports.record = (keyPredicate, valuePredicate) => { | ||
return (value) => { | ||
const isObjectValue = predicates_1.isObject(value); | ||
if (isObjectValue) { | ||
for (const key in value) { | ||
if (Object.hasOwnProperty.call(value, key) && | ||
(!keyPredicate(key) || !valuePredicate(value[key]))) { | ||
return false; | ||
} | ||
exports.record = (keyPredicate, valuePredicate) => (value) => { | ||
const isObjectValue = predicates_1.isObject(value); | ||
if (isObjectValue) { | ||
for (const key in value) { | ||
if (Object.hasOwnProperty.call(value, key) && | ||
(!keyPredicate(key) || !valuePredicate(value[key]))) { | ||
return false; | ||
} | ||
} | ||
return isObjectValue; | ||
}; | ||
} | ||
return isObjectValue; | ||
}; | ||
//# sourceMappingURL=record.js.map |
import { Predicate } from '../../index'; | ||
export declare const isArray: Predicate<Array<unknown>>; | ||
export declare const isArray: Predicate<Array<any>>; |
@@ -1,2 +0,1 @@ | ||
import { Predicate } from '../types'; | ||
export declare const isDictionary: Predicate<Record<any, string>>; | ||
export declare const isDictionary: (value: any) => value is Record<any, string>; |
@@ -1,2 +0,2 @@ | ||
import { Predicate, ObjectLike } from '../types'; | ||
export declare const isObjectLike: Predicate<ObjectLike>; | ||
import { ObjectLike } from '../types'; | ||
export declare const isObjectLike: (value: unknown) => value is ObjectLike; |
@@ -1,4 +0,1 @@ | ||
declare type ObjectWith<K extends PropertyKey> = { | ||
[P in K]: unknown; | ||
}; | ||
export declare type InferredPartial<T extends {}> = { | ||
@@ -24,10 +21,11 @@ [P in { | ||
export declare type Nullable<T> = T | null; | ||
export declare type ObjectLike = ObjectWith<PropertyKey>; | ||
export declare type Primitive = null | undefined | boolean | number | string | symbol | bigint; | ||
export declare type SerializableArray = Array<Serializable>; | ||
export declare type ObjectLike = { | ||
[P in PropertyKey]: unknown; | ||
}; | ||
export declare type Primitive = SerializablePrimitive | undefined | symbol | bigint; | ||
export declare type SerializableArray = Array<Serializable> | ReadonlyArray<Serializable>; | ||
export declare type SerializablePrimitive = null | boolean | number | string; | ||
export declare type SerializableObject = Partial<{ | ||
[key: string]: Serializable; | ||
}>; | ||
export declare type SerializableObject = { | ||
[key: string]: Serializable | undefined; | ||
}; | ||
export declare type Serializable = SerializablePrimitive | SerializableObject | SerializableArray; | ||
export {}; |
{ | ||
"name": "isntnt", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"description": "A collection of composable JavaScript runtime type predicates with TypeScript type guard declarations", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
185
README.md
@@ -6,14 +6,22 @@ Isntnt is a collection of composable JavaScript runtime type predicates with TypeScript type guard declarations. Supports generics including union and intersection types. | ||
## above | ||
`(floor: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isAboveZero = above(0) // (value: unknown) => value is number | ||
const isAboveZero = above(0) | ||
``` | ||
## and | ||
`<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Predicate<Intersect<Static<T[number]>>>` | ||
```typescript | ||
const isBetween0And21 = and(above(0), below(21)) // (value: unknown) => value is number | ||
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 } | ||
``` | ||
## array | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is Array<T>` | ||
@@ -25,2 +33,3 @@ ```typescript | ||
## at | ||
`<T extends PropertyKey, U>(key: T, predicate: Predicate<U>) => (value: unknown) => value is { [P in T]: U }` | ||
@@ -32,8 +41,10 @@ ```typescript | ||
## below | ||
`(max: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isBelow21 = below(21) // (value: unknown) => value is { foo: any } | ||
const isBelow21 = below(21) | ||
``` | ||
## either | ||
`<T extends Array<Primitive>>(...literalValues: T) => (value: unknown) => value is T[number]` | ||
@@ -45,2 +56,3 @@ ```typescript | ||
## has | ||
`<T extends PropertyKey>(key: T) => (value: unknown) => value is { [P in T]: unknown }` | ||
@@ -52,2 +64,3 @@ ```typescript | ||
## instance | ||
`<T extends Constructor<any, any>>(constructor: T) => (value: unknown) => value is InstanceType<T>` | ||
@@ -59,2 +72,3 @@ ```typescript | ||
## literal | ||
`<T extends Primitive>(literalValue: T) => (value: unknown) => value is T` | ||
@@ -66,8 +80,10 @@ ```typescript | ||
## max | ||
`<T extends number>(max: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isMax255 = max(255) // (value: unknown) => value is number | ||
const isMax255 = max(255) | ||
``` | ||
## maybe | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null | undefined` | ||
@@ -79,5 +95,6 @@ ```typescript | ||
## min | ||
`(min: number) => (value: unknown) => value is number` | ||
```typescript | ||
const isMin18 = min(18) // (value: unknown) => value is number | ||
const isMin18 = min(18) | ||
``` | ||
@@ -87,5 +104,6 @@ | ||
Aliases `maybe` | ||
Aliases [`maybe`](#maybe) | ||
## nullable | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | null` | ||
@@ -97,8 +115,10 @@ ```typescript | ||
## object | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is Record<any, T>` | ||
```typescript | ||
const isEnum = object(isNumber) // (value: unknown) => value is ObjectOf<number> | ||
const isEnum = object(isUint) // (value: unknown) => value is Record<any, number> | ||
``` | ||
## optional | ||
`<T>(predicate: Predicate<T>) => (value: unknown) => value is T | undefined` | ||
@@ -110,2 +130,3 @@ ```typescript | ||
## or | ||
`<T extends Array<Predicate<any>>>(...predicates: T) => (value: unknown) => value is Static<T[number]>` | ||
@@ -117,25 +138,29 @@ ```typescript | ||
## record | ||
`<T extends PropertyKey, U>(keyPredicate: Predicate<T>, valuePredicate: Predicate<U>) => (value: unknown) => value is Record<T, U>` | ||
```typescript | ||
const isEnum = record(isString, isNumber) // (value: unknown) => value is Record<string, number> | ||
const isDictionary = record(isString, isInt) // (value: unknown) => value is Record<string, number> | ||
``` | ||
Note: `record` is limited to `string` and `symbol` type keys. | ||
## 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 isPosition = shape({ x: isNumber, y: isNumber }) // (value: unknown) => value is { x: number, y: number } | ||
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-]+$/) // (value: unknown) => value is string | ||
const isSlug = test(/^[\w-]+$/) | ||
``` | ||
## tuple | ||
`<T extends Array<any>>(...predicates: { [K in keyof T]: Predicate<T[K]> }) => (value: unknown) => value is T` | ||
```typescript | ||
const isEntry = tuple(isNumber, isNumber) // (value: unknown) => value is [number, number] | ||
const isPoint = tuple(isNumber, isNumber) // (value: unknown) => value is [number, number] | ||
``` | ||
@@ -146,257 +171,305 @@ | ||
## isAny | ||
`(value: unknown) => value is any` | ||
Always returns `true`. | ||
```typescript | ||
isAny(value) // value is any | ||
isAny(value) | ||
``` | ||
## isArray | ||
`(value: unknown) => value is Array<unknown>` | ||
```typescript | ||
isArray(value) // value is Array<unknown> | ||
isArray(value) | ||
``` | ||
## isArrayLike | ||
`(value: unknown) => value is Record<number, unknown>` | ||
```typescript | ||
isArrayLike(value) // value is Record<number, unknown> | ||
isArrayLike(value) | ||
``` | ||
## isBigInt | ||
`(value: unknown) => value is bigint` | ||
```typescript | ||
isBigInt(value) // value is bigint | ||
isBigInt(value) | ||
``` | ||
## isBoolean | ||
`(value: unknown) => value is boolean` | ||
```typescript | ||
isBoolean(value) // value is boolean | ||
isBoolean(value) | ||
``` | ||
## isDate | ||
`(value: unknown) => value is Date` | ||
```typescript | ||
isDate(value) // value is Date | ||
isDate(value) | ||
``` | ||
## isDictionary | ||
`(value: unknown) => value is Record<any, string>` | ||
```typescript | ||
isDictionary(value) // value is ObjectOf<string> | ||
isDictionary(value) | ||
``` | ||
## isFalse | ||
`(value: unknown) => value is false` | ||
```typescript | ||
isFalse(value) // value is false | ||
isFalse(value) | ||
``` | ||
## isFunction | ||
`(value: unknown) => value is Function` | ||
```typescript | ||
isFunction(value) // value is Function | ||
isFunction(value) | ||
``` | ||
## isInt | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isInt(value) // value is number | ||
isInt(value) | ||
``` | ||
## isInt8 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isInt8(value) // value is number | ||
isInt8(value) | ||
``` | ||
## isInt16 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isInt16(value) // value is number | ||
isInt16(value) | ||
``` | ||
## isInt32 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isInt32(value) // value is number | ||
isInt32(value) | ||
``` | ||
## isLength | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isLength(value) // value is number | ||
isLength(value) | ||
``` | ||
## isMap | ||
`(value: unknown) => value is Map<any, unknown>` | ||
```typescript | ||
isMap(value) // value is Map<any, unknown> | ||
isMap(value) | ||
``` | ||
## isNegative | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isNegative(value) // value is number | ||
isNegative(value) | ||
``` | ||
## isNever | ||
`(value: unknown) => value is never` | ||
Always returns `false`; | ||
```typescript | ||
isNever(value) // value is never | ||
isNever(value) | ||
``` | ||
## isNone | ||
`(value: unknown) => value is null | undefined` | ||
```typescript | ||
isNone(value) // value is null | undefined | ||
isNone(value) | ||
``` | ||
## isNull | ||
`(value: unknown) => value is null` | ||
```typescript | ||
isNull(value) // value is null | ||
isNull(value) | ||
``` | ||
## isNumber | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isNumber(value) // value is number | ||
isNumber(value) | ||
``` | ||
## isObject | ||
`(value: unknown) => value is object` | ||
```typescript | ||
isObject(value) // value is {} | ||
isObject(value) | ||
``` | ||
## isObjectLike | ||
`(value: unknown) => value is ObjectLike` | ||
```typescript | ||
isObjectLike(value) // value is ObjectLike | ||
isObjectLike(value) | ||
``` | ||
## isPlainObject | ||
`(value: unknown) => value is {}` | ||
```typescript | ||
isPlainObject({}) // value is {} | ||
isPlainObject(value) | ||
``` | ||
## isPositive | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isPositive(value) // value is number | ||
isPositive(value) | ||
``` | ||
## isPrimitive | ||
`(value: unknown) => value is Primitive` | ||
```typescript | ||
isPrimitive(value) // value is Primitive | ||
isPrimitive(value) | ||
``` | ||
## isRegExp | ||
`(value: unknown) => value is RegExp` | ||
```typescript | ||
isRegExp(value) // value is RegExp | ||
isRegExp(value) | ||
``` | ||
## isSerializable | ||
`(value: unknown) => value is Serializable` | ||
```typescript | ||
isSerializable(value) // value is Serializable | ||
isSerializable(value) | ||
``` | ||
## isSerializableArray | ||
`(value: unknown) => value is Array<Serializable>` | ||
```typescript | ||
isSerializableArray(value) // value is Array<Serializable> | ||
isSerializableArray(value) | ||
``` | ||
## isSerializableNumber | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isSerializableNumber(value) // value is number | ||
isSerializableNumber(value) | ||
``` | ||
## isSerializableObject | ||
`(value: unknown) => value is Record<string, Serializable>` | ||
```typescript | ||
isSerializableObject(value) // value is Record<string, Serializable> | ||
isSerializableObject(value) | ||
``` | ||
## isSerializablePrimitive | ||
`(value: unknown) => value is SerializablePrimitive` | ||
```typescript | ||
isSerializablePrimitive(value) // value is SerializablePrimitive | ||
isSerializablePrimitive(value) | ||
``` | ||
## isSet | ||
`(value: unknown) => value is Set<unknown>` | ||
```typescript | ||
isSet(value) // value is Set<unknown> | ||
isSet(value) | ||
``` | ||
## isSome | ||
`(value: unknown) => value is Some` | ||
```typescript | ||
isSome(value) // value is Some | ||
isSome(value) | ||
``` | ||
## isString | ||
`(value: unknown) => value is string` | ||
```typescript | ||
isString(value) // value is string | ||
isString(value) | ||
``` | ||
## isSymbol | ||
`(value: unknown) => value is symbol` | ||
```typescript | ||
isSymbol(value) // value is symbol | ||
isSymbol(value) | ||
``` | ||
## isTrue | ||
`(value: unknown) => value is true` | ||
```typescript | ||
isTrue(value) // value is true | ||
isTrue(value) | ||
``` | ||
## isUint | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isUint(value) // value is number | ||
isUint(value) | ||
``` | ||
## isUint8 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isUint8(value) // value is number | ||
isUint8(value) | ||
``` | ||
## isUint16 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isUint16(value) // value is number | ||
isUint16(value) | ||
``` | ||
## isUint32 | ||
`(value: unknown) => value is number` | ||
```typescript | ||
isUint32(value) // value is number | ||
isUint32(value) | ||
``` | ||
## isUndefined | ||
`(value: unknown) => value is undefined` | ||
```typescript | ||
isUndefined(value) // value is undefined | ||
isUndefined(value) | ||
``` | ||
## isWeakMap | ||
`(value: unknown) => value is WeakMap<any, unknown>` | ||
```typescript | ||
isWeakMap(value) // value is WeakMap<any, unknown> | ||
isWeakMap(value) | ||
``` | ||
## isWithLength | ||
`(value: unknown) => value is { length: number }` | ||
```typescript | ||
isWithLength(value) // value is { length: number } | ||
isWithLength(value) | ||
``` | ||
@@ -403,0 +476,0 @@ |
@@ -1,7 +0,7 @@ | ||
import { Static, Predicate } from '../types' | ||
import { Predicate } from '../types' | ||
import { isObject } from '../predicates/isObject' | ||
export const object = <T extends Predicate<any>>(predicate: T) => ( | ||
export const object = <T>(predicate: Predicate<T>) => ( | ||
value: any, | ||
): value is Record<any, Static<T>> => { | ||
): value is Record<any, T> => { | ||
const isObjectValue = isObject(value) | ||
@@ -8,0 +8,0 @@ if (isObjectValue) { |
@@ -1,22 +0,21 @@ | ||
import { Predicate, Static } from '../types' | ||
import { Predicate } from '../types' | ||
import { isObject } from '../predicates' | ||
export const record = <K extends Predicate<string | symbol>, T extends Predicate<any>>( | ||
keyPredicate: K, | ||
valuePredicate: T, | ||
) => { | ||
return (value: unknown): value is Record<Static<K>, Static<T>> => { | ||
const isObjectValue = isObject(value) | ||
if (isObjectValue) { | ||
for (const key in value as object) { | ||
if ( | ||
Object.hasOwnProperty.call(value, key) && | ||
(!keyPredicate(key) || !valuePredicate((value as any)[key])) | ||
) { | ||
return false | ||
} | ||
export const record = <K extends PropertyKey, T>( | ||
keyPredicate: Predicate<K>, | ||
valuePredicate: Predicate<T>, | ||
) => (value: unknown): value is Record<K, T> => { | ||
const isObjectValue = isObject(value) | ||
if (isObjectValue) { | ||
for (const key in value as object) { | ||
if ( | ||
Object.hasOwnProperty.call(value, key) && | ||
(!keyPredicate(key) || !valuePredicate((value as any)[key])) | ||
) { | ||
return false | ||
} | ||
} | ||
return isObjectValue | ||
} | ||
return isObjectValue | ||
} | ||
import { Predicate } from '../../index' | ||
export const isArray: Predicate<Array<unknown>> = Array.isArray | ||
export const isArray: Predicate<Array<any>> = Array.isArray |
@@ -1,5 +0,4 @@ | ||
import { Predicate } from '../types' | ||
import { object } from '../generics/object' | ||
import { isString } from './isString' | ||
export const isDictionary: Predicate<Record<any, string>> = object(isString) | ||
export const isDictionary = object(isString) |
@@ -1,6 +0,6 @@ | ||
import { Predicate, ObjectLike } from '../types' | ||
import { ObjectLike } from '../types' | ||
import { isBoolean } from './isBoolean' | ||
import { isSome } from './isSome' | ||
export const isObjectLike: Predicate<ObjectLike> = (value: unknown): value is ObjectLike => | ||
export const isObjectLike = (value: unknown): value is ObjectLike => | ||
isSome(value) && !isBoolean(value) |
@@ -1,3 +0,1 @@ | ||
type ObjectWith<K extends PropertyKey> = { [P in K]: unknown } | ||
export type InferredPartial<T extends {}> = { | ||
@@ -21,3 +19,3 @@ [P in { | ||
export type Constructor<T extends object, U extends Array<any> = []> = { | ||
new(...rest: U): T | ||
new (...rest: U): T | ||
} | ||
@@ -31,3 +29,6 @@ | ||
export type None = null | undefined | ||
export type Some<T = Function | boolean | bigint | number | string | symbol | object> = Exclude<T, None> | ||
export type Some<T = Function | boolean | bigint | number | string | symbol | object> = Exclude< | ||
T, | ||
None | ||
> | ||
export type Maybe<T> = T | None | ||
@@ -37,9 +38,11 @@ export type Optional<T> = T | undefined | ||
export type ObjectLike = ObjectWith<PropertyKey> | ||
export type ObjectLike = { | ||
[P in PropertyKey]: unknown | ||
} | ||
export type Primitive = null | undefined | boolean | number | string | symbol | bigint | ||
export type Primitive = SerializablePrimitive | undefined | symbol | bigint | ||
export type SerializableArray = Array<Serializable> | ||
export type SerializableArray = Array<Serializable> | ReadonlyArray<Serializable> | ||
export type SerializablePrimitive = null | boolean | number | string | ||
export type SerializableObject = Partial<{ [key: string]: Serializable }> | ||
export type SerializableObject = { [key: string]: Serializable | undefined } | ||
export type Serializable = SerializablePrimitive | SerializableObject | SerializableArray |
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
159879
569
3062