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

@shadow578/type-guardian

v1.1.0

Published

Quickly write Type-Guard functions for checking objects

Downloads

5

Readme

type-guardian

functional, composable type guards.

Installation

Install type-guardian using

npm install type-guardian

Usage

Function match

import { match } from 'type-guardian'

The match function creates a simple type guard. The function takes either a TypeName or another TypeGuard and returns a new guard for that type:

const isString = match<string>('string');

isString(''); // returns true
isString('i am a string'); // returns true
isString(0); // returns false
isString(null); // returns false
isString(undefined); // returns false

Function matchNullable

import { matchNullable } from 'type-guardian'

The matchNullable function creates a type guard for a nullable type. A nullable type accepts every value of the target type, null and undefined. The function takes either a TypeName or another TypeGuard and returns a new guard for that type:

const isNullableString = matchNullable<string>('string');

isNullableString(''); // returns true
isNullableString('i am a string'); // returns true
isNullableString(null); // returns true
isNullableString(undefined); // returns true
isNullableString(0); // returns false

Function matchArray

import { matchArray } from 'type-guardian'

The matchArray function creates a type guard for a array of a given type. matchArray can be configured to either accept or reject empty arrays using the emptyBehaviour parameter. Default behaviour is to accept empty arrays (accept-empty). The function takes either a TypeName or another TypeGuard and returns a new guard for that type:

const isStringArray = matchArray<string>('string'); // equivalent to matchArray('string', 'accept-empty')

isStringArray(['a string']); // returns true
isStringArray(['a string', 0]); // returns false
isStringArray([]); // returns true
isStringArray(null); // returns false

Or, with emptyBehaviour set to reject-empty:

const isStringArray = matchArray<string>('string', 'reject-empty');

isStringArray(['a string']); // returns true
isStringArray(['a string', 0]); // returns false
isStringArray([]); // returns false
isStringArray(null); // returns false

Function matchUnion

import { matchUnion } from 'type-guardian'

The matchUnion function creates a type guard for a union type. The function takes a list of either a TypeName or another TypeGuard and returns a new guard for the corrosponding union type:

const isStringOrBoolean = matchUnion<string|boolean>('string', 'boolean');

isStringOrBoolean('i am a string'); // returns true
isStringOrBoolean(true); // returns true
isStringOrBoolean(null); // returns false
isStringOrBoolean(undefined); // returns false
isStringOrBoolean(0); // returns false

Function matchEnumeration

import { matchEnumeration } from 'type-guardian'

The matchEnumeration function creates a type guard for a enumeration. The function takes a list of either a TypeName or another TypeGuard and returns a new guard for the corrosponding union type:

const isMyEnum = matchEnumeration<'yes'|'no'|'maybe'>('yes', 'no', 'maybe');

isMyEnum('yes'); // returns true
isMyEnum('no'); // returns true
isMyEnum('maybe'); // returns true
isMyEnum('something else'); // returns false
isMyEnum(''); // returns false

Function matchObject

import { matchObject } from 'type-guardian'

The matchObject function creates a type guard for a enumeration. The function takes a list of either a TypeName or another TypeGuard and returns a new guard for the corrosponding union type:

const isMyObject = matchObject<MyObject>({
    'name': 'string',
    'age': 'number'
});

isMyObject({
    name: 'Peter',
    age: 23
}); // returns true
isMyObject({
    name: 'Peter'
}); // returns false
isMyObject({}); // returns false
isMyObject(null); // returns false

For a more complex example, see ComplexObject.test.ts.

TypeName

The TypeName type is a enumeration of all basic types available in JavaScript (the same as you can get from typeof). Additionally, it contains a special value 'any' which matches anything.

TypeGuard

A TypeGuard is a function that checks if a input matches a pre-defined type. Type guards can be nested to define more complex types or even objects.

Builtin Type Guards

type-guardian comes with type guards for commonly used types.

import { isString } from "type-guardian/TypeGuards"

License

Copyright 2023 shadow578

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, > software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or > implied. See the License for the specific language governing permissions > and limitations under the License.