npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@konfirm/guard

v2.0.2

Published

Building blocks for type guards

Downloads

3

Readme

tests release

Guard

Guards - or input validation - is a concept in Typescript which allows you to easily ensure specific types. Besides being a great assistance when authoring your code in Typescript, they have the added benefit of being preserved during the transpilation to Javascript, which means your validations will become part of the 'compiled' Javascript. This solves an important issue with Typescript based projects at runtime, as the integrity of the data will still be guaranteed by your Guards.

For pure Javascript developers this is good news, as these Guards easily let you validate your variables too.

Installation

npm install --save @konfirm/guard

Or use your favorite package manager to install @konfirm/guard

API

Exports

| name | type | | ------------------------------------------- | ---------- | | all | function | | any | function | | assert | function | | assertion | function | | is | function | | isArray | function | | isArrayOfSize | function | | isArrayOfType | function | | isBigInt | function | | isBoolean | function | | isFloat | function | | isFunction | function | | isGreater | function | | isGreaterOrEqual | function | | isInstanceOf | function | | isInteger | function | | isKey | function | | isKeyOfType | function | | isLess | function | | isLessOrEqual | function | | isNULL | function | | isNegative | function | | isNumber | function | | isObject | function | | isOptionalKeyOfType | function | | isPositive | function | | isStrictStructure | function | | isString | function | | isStringWithPattern | function | | isStructure | function | | isSymbol | function | | isUndefined | function | | not | function | | Float | Type alias | | Guard | Type alias | | Integer | Type alias | | Negative | Type alias | | Positive | Type alias | | StringWithPattern | Type alias | | Validator | Type alias |

all

all<T>(...checks): Guard<T>

Create a guard verifying the given value matches all of the validators

Parameters

| Name | Type | | :---------- | :---------------------------------------------------------- | | ...checks | [Validator, ...<Validator>] |

Returns

Guard<T>

any

any<T>(...checks): Guard<T>

Create a guard verifying the given value matches any of the validators

Parameters

| Name | Type | | :---------- | :---------------------------------------------------------- | | ...checks | [Validator, ...<Validator>] |

Returns

Guard<T>

assert

assert<T>(value, message, ...rules): value is T

Guard asserting the given value to match the conditions or throw an AssertionError otherwise

Parameters

| Name | Type | | :--------- | :---------------------------------------------------------- | | value | any | | message | string | | ...rules | [Validator, ...<Validator>] |

Returns

value is T

assertion

assertion<T>(message, ...rules): Guard<T>

Create an assertion guard, throwing an AssertionError with the provided message if any validator fails

Parameters

| Name | Type | | :--------- | :---------------------------------------------------------- | | message | string | | ...rules | [Validator, ...<Validator>] |

Returns

Guard<T>

is

is<T>(type): Guard<T>

Create a guard verifying the given value matches the specified type

Parameters

| Name | Type | | :----- | :--------------------------------------------------------------------------------------------------------------------------------- | | type | "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" |

Returns

Guard<T>

isArray

Const isArray(value): value is unknown[]

Guard verifying the value is a array

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is unknown[]

isArrayOfSize

isArrayOfSize<T>(min, max?): Guard<T>

Guard verifying the value to be an array with a length between the given boundaries

Parameters

| Name | Type | Default value | | :---- | :------- | :------------ | | min | number | undefined | | max | number | Infinity |

Returns

Guard<T>

isArrayOfType

isArrayOfType<T>(...validators): Guard<T>

Guard verifying the value to be an array containing only elements matching the validators

Parameters

| Name | Type | | :-------------- | :---------------------------------------------------------- | | ...validators | [Validator, ...<Validator>] |

Returns

Guard<T>

isBigInt

Const isBigInt(value): value is BigInt

Guard verifying the value is a bigint

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is BigInt

isBoolean

Const isBoolean(value): value is boolean

Guard verifying the value is a boolean

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is boolean

isFloat

isFloat<T>(value): value is T

Guard verifying the value to be a float number

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is T

isFunction

Const isFunction(value): value is Function

Guard verifying the value is a function

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is Function

isGreater

isGreater<T>(than): Guard<T>

Create a guard matching numbers greater than the provided value

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :----- | :------- | | than | number |

Returns

Guard<T>

isGreaterOrEqual

