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

letype

v1.1.0

Published

Type checker for any data structures

Downloads

12

Readme

Letype

Type checker that uses native data types.

Letype is simple and small (1kB minified + gzipped) type checker library that can validate any JS data types and structures as well as any custom ones.

Features

  • Uses native JS data types.
  • Can validate type structures.
  • Supports regex validation as a type.
  • Supports custom types.
  • Small in size.

Installation

To install the stable version:

npm install --save letype

This assumes you are using npm as your package manager.

If you're not, you can access these files on unpkg (letype.min.js is the file you're probalby after), download them, or point your package manager to them.

Browser Compatibility

Letype.js currently is compatible with all modern browsers.

Example usage

import { types, check } from 'letype';

check(1, Number); // -> true

check('1', Number); // -> false
// "Type error: `1` is not of type `number`"

check({ counter: 1 }, { counter: Number }); // -> true

check({
  id: 1,
  name: 92942,
  age: '21',
  work: null,
  anythingGoes: 'flamingo',
  date: new Date(),
  regexp: 123,
}, {
  id: Number,
  name: String,
  age: Number,
  role: String,
  anythingGoes: types.Any,
  date: Date,
  regexp: /123/,
}); // -> false
// "Type error: `92942` is not of type `string` in `name`"
// "Type error: `21` is not of type `number` in `age`"
// "Type error: `role` is undefined! Required value of type `string`"
// "Type error: `work` is defined as `null`! But it should not be defined at all!"

All available exports from package:

import {
  types,
  check,
  assert,
} from 'letype';

const {
  Any,
  Or,
  Undefined,
  Custom,
} = types;

List of available functions

Usage of functions

check()

It takes first argument as value that should be checked. Second argument is type that the value should be checked against. It returns boolean (true if valid, false if invalid).

check('John Doe', String); // -> true
check(123, String); // -> false

assert()

It does exactly the same thing as check() function, but with a little difference. If validation fails it throws error.

assert('John Doe', String); // -> true
assert(123, String); // -> Throw

List of available types

Types are meant to be used as data types that does custom validation against given value inside assert() or check() functions.

Types from letype library:

Types from JavaScript language:

Types can also be created in structures:

Usage of types

types.Any

check('string', Any); // -> true
check(123, Any);      // -> true

types.Or()

check('string', Or(String, Number)); // -> true
check(123, Or(String, Number));  // -> true
check(true, Or(String, Number)); // -> false

types.Undefined

check('string', Undefined);  // -> false
check(undefined, Undefined); // -> true

types.Custom

Custom type is empty and does no checks against anything. It is meant for creating your own custom types.

To do that just extend Custom class and define public parse method that has one parameter - "value". It is value to be checked/validated. parse method should return boolean (true if valid, false if invalid).

For example lets create type that checks if value has first capital letter.

class Capital extends types.Custom {
  parse(value) {
    return value[0] === value[0].toUpperCase();
  }
}

To use it simply pass it in any of assert() or check() functions.

check('John', Capital); // -> true
check('doe', Capital);  // -> false

String

check('123', String); // -> true
check(123, String);   // -> false

Number

check('123', Number); // -> false
check(123, Number);   // -> true

Boolean

check('false', Boolean); // -> false
check(false, Boolean);   // -> true

Array

check('array', Array); // -> false
check([], Array);      // -> true
check([1,2,3], Array); // -> true

Function

check('fn', Function);     // -> false
check(() => {}, Function); // -> true

Date

check('10-12-2020', Date); // -> false
check(new Date('10-12-2020'), Date); // -> true

RegExp

check('A-Z', RegExp); // -> false
check(/A-Z/, RegExp); // -> true

Typed Arrays

check([], [String]);    // -> false
check([1], [String]);   // -> false
check(['1'], [String]); // -> true

Typed Objects

check({}, { name: String }); // -> false
check({ name: 1 }, { name: String }); // -> false
check({ name: 'John' }, { name: String }); // -> true

Regular Expressions

check('a', /A-Z/); // -> false
check('A', /A-Z/); // -> true

Motivation

We can get awesome type checking in JS with TS, but that only checks types in compile time.

So I wanted some kind of runtime type checking with types that are already available in JS - not using strings as a types. This feels more JS and more natural.

License

MIT licenced. Copyright © 2020-present, Marcis (Marcisbee) Bergmanis