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

typadyne

v0.2.0

Published

Dynamic typed JS runtime concept for NodeJS apps

Downloads

9

Readme

Typadyne

Dynamic typed Javascript runtime concept:

  • Check types during runtime
  • Extremely flexible types that are programmable
  • No transpiling, no weird languages, just normal Javascript
  • Supports function parameters and return types

Install

npm i typadyne

Usage

Start NodeJS with typadyne pre-required globally:

# If path is set up for global packages
node -r typadyne

# Try this to set path manually
node -r `npm root -g`/typadyne

Expose the typadyne fun-function manually:

require('typadyne')

Types

Currently, since this is just a concept, the only built in types are string and number. Add your own types in the global.types object. Types are just functions that receive the value and returns true if the value is the correct type:

Example for number:

// Example type definition
global.types.number = function (val) {
  return typeof val == 'number'
}

This makes it possible to add any kind of type, even very complex ones using JSON schema or d8a validations.

Here's an example of an email type:

global.types.email = function (val) {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val)
}

The types can even share code, and switch based on data during runtime. Flexible, isn't it?

Typed Javascript

After require('typadyne') the fun-function is available in the gobal scope. It's just a wrapper for a normal function that sets up type checking for you.

It checks:

  • correct number of arguments
  • that all types are valid (defined)
  • correct type for each function argument
  • valid return type

An error is thrown when one of these checks kicks in.

// Useless, but works without parameters or return types
var hello = fun(() => { return 'hello' })
hello() // => 'hello'

// Without return type (if you don't care)
var pow = fun('number', (n) => {
  return n * n
})
pow(2) // => 4

// Normal function with typed parameters which returns a string
var name = fun(
  'string',
  'string',
  (firstname, lastname) => {
    return firstname + ' ' + lastname
  },
  'string'
)
name('Vidar', 'Eldøy') // => 'Vidar Eldøy'

// Use await with async function, returns a custom type 'email'
var getEmailAddress = fun(
  'string',
  'domain',
  async (name, domain) => {
    return `${name}@${domain}`
  },
  'email'
)
await getEmailAddres('vidar', 'eldoy.com') // => '[email protected]

Notes

You could possibly also create an "error free" Javascript alternative like Golang has:

f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with f

becomes this with Javascript:

let [f, err] = await fun(() => {})
if (err != nil)
  return console.log(err)
}
// do something with f

Could this be extended to include predefined error messages in ./errors/something.err.js?

WTFPL licensed. Enjoy!