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

strong-typed

v0.4.0

Published

A runtime type-checking library for javascript

Downloads

25

Readme

Test install size License: MIT

strong-typed

strong-typed is a lightweight and easy-to-use library that can help you catch type errors at runtime and improve the reliability of your code. It is compatible with modern JavaScript features, supports a wide range of types, and provides clear error messages.

Installation

npm i strong-typed

Usage

Strong-typed takes two arguments:

| Parameter | Type | Description | |-----------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Types | array | A javascript array of supported types (supported types are any, date, integer, boolean, decimal, string, object, function, array, set, map, null, undefined). The number of elements in the array must match the parameter number of function that's passed in. | | Fn | function | The javascript function to be type checked |

Examples

import strongTyped, {Types} from 'strong-typed';

const myFunc = st([Types.STRING], (a) => { console.log(a) });

myFunc();    // Error: Function expects 1 arguments, instead only 0 parameter(s) passed in.
myFunc(1);   // Error: Expected argument 1 to be of type 'string'
myFunc("1"); // Will print 1 in the console
import strongTyped, {Types} from 'strong-typed';

const myFunc = strongTyped([Types.STRING, Types.INTEGER], (a, b) => { console.log(a + b) });

myFunc("1", 2);  // Will print 3 in the console
myFunc("1", "2");  // Error: Expected argument 2 to be of type 'integer'
import strongTyped, {Types} from 'strong-typed';

class MyClass{
    constructor(a,b){
        this.a = a;
        this.b = b;
    }
}
const myFunc = strongTyped([MyClass, Types.INTEGER], (a, b) => { console.log(a.a + b) });

myFunc(new MyClass(1,2), 2);  // Will print 3 in the console
myFunc({a:1, b:2}, 2);  // Error: Expected argument 1 to be of type 'MyClass'

Browser Usage

To use strong-typed in a browser environment, you can include the library in a script tag on your HTML page. Make sure to include the library after you include any dependencies:

<script src="path/to/strong-typed.js"></script>
<script>
  const myFunc = strongTyped([Types.STRING], (a) => { console.log(a) });
  myFunc("1"); // Will print 1 in the console
</script>

Supported Types

The following types are supported by the library:

  • any: any type of value is accepted
  • date: an instance of the JavaScript Date object
  • integer: a JavaScript number that is an integer
  • bigint: a JavaScript bigint
  • boolean: a JavaScript boolean value
  • decimal: a JavaScript number that is not an integer
  • string: a JavaScript string
  • object: a JavaScript object, including null
  • function: a JavaScript function
  • array: a JavaScript array
  • set: a JavaScript Set object
  • map: a JavaScript Map object
  • null: a JavaScript null value
  • undefined: a JavaScript undefined value

When defining the types for your function, make sure the number of elements in the Types array matches the number of arguments in your function. If a type is not recognized, the library will throw an error and the function will not execute.

Error handling

When an error occurs, the library will throw an error with a descriptive message indicating which argument caused the error and what type it was expected to be. For example, if the first argument passed to a function was expected to be a string but was passed as a number, the error message would be: Expected argument 1 to be of type 'string'. Please note that if the function passed to the library is an anonymous function, it will not show any name in the error message.