Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

isntnt

Package Overview
Dependencies
Maintainers
6
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

isntnt

A collection of composable JavaScript runtime type predicates with TypeScript type guard declarations

  • 2.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
762
decreased by-26.23%
Maintainers
6
Weekly downloads
 
Created
Source

Isntnt is a collection of composable JavaScript runtime type predicates with TypeScript type guard declarations. Supports generics including union and intersection types.

Features

  • Predicates
  • Predicate Factories
  • Types

Predicates

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.

hasLength

Returns true when you pass an object with a length property that is a valid length.

hasLength(new ArrayBuffer()) // true
hasLength({}) // false

isAny

Always returns true.

isAny(null) // true
isAny(12) // true
// always true

isArray

Returns true when you pass an array.

isArray([]) // true
isArray('foo') // false

isArrayLike

Returns true when you pass a object that is not a function with a length property that is a valid length.

isArrayLike({ length: 12 }) // true
isArrayLike(new Date()) // false

isBigInt

Returns true when you pass a bigint.

isBigInt(116n) // true
isBigInt(12) // false

isBoolean

Returns true when you pass a boolean.

isBoolean(true) // true
isBoolean(null) // false

isDate

Returns true when you pass a Date object.

isDate(new Date()) // true
isDate(Date.now()) // false

isDictionary

Returns true when you pass an object of which each property name, and each property value are a string.

isDictionary({ a: 'foo' })
isDictionary({ b: 12 })

isFalse

Returns true when you pass false.

isFalse(false) // true
isFalse(true) // false

isFunction

Returns true when you pass a function.

isFunction(isFunction) // true
isFunction({}) // true

isInt

Returns true when you pass an integer (a whole number).

isInt(1) // true
isInt(1.5) // false

isInt8

Returns true when you pass an integer ranging from -128 to 127.

isInt8(127) // true
isInt8(128) // false

isInt16

Returns true when you pass an integer ranging from -32,768 to 32,767.

isInt16(12) // true
isInt16(-50_000) // false

isInt32

Returns true when you pass an integer ranging from -2,147,483,648 to 2,147,483,647.

isInt32(12) // true
isInt32(Number.MAX_SAFE_INTEGER) // false

isLength

Alias to isUint32.

isMap

Returns true when you pas a Map object.

isMap(new Map()) // true
isMap({}) // false

isNegative

Returns true when you pas a negative number (including -0).

isNegative(-1) // true
isNegative(1) // false

isNever

Always returns false.

isNever(null) // false
isNever(true) // false
// always returns false

isNone

Returns true when you pass null or undefined.

isNone(null) // true
isNone(128) // false

isNull

Returns true when you pass null.

isNull(null) // true
isNull(undefined) // false

isNumber

Returns true when you pass a number that is not NaN.

isNumber(12) // true
isNumber(NaN) // false

isObject

Returns true when you pass an object that is not null.

isObject({}) // true
isObject(null) // false

isObjectLike

Returns true when you pass an value that is not a boolean, null, or undefined.

isObjectLike([]) // true
isObjectLike(true) // false

isPlainObject

Returns true when you pass an object that was constructed by Object.

isPlainObject({}) // true
isPlainObject(new Date()) // false

isPositive

Returns true when you pass a positive number.

isPositive(1) // true
isPositive(-1) // false

isPrimitive

Returns true when you pass null, undefined, a boolean, bigint, number, symbol, or string.

isPrimitive(1) // true
isPrimitive([]) // false

isRegExp

Returns true when you pass a RegExp object.

isRegExp(/foo/) // true
isRegExp([]) // false

isSerializable

Returns true when you pass a serializable primitive, a serializable array, or serializable object.

isSerializable(12) // true
isSerializable(console.log) // false

isSerializableArray

Returns true when you pass an array where every element is serializable.

isSerializableArray(['foo', 1]) // true
isSerializableArray([new Date()]) // false

isSerializableNumber

Returns true when you pass a number that is not NaN, Infinity, or -Infinity.

isSerializableNumber(1) // true
isSerializableNumber(Infinity) // false

isSerializableObject

Returns true when you pass an object where every property is serializable.

isSerializableObject({ foo: 1 }) // true
isSerializableObject({ bar: Infinity }) // false

isSerializablePrimitive

Returns true when you pass null, a serializable number, a boolean, bigint, symbol, or string.

