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

type-guard-utils

v0.1.2

Published

Decorators, rxjs operators, and utility functions for doing runtime checks on returned types against type guard.

Downloads

3

Readme

Type Guard Utils -- WIP -- Use at your own risk


Rxjs Operators


Typescript Method Decorators


Utility functions/factories


Detailed function descriptions


typeGuardCheckOperator

An an operator to use with rxjs observables. Could be used to check the type on any state of an observable.

The best use case for this would be on the edges of your application such as request to external APIs. This would help with both verifying that the you defined the correct types for the returned values. This would also help to catch if the data type returned from an API changes.

Kind: global variable

| Param | Description | | --- | --- | | typeChecker | A function that acts as a type guard. Accepts the item in and returns a boolean for if its valid | | expectArray | Apply the typeChecker to an array of items instead of individual item. | | invalidTypeCallback | Callback for if typeChecker(item) returns false | | validTypeCallback | Callback for if typeChecker(item) returns true |

typeGuardMapOperator

Same as typeGuardCheckOperator expect that the observable gets emitted to the return value from either the invalidTypeCallback or the validTypeCallback. If either callback is omitted the stream is re-emitted when that callback is called.

Kind: global variable

checkWithTypeGuard(typeChecker, expectArray, invalidTypeCallback, validTypeCallback, mapInsteadOfCheck)

A method decorator to check return type.

For Observables: Wraps the typeGuardMapOperator.

For Promises and non-async values: Wraps internal handleTypeGuardValidation function.

Can handle () => ReturnType | <Promise> | <Observable>

Kind: global function

| Param | Default | Description | | --- | --- | --- | | typeChecker | | A function that acts as a type guard. Accepts the item in and returns a boolean for if its valid | | expectArray | false | Apply the typeChecker to an array of items instead of individual item | | invalidTypeCallback | | Callback for if typeChecker(item) returns false | | validTypeCallback | | Callback for if typeChecker(item) returns true | | mapInsteadOfCheck | false | If true the return type gets mapped to the return from either the invalidTypeCallback or validTypeCallback. Default is that the return value is unchanged. |

validatorFactory(typeChecker, expectArray, invalidTypeCallback, validTypeCallback)

The validatorFactory returns a validator function that can be used to do runtime type checks for a certain type.

The invalidTypeCallback and validTypeCallbacks null/undefined or a some value. If null/undefined the original value is returned from the validator function, if any other value is return that value is also returned by the validator.

Example: let cars = 0; const logInvalidType = (car) => { postToSomeLoggingService(car) } const incrementCarCount = () => { cars++ }

const carsValidator = validatorFactory(isCar, true, logInvalidCar, incrementCarCount)

In cases where invalidTypeCallback or validTypeCallback don't return anything we can use this to check a value and also a return value.

To check an existing value: const car = {...} carValidator(car)

To check a return value, you can just use the validator as a higher order function:

const car = carValidator(getNextCar())

Kind: global function

| Param | Description | | --- | --- | | typeChecker | A function that acts as a type guard. Accepts the item in and returns a boolean for if its valid | | expectArray | Apply the typeChecker to an array of items instead of individual item. | | invalidTypeCallback | Callback for if typeChecker(item) returns false | | validTypeCallback | Callback for if typeChecker(item) returns true |

typeGuardOperatorFactory(operator)

Mostly for internal use but exposing incase anyone wants to do anything other than the predefined operators.

Kind: global function

| Param | Description | | --- | --- | | operator | Accepts an rxjs operator |