isGreaterOrEqual<T>(than): Guard<T>

Create a guard matching numbers greater than or equal to the provided value

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :----- | :------- | | than | number |

Returns

Guard<T>

isInstanceOf

isInstanceOf(type): Guard<typeof type>

Creates a guard verifying the value is an object and an instance of the given class

Parameters

| Name | Type | | :----- | :-------------------------------- | | type | (...args: any[]) => unknown |

Returns

Guard<typeof type>

isInteger

isInteger<T>(value): value is T

Guard verifying the value to be an integer number

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is T

isKey

isKey<T>(key): Guard<T>

Creates a guard verifying the value is an object and has the given key

Parameters

| Name | Type | | :---- | :---- | | key | Key |

Returns

Guard<T>

isKeyOfType

isKeyOfType<T>(key, ...validators): Guard<T>

Creates a guard verifying the value is an object and has the given key matching the validators

Parameters

| Name | Type | | :-------------- | :-------------------------- | | key | Key | | ...validators | Validator[] |

Returns

Guard<T>

isLess

isLess<T>(than): Guard<T>

Create a guard matching numbers less than the provided value

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :----- | :------- | | than | number |

Returns

Guard<T>

isLessOrEqual

isLessOrEqual<T>(than): Guard<T>

Create a guard matching numbers less than or equal to the provided value

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :----- | :------- | | than | number |

Returns

Guard<T>

isNULL

Const isNULL(value): value is null

Guard verifying the value is a null

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is null

isNegative

isNegative<T>(value): value is T

Guard verifying the value to be a negative number

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is T

isNumber

Const isNumber(value): value is number

Guard verifying the value is a number

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is number

isObject

Const isObject(value): value is unknown

Guard verifying the value is a object

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is unknown

isOptionalKeyOfType

isOptionalKeyOfType<T>(key, ...validators): Guard<T>

Creates a guard verifying the value is an object and doesn't have the key or has the given key matching the validators

Parameters

| Name | Type | | :-------------- | :-------------------------- | | key | Key | | ...validators | Validator[] |

Returns

Guard<T>

isPositive

isPositive<T>(value): value is T

Guard verifying the value to be a positive number

Type parameters

| Name | Type | | :--- | :------- | | T | number |

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is T

isStrictStructure

isStrictStructure<T>(struct, ...options): Guard<T>

Creates a guard verifying the value is an object matching exactly the structure

Parameters

| Name | Type | | :----------- | :------------------- | | struct | Object | | ...options | (Key | Key[])[] |

Returns

Guard<T>

isString

Const isString(value): value is string

Guard verifying the value is a string

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is string

isStringWithPattern

isStringWithPattern<T>(pattern): Guard<T>

Guard verifying the value to be a string which matches the given pattern

Type parameters

| Name | Type | | :--- | :------- | | T | string |

Parameters

| Name | Type | | :-------- | :------- | | pattern | RegExp |

Returns

Guard<T>

isStructure

isStructure<T>(struct, ...options): Guard<T>

Creates a guard verifying the value is an object matching at least the structure

Parameters

| Name | Type | | :----------- | :----------------------------------- | | struct | Struct<Validator> | | ...options | (Key | Key[])[] |

Returns

Guard<T>

isSymbol

Const isSymbol(value): value is Symbol

Guard verifying the value is a symbol

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is Symbol

isUndefined

Const isUndefined(value): value is undefined

Guard verifying the value is a undefined

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is undefined

not

not<T>(...checks): Guard<T>

Create a guard verifying the given value matches none of the validators

Parameters

| Name | Type | | :---------- | :---------------------------------------------------------- | | ...checks | [Validator, ...<Validator>] |

Returns

Guard<T>

Type aliases

Float

Ƭ Float: number

Guard

Ƭ Guard<T>: (value: any) => value is T

Type declaration

▸ (value): value is T

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

value is T

Integer

Ƭ Integer: number

Negative

Ƭ Negative: number

Positive

Ƭ Positive: number

StringWithPattern

Ƭ StringWithPattern: string

Validator

Ƭ Validator: (value: any) => boolean

Type declaration

▸ (value): boolean

Parameters

| Name | Type | | :------ | :---- | | value | any |

Returns

boolean

License

MIT License Copyright (c) 2021-2023 Rogier Spieker (Konfirm)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.