isSerializablePrimitive(null) // true
isSerializablePrimitive(NaN) // false

isSet

Returns true when you pas a Set object.

isSet(new Set()) // true
isSet([]) // false

isSome

Returns true when you pas a value other than null or undefined.

isSome('foo') // true
isSome(null) // false

isString

Returns true when you pas a string.

isString('bar') // true
isString(48) // false

isSymbol

Returns true when you pas a symbol.

isSymbol(Symbol()) // true
isSymbol('baz') // false

isTrue

Returns true when you pas true.

isTrue(true) // true
isTrue(false) // false

isUint

Returns true when you pass a positive integer.

isUint(112) // true
isUint(-16) // false

isUint8

Returns true when you pass an unsigned integer ranging from 0 to 255.

isUint8(255) // true
isUint8(1.2) // false

isUint16

Returns true when you pass an unsigned integer ranging from 0 to 65,535.

isUint16(2048) // true
isUint16(-12) // false

isUint32

Returns true when you pass an unsigned integer ranging from 0 to 4,294,967,295.

isUint32(256) // true
isUint(Number.MAX_SAFE_INTEGER) // false

isUndefined

Returns true when you pass undefined.

isUndefined(undefined) // true
isUndefined(null) // false

isWeakMap

Returns true when you pass a WeakMap object.

isWeakMap(new WeakMap()) // true
isWeakMap(new Map()) // false

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 exceeds the floor value it’s given. This is an analogue to the > operator.

const isAboveZero = above(0)

isAboveZero(-1) // false
isAboveZero(12) // true

The isAboveZero predicate above returns true when you pass a number 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.

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 that is below 18.

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.

const isNumberArray = array(isNumber)

The isNumberArray predicate above returns true when you pass an array of which every element is a number.

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.

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 is below the ceiling value it’s given. This is an analogue to the < operator.

const isBelow100 = below(100)

isBelow100(124) // false
isBelow100(32) // true

The isBelow100 predicate above returns true when you pass a number that is less than 100.

either

Create a predicate based on several literal values, it returns true if a value matches either of the primitive values it’s given.

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.

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.

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 value, it returns true if a value equals the primitive value it’s given.

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 is below or equal to the max. value it’s given. This is an analogue to the <= operator.

const isMax100 = max(100)

isMax100(100) // true
isMax100(101) // false

The isMax100 predicate above returns true when you pass a number 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.

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 is above or equal to the min. value it’s given. This is an analogue to the >= operator.

const isMin100 = min(100)

isMin100(100) // true
isMin100(99) // false

The isMin0 predicate above returns true when you pass a number that is above or equal to 100.

noneable

Aliases maybe.

nullable

Create a predicate based on another predicate, it checks if a value matches it’s given predicate, or equals null.

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.

const isEnumerable = object(isUint)

The isEnumerable predicate above returns true when you pass an object of which every element is an unsigned integer.

optional

<T>(predicate: Predicate<T>) => (value: unknown) => value is T | undefined

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.

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.

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.

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.

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.

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.

test

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.

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

Intersect

type User = Intersect<{ name: string } | { email: string }> // { name: string } & { email: string }

Maybe

Unions a type with null and undefined.

type MaybeString = Maybe<string> // string | null | undefined

None

Union of null and undefined.

type X = None // null | undefined

Nullable

Unions a type with null.

type NullableString = Nullable<string> // string | null

Optional

Unions a type with undefined.

type OptionalString = Optional<string> // string | undefined

Predicate

Describes a predicate function signature

type IsString = Predicate<string> // <U>(value: U): value is Extract<U, string>

Primitive

Union of all primitive types.

type X = Primitive // null | undefined | boolean | number | string | symbol | bigint

Serializable

type X = Serializable // SerializableArray | SerializableObject | SerializableObject

SerializableArray

type X = SerializableArray // Array<Serializable> | ReadonlyArray<Serializable>

SerializableObject

type X = SerializableObject // { [key: string]: Serializable | undefined }

SerializablePrimitive

type X = SerializablePrimitive // null | boolean | number | string

Some

Excludes null and undefined from a type, type parameter is optional.

type X = Some // Function | boolean | bigint | number | string | symbol | object
type Y = Some<string | null> // string

Static

Extracts type parameter from Predicate type. This is useful to extract TypeScript types from (complex) predicates.

type X = Static<Predicate<string>>> // string

FAQs

Package last updated on 25 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc