New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
624
increased by23.32%
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.

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.

isWithLength(value)

isAny

Always returns true.

isAny(value)

isArray

Returns true when you pass an array.

isArray(value)

isArrayLike

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

isArrayLike(value)

isBigInt

Returns true when you pass a bigint.

isBigInt(value)

isBoolean

Returns true when you pass a boolean.

isBoolean(value)

isDate

Returns true when you pass a Date object.

isDate(value)

isDictionary

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

isDictionary(value)

isFalse

Returns true when you pass false.

isFalse(value)

isFunction

Returns true when you pass a function.

isFunction(value)

isInt

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

isInt(value)

isInt8

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

isInt8(value)

isInt16

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

isInt16(value)

isInt32

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

isInt32(value)

isLength

Alias to isUint32.

isLength(value)

isMap

Returns true when you pas a Map object.

isMap(value)

isNegative

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

isNegative(value)

isNever

Always returns false.

isNever(value)

isNone

Returns true when you pass null or undefined.

isNone(value)

isNull

Returns true when you pass null.

isNull(value)

isNumber

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

isNumber(value)

isObject

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

isObject(value)

isObjectLike

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

isObjectLike(value)

isPlainObject

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

isPlainObject(value)

isPositive

Returns true when you pass a positive number.

isPositive(value)

isPrimitive

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

isPrimitive(value)

isRegExp

Returns true when you pass a RegExp object.

isRegExp(value)

isSerializable

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

isSerializable(value)

isSerializableArray

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

isSerializableArray(value)

isSerializableNumber

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

isSerializableNumber(value)

isSerializableObject

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

isSerializableObject(value)

isSerializablePrimitive

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

isSerializablePrimitive(value)

isSet

Returns true when you pas a Set object.

isSet(value)

isSome

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

isSome(value)

isString

Returns true when you pas a string.

isString(value)

isSymbol

Returns true when you pas a symbol.

isSymbol(value)

isTrue

Returns true when you pas true.

isTrue(value)

isUint

Returns true when you pass a positive integer.

isUint(value)

isUint8

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

isUint8(value)

isUint16

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

isUint16(value)

isUint32

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

isUint32(value)

isUndefined

Returns true when you pass undefined.

isUndefined(value)

isWeakMap

Returns true when you pass a WeakMap object.

isWeakMap(value)

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 }>

equals

type User = {
  name: string
} & {
  email: string
}

Maybe

Unions a type with null and undefined.

type MaybeString = Maybe<string>

equals

type MaybeString = string | null | undefined

None

Union of null and undefined.

type NoneType = None

equals

type NoneType = null | undefined

Nullable

Unions a type with null.

type NullableString = Nullable<string>

equals

type NullableString = string | null

Optional

Unions a type with undefined.

type OptionalString = Optional<string>

equals

type OptionalString = string | undefined

Predicate

Describes a predicate function signature

type IsString = Predicate<string>

equals

type IsString = <T>(value: T) => value is Extract<T, string>

Primitive

Union of all primitive types.

type PrimitiveType = Primitive

equals

type PrimitiveType = null | undefined | boolean | number | string | symbol | bigint

Serializable

type SerializableType = Serializable

equals

type SerializableType = SerializableArray | SerializableObject | SerializableObject

SerializableArray

type SerializableArrayType = SerializableArray

equals

type SerializableArrayType = Array<Serializable> | ReadonlyArray<Serializable>

SerializableObject

type SerializableObjectType = SerializableObject

equals

type SerializableObjectType = { [key: string]: Serializable | undefined }

SerializablePrimitive

type SerializablePrimitiveType = SerializablePrimitive

equals

type SerializablePrimitiveType = null | boolean | number | string

Some

Excludes null and undefined from type.

type SomeType = Some
type SomeString = Some<string | null>

equals

type SomeType = Function | boolean | bigint | number | string | symbol | object
type SomeString = string

Static

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

type PredicateType = Static<Predicate<string>>>

equals

type PredicateType = string

FAQs

Package last updated on 24 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