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

mol_data_all

v1.1.1306

Published

Defines static typed DTO with strict runtime validation and user friendly error messages like:

Downloads

5,804

Readme

$mol_data

Defines static typed DTO with strict runtime validation and user friendly error messages like:

["friends"][0]["phone"] undefined is not any of variants
undefined is not a string
undefined is not a number

Known parsers

Usage examples

Entities

// Base Type
const PersonDTO = $mol_data_record({
	name : $mol_data_string ,
	age : $mol_data_optional( $mol_data_integer ) ,
	birthday : $mol_data_pipe( $mol_data_string , $mol_time_moment ) ,
})

// Derived Type
const UserDTO = $mol_data_record({
	... PersonDTO.config,
	phone: $mol_data_variant( $mol_data_string , $mol_data_integer ),
	mail: $mol_data_email,
})

// Ensure this is a User
const jin = UserDTO({
	name : 'Jin' ,
	age : 33 ,
	birthday : '1984-08-04T12:00:00Z' ,
	phone : 791234567890,
	mail : '[email protected]' ,
})

// typeof jin === {
// 	readonly name: string;
// 	readonly age?: number | undefined;
// 	readonly birthday: $mol_time_moment;
// 	readonly phone: string | number;
// 	readonly mail: string;
// }

// Allow only Users
function printName( user : typeof UserDTO.Value ) {
	console.log( user.name )
}

printName( jin )

// Wrong json from server
const json = {
	name : 'Jin' ,
	age : 33 ,
	birthday : '1984-08-04T12:00:00Z' ,
	phone : 791234567890,
	mail : '</script>' , // !
} as any

// Runtime error: ["mail"] </script> is not a /.+@.+/
printName( UserDTO( json ) )

Units

const { Weight, Length } = $mol_data_tagged({
	Weight: $mol_data_integer,
	Length: $mol_data_integer,
})

Length( 20 ) // Validate
let len = Length( 10 ) // Inferred type
let kg: typeof Weight.Value = Weight( 1000 ) // Explicit type

len = 20 // Implicit Cast
let num: number = len // Implicit Cast
len = Length( Weight( 20 ) ) // Explicit Cast

len = Weight( 20 ) // Compile time error
len = Length( 20.1 ) // Run time error

(De)Serialization

const Duration = $mol_data_pipe(
	$mol_data_variant(
		$mol_data_string ,
		$mol_data_integer ,
	) ,
	$mol_time_duration ,
)

JSON.stringify( Duration( 'P1D' ) ) // "P1DT"
JSON.stringify( Duration( 1000 ) ) // "PT1S"

Custom runtypes

const Password = $mol_data_setup( ( val: string ) => {
	
	const str = $mol_data_string( val )
	if( str.length >= 8 ) return str
	
	return $mol_fail(
		new $mol_data_error( `${ val } have length less than 8` )
	)
	
} )

Password( 123 ) // ❌ 123 is not a string
Password( 'qwerty' ) // ❌ qwerty have length less than 8
Password( 'qwertyuiop' ) // ✅
const MinLength = ( minLength = 8 )=> $mol_data_setup( ( val: string ) => {
		
	const str = $mol_data_string( val )
	if( str.length >= minLength ) return str
	
	return $mol_fail(
		new $mol_data_error( `${ val } have length less than ${minLength}` )
	)
	
}, minLength )

const Password = MinLength(8)
Password( 123 ) // ❌ 123 is not a string
Password( 'qwerty' ) // ❌ qwerty have length less than 8
Password( 'qwertyuiop' ) // ✅

Usage from NPM

npm install mol_data_all
import {
  $mol_data_tagged as Tagged,
  $mol_data_integer as Integer,
} from "mol_data_all"

const { Age } = Tagged({ Age: Integer })

let age = Age( 17 )
age = Age( age + 1 )

More complex example.

Similar projects