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

@elijahjcobb/types

v0.0.14

Published

A runtime type checker built with and for TypeScript.

Downloads

26

Readme

Types

A runtime type checker built with and for TypeScript.

Example

try {

	let validator: ECTValidator = new ECTValidator({
		arr: ECTItem.array([ECTItem.string()]),
		str: ECTItem.string(),
		boo: ECTItem.boolean(),
		num: ECTItem.number(),
		obj: ECTItem.object({
			str: ECTItem.string(),
			num: ECTItem.number()
		}),
		allObj: ECTItem.object({
			"*": ECTItem.string()
		})
	});

	validator.verify({
		arr: [
			"Hello",
			12
		],
		str: 2309,
		boo: "fq23",
		num: false,
		obj: {
			str: 12,
			num: "f2elj"
		},
		allObj: {
			foo: 89,
			bar: 87123
		}
	});

} catch (e) {

	e.print();

}


/*

Value '12' for key 'arr' is incorrect type (expected: 'array<string>' actual: 'array<number>').
Value '2309' for key 'str' is incorrect type (expected: 'string' actual: 'number').
Value 'fq23' for key 'boo' is incorrect type (expected: 'boolean' actual: 'string').
Value 'false' for key 'num' is incorrect type (expected: 'number' actual: 'boolean').
Value '12' for key 'str' in object 'obj' is incorrect type (expected: 'string' actual: 'number').
Value 'f2elj' for key 'num' in object 'obj' is incorrect type (expected: 'number' actual: 'string').
Value '89' for key 'foo' in object 'allObj' is incorrect type (expected: 'string' actual: 'number').

*/

Import

import {
	ECTValidator,
	ECTItem,
	ECTOutput 
} from "@elijahjcobb/types";

Defining A Structure

import { ECTValidator, ECTItem } from "@elijahjcobb/types";

let validator: ECTValidator = new ECTValidator({
	firstName: ECTItem.string(true), // allows string but is optional
	lastName: ECTItem.string(), // allows string
	age: ECTItem.number(), // allows number
	agreesItIsTimeToGetIll: ECTItem.boolean(), // allows boolean
	tags: ECTItem.array(false, ECTItem.string(), ECTItem.number()), // allows string or number as values of an array and is required
	options: ECTItem.object({
	    darkMode: ECTItem.boolean(), // allows boolean
	    timeout: ECTItem.number() // allows number
	}, false) // checks each key in the object and is required
});

Validating Object with Structure

import { ECTValidator, ECTItem, ECTOutput } from "@elijahjcobb/types";

// make a validator ...

let exampleValues: object = {
	firstName: "Elijah",
	lastName: "Cobb",
	birthYear: 1999,
	agreesItIsTimeToGetIll: true,
	tags: [ 1, "two"],
	options: { darkMode: true, timeout: 123 }
};

let output: ECTOutput = validator.inspect(exampleValues);

Get Only Failures

let output: ECTOutput = validator.getFailures(exampleValues);

Check if it Fails

let res: boolean = validator.doesFail(exampleValues);

Output Structure

The ECTOutput will return an object with the same keys as the one you give it. However, instead of the value you provided, it will be an object with the attributes in the example below. On an object type it will respond each key with the format below.

{
    firstName: {
	    expected: string,
	    data: any,
	    index?: number,
	    key?: string,
	    actual: string,
	    passed: boolean
    }
}

Documentation

Everything is completely documented. You can view the declaration files or even the source code on GitHub.

Bugs

If you find any bugs please create an issue on GitHub or if you are old fashioned email me at [email protected].