Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

runtypes

Package Overview
Dependencies
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

runtypes - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

lib/match.d.ts

3

lib/index.d.ts

@@ -5,2 +5,3 @@ export { Runtype, Static } from './runtype';

export * from './contract';
export * from './match';
export * from './types/always';

@@ -21,4 +22,4 @@ export * from './types/never';

export * from './types/function';
export { InstanceOf } from './types/instanceof';
export * from './types/lazy';
export * from './types/constraint';
export { InstanceOf } from './types/instanceof';

@@ -7,2 +7,3 @@ "use strict";

__export(require("./contract"));
__export(require("./match"));
__export(require("./types/always"));

@@ -26,5 +27,5 @@ __export(require("./types/never"));

__export(require("./types/function"));
var instanceof_1 = require("./types/instanceof");
exports.InstanceOf = instanceof_1.InstanceOf;
__export(require("./types/lazy"));
__export(require("./types/constraint"));
var instanceof_1 = require("./types/instanceof");
exports.InstanceOf = instanceof_1.InstanceOf;

@@ -58,3 +58,3 @@ import { Runtype } from './runtype';

underlying: Reflect;
constraint: ConstraintCheck<Runtype<always>>;
constraint: ConstraintCheck<Runtype<never>>;
args?: any;

@@ -61,0 +61,0 @@ } & Runtype<always> | {

@@ -45,6 +45,6 @@ "use strict";

case 'instanceof':
var name = refl.ctor.name;
return "InstanceOf<" + name + ">";
var name_1 = refl.ctor.name;
return "InstanceOf<" + name_1 + ">";
}
}; };
exports.default = show(false);

@@ -66,30 +66,30 @@ import { Runtype as Rt, Static } from '../runtype';

export interface Match1<A extends Rt> {
<Z>(cases: [Case<A, Z>]): (x: Static1<A>) => Z;
<Z>(a: Case<A, Z>): Matcher1<A, Z>;
}
export interface Match2<A extends Rt, B extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>]): (x: Static2<A, B>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>): Matcher2<A, B, Z>;
}
export interface Match3<A extends Rt, B extends Rt, C extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>]): (x: Static3<A, B, C>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>): Matcher3<A, B, C, Z>;
}
export interface Match4<A extends Rt, B extends Rt, C extends Rt, D extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>]): (x: Static4<A, B, C, D>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>): Matcher4<A, B, C, D, Z>;
}
export interface Match5<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>]): (x: Static5<A, B, C, D, E>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>): Matcher5<A, B, C, D, E, Z>;
}
export interface Match6<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>]): (x: Static6<A, B, C, D, E, F>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>, f: Case<F, Z>): Matcher6<A, B, C, D, E, F, Z>;
}
export interface Match7<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>, Case<G, Z>]): (x: Static7<A, B, C, D, E, F, G>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>, f: Case<F, Z>, g: Case<G, Z>): Matcher7<A, B, C, D, E, F, G, Z>;
}
export interface Match8<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>, Case<G, Z>, Case<H, Z>]): (x: Static8<A, B, C, D, E, F, G, H>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>, f: Case<F, Z>, g: Case<G, Z>, h: Case<H, Z>): Matcher8<A, B, C, D, E, F, G, H, Z>;
}
export interface Match9<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, I extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>, Case<G, Z>, Case<H, Z>, Case<I, Z>]): (x: Static9<A, B, C, D, E, F, G, H, I>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>, f: Case<F, Z>, g: Case<G, Z>, h: Case<H, Z>, i: Case<I, Z>): Matcher9<A, B, C, D, E, F, G, H, I, Z>;
}
export interface Match10<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, I extends Rt, J extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>, Case<G, Z>, Case<H, Z>, Case<I, Z>, Case<J, Z>]): (x: Static10<A, B, C, D, E, F, G, H, I, J>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>, f: Case<F, Z>, g: Case<G, Z>, h: Case<H, Z>, i: Case<I, Z>, j: Case<J, Z>): Matcher10<A, B, C, D, E, F, G, H, I, J, Z>;
}

@@ -107,1 +107,11 @@ export declare type Case<T extends Rt, Result> = (v: Static<T>) => Result;

export declare type Static10<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, I extends Rt, J extends Rt> = Static<A> | Static<B> | Static<C> | Static<D> | Static<E> | Static<F> | Static<G> | Static<H> | Static<I> | Static<J>;
export declare type Matcher1<A extends Rt, Z> = (x: Static1<A>) => Z;
export declare type Matcher2<A extends Rt, B extends Rt, Z> = (x: Static2<A, B>) => Z;
export declare type Matcher3<A extends Rt, B extends Rt, C extends Rt, Z> = (x: Static3<A, B, C>) => Z;
export declare type Matcher4<A extends Rt, B extends Rt, C extends Rt, D extends Rt, Z> = (x: Static4<A, B, C, D>) => Z;
export declare type Matcher5<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, Z> = (x: Static5<A, B, C, D, E>) => Z;
export declare type Matcher6<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, Z> = (x: Static6<A, B, C, D, E, F>) => Z;
export declare type Matcher7<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, Z> = (x: Static7<A, B, C, D, E, F, G>) => Z;
export declare type Matcher8<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, Z> = (x: Static8<A, B, C, D, E, F, G, H>) => Z;
export declare type Matcher9<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, I extends Rt, Z> = (x: Static9<A, B, C, D, E, F, G, H, I>) => Z;
export declare type Matcher10<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, F extends Rt, G extends Rt, H extends Rt, I extends Rt, J extends Rt, Z> = (x: Static10<A, B, C, D, E, F, G, H, I, J>) => Z;

@@ -9,9 +9,15 @@ "use strict";

}
var match = function (cases) { return function (x) {
for (var i = 0; i < alternatives.length; i++) {
if (alternatives[i].guard(x)) {
return cases[i](x);
var match = function () {
var cases = [];
for (var _i = 0; _i < arguments.length; _i++) {
cases[_i] = arguments[_i];
}
return function (x) {
for (var i = 0; i < alternatives.length; i++) {
if (alternatives[i].guard(x)) {
return cases[i](x);
}
}
}
}; };
};
};
return runtype_1.create(function (x) {

@@ -18,0 +24,0 @@ for (var _i = 0, alternatives_1 = alternatives; _i < alternatives_1.length; _i++) {

{
"name": "runtypes",
"version": "1.0.1",
"version": "2.0.0",
"description": "Runtime validation for static types",

@@ -9,3 +9,2 @@ "main": "./lib/index.js",

"build": "tsc --pretty",
"build:watch": "tsc --noEmit --pretty --watch",
"format": "prettier $([ \"$CI\" == true ] && echo --list-different || echo --write) './**/*.{ts,tsx,js,json,css}'",

@@ -12,0 +11,0 @@ "test": "yarn build && jest --no-cache --verbose --coverage",

@@ -169,3 +169,7 @@ # Runtypes [![Build Status](https://travis-ci.org/pelotom/runtypes.svg?branch=master)](https://travis-ci.org/pelotom/runtypes) [![Coverage Status](https://coveralls.io/repos/pelotom/runtypes/badge.svg?branch=master)](https://coveralls.io/github/pelotom/runtypes?branch=master)

```ts
const isHabitable = SpaceObject.match([asteroid => false, planet => planet.habitable]);
const isHabitable = SpaceObject.match(
asteroid => false,
planet => planet.habitable,
ship => true,
);

@@ -177,2 +181,25 @@ if (isHabitable(spaceObject)) {

There's also a top-level `match` function which allows testing an ad-hoc sequence of runtypes:
```ts
const makeANumber = match(
[Number, n => n * 3],
[Boolean, b => (b ? 1 : 0)],
[String, s => s.length],
);
makeANumber(9); // = 27
```
To allow the function to be applied to anything and then handle match failures, simply use an `Always` case at the end:
```ts
const makeANumber = match(
[Number, n => n * 3],
[Boolean, b => (b ? 1 : 0)],
[String, s => s.length],
[Always, () => 42]
);
```
## Constraint checking

@@ -179,0 +206,0 @@

@@ -12,2 +12,3 @@ import {

Static,
match,
} from '../index';

@@ -96,1 +97,10 @@

type SpaceObject = Static<typeof SpaceObject>; // = Asteroid | Planet | Ship
const isHabitable = SpaceObject.match(asteroid => false, planet => planet.habitable, ship => true);
// Pattern matching from a union
function foo(spaceObject: SpaceObject) {
if (isHabitable(spaceObject)) {
// ...
}
}

@@ -1,2 +0,2 @@

import { Static, Union, Literal } from '../index';
import { Static, Union, Literal, match } from '../index';

@@ -23,1 +23,7 @@ // Define the runtype

}
const isWeekend = match(
[Literal('Sunday'), () => true],
[Literal('Saturday'), () => true],
[Day, () => false],
);

@@ -42,6 +42,6 @@ import {

class SomeClass {
constructor(n: number) {}
constructor(public n: number) {}
}
class SomeOtherClass {
constructor(n: number) {}
constructor(public n: number) {}
}

@@ -311,3 +311,3 @@

| Constraint<Reflect, any>
| InstanceOf<Constructor>,
| InstanceOf<Constructor<never>>,
): Reflect => {

@@ -314,0 +314,0 @@ const check = <A>(X: Runtype<A>): A => X.check({});

@@ -5,2 +5,3 @@ export { Runtype, Static } from './runtype';

export * from './contract';
export * from './match';
export * from './types/always';

@@ -21,4 +22,4 @@ export * from './types/never';

export * from './types/function';
export { InstanceOf } from './types/instanceof';
export * from './types/lazy';
export * from './types/constraint';
export { InstanceOf } from './types/instanceof';

@@ -28,5 +28,5 @@ import { Runtype } from './runtype';

underlying: Reflect;
constraint: ConstraintCheck<Runtype<always>>;
constraint: ConstraintCheck<Runtype<never>>;
args?: any;
} & Runtype<always>
| { tag: 'instanceof'; ctor: Constructor<always> } & Runtype<always>;

@@ -22,3 +22,3 @@ import {

Reflect,
} from './index';
} from '.';
import show from './show';

@@ -25,0 +25,0 @@

@@ -8,3 +8,3 @@ import { Union, String, Literal } from '..';

it('works with exhaustive cases', () => {
const match = ThreeOrString.match([three => three + 5, str => str.length * 4]);
const match = ThreeOrString.match(three => three + 5, str => str.length * 4);
expect(match(3)).toBe(8);

@@ -11,0 +11,0 @@ expect(match('hello')).toBe(20);

@@ -217,3 +217,3 @@ import { Runtype as Rt, Static, create } from '../runtype';

export function Union(...alternatives: Rt[]): any {
const match = (cases: any[]) => (x: any) => {
const match = (...cases: any[]) => (x: any) => {
for (let i = 0; i < alternatives.length; i++) {

@@ -236,17 +236,22 @@ if (alternatives[i].guard(x)) {

export interface Match1<A extends Rt> {
<Z>(cases: [Case<A, Z>]): (x: Static1<A>) => Z;
<Z>(a: Case<A, Z>): Matcher1<A, Z>;
}
export interface Match2<A extends Rt, B extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>]): (x: Static2<A, B>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>): Matcher2<A, B, Z>;
}
export interface Match3<A extends Rt, B extends Rt, C extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>]): (x: Static3<A, B, C>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>): Matcher3<A, B, C, Z>;
}
export interface Match4<A extends Rt, B extends Rt, C extends Rt, D extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>]): (x: Static4<A, B, C, D>) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>): Matcher4<A, B, C, D, Z>;
}
export interface Match5<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>]): (
x: Static5<A, B, C, D, E>,
) => Z;
<Z>(a: Case<A, Z>, b: Case<B, Z>, c: Case<C, Z>, d: Case<D, Z>, e: Case<E, Z>): Matcher5<
A,
B,
C,
D,
E,
Z
>;
}

@@ -261,5 +266,10 @@ export interface Match6<

> {
<Z>(cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>]): (
x: Static6<A, B, C, D, E, F>,
) => Z;
<Z>(
a: Case<A, Z>,
b: Case<B, Z>,
c: Case<C, Z>,
d: Case<D, Z>,
e: Case<E, Z>,
f: Case<F, Z>,
): Matcher6<A, B, C, D, E, F, Z>;
}

@@ -276,4 +286,10 @@ export interface Match7<

<Z>(
cases: [Case<A, Z>, Case<B, Z>, Case<C, Z>, Case<D, Z>, Case<E, Z>, Case<F, Z>, Case<G, Z>],
): (x: Static7<A, B, C, D, E, F, G>) => Z;
a: Case<A, Z>,
b: Case<B, Z>,
c: Case<C, Z>,
d: Case<D, Z>,
e: Case<E, Z>,
f: Case<F, Z>,
g: Case<G, Z>,
): Matcher7<A, B, C, D, E, F, G, Z>;
}

@@ -291,13 +307,11 @@ export interface Match8<

<Z>(
cases: [
Case<A, Z>,
Case<B, Z>,
Case<C, Z>,
Case<D, Z>,
Case<E, Z>,
Case<F, Z>,
Case<G, Z>,
Case<H, Z>
],
): (x: Static8<A, B, C, D, E, F, G, H>) => Z;
a: Case<A, Z>,
b: Case<B, Z>,
c: Case<C, Z>,
d: Case<D, Z>,
e: Case<E, Z>,
f: Case<F, Z>,
g: Case<G, Z>,
h: Case<H, Z>,
): Matcher8<A, B, C, D, E, F, G, H, Z>;
}

@@ -316,14 +330,12 @@ export interface Match9<

<Z>(
cases: [
Case<A, Z>,
Case<B, Z>,
Case<C, Z>,
Case<D, Z>,
Case<E, Z>,
Case<F, Z>,
Case<G, Z>,
Case<H, Z>,
Case<I, Z>
],
): (x: Static9<A, B, C, D, E, F, G, H, I>) => Z;
a: Case<A, Z>,
b: Case<B, Z>,
c: Case<C, Z>,
d: Case<D, Z>,
e: Case<E, Z>,
f: Case<F, Z>,
g: Case<G, Z>,
h: Case<H, Z>,
i: Case<I, Z>,
): Matcher9<A, B, C, D, E, F, G, H, I, Z>;
}

@@ -343,15 +355,13 @@ export interface Match10<

<Z>(
cases: [
Case<A, Z>,
Case<B, Z>,
Case<C, Z>,
Case<D, Z>,
Case<E, Z>,
Case<F, Z>,
Case<G, Z>,
Case<H, Z>,
Case<I, Z>,
Case<J, Z>
],
): (x: Static10<A, B, C, D, E, F, G, H, I, J>) => Z;
a: Case<A, Z>,
b: Case<B, Z>,
c: Case<C, Z>,
d: Case<D, Z>,
e: Case<E, Z>,
f: Case<F, Z>,
g: Case<G, Z>,
h: Case<H, Z>,
i: Case<I, Z>,
j: Case<J, Z>,
): Matcher10<A, B, C, D, E, F, G, H, I, J, Z>;
}

@@ -444,1 +454,66 @@

| Static<J>;
export type Matcher1<A extends Rt, Z> = (x: Static1<A>) => Z;
export type Matcher2<A extends Rt, B extends Rt, Z> = (x: Static2<A, B>) => Z;
export type Matcher3<A extends Rt, B extends Rt, C extends Rt, Z> = (x: Static3<A, B, C>) => Z;
export type Matcher4<A extends Rt, B extends Rt, C extends Rt, D extends Rt, Z> = (
x: Static4<A, B, C, D>,
) => Z;
export type Matcher5<A extends Rt, B extends Rt, C extends Rt, D extends Rt, E extends Rt, Z> = (
x: Static5<A, B, C, D, E>,
) => Z;
export type Matcher6<
A extends Rt,
B extends Rt,
C extends Rt,
D extends Rt,
E extends Rt,
F extends Rt,
Z
> = (x: Static6<A, B, C, D, E, F>) => Z;
export type Matcher7<
A extends Rt,
B extends Rt,
C extends Rt,
D extends Rt,
E extends Rt,
F extends Rt,
G extends Rt,
Z
> = (x: Static7<A, B, C, D, E, F, G>) => Z;
export type Matcher8<
A extends Rt,
B extends Rt,
C extends Rt,
D extends Rt,
E extends Rt,
F extends Rt,
G extends Rt,
H extends Rt,
Z
> = (x: Static8<A, B, C, D, E, F, G, H>) => Z;
export type Matcher9<
A extends Rt,
B extends Rt,
C extends Rt,
D extends Rt,
E extends Rt,
F extends Rt,
G extends Rt,
H extends Rt,
I extends Rt,
Z
> = (x: Static9<A, B, C, D, E, F, G, H, I>) => Z;
export type Matcher10<
A extends Rt,
B extends Rt,
C extends Rt,
D extends Rt,
E extends Rt,
F extends Rt,
G extends Rt,
H extends Rt,
I extends Rt,
J extends Rt,
Z
> = (x: Static10<A, B, C, D, E, F, G, H, I, J>) => Z;

@@ -5,3 +5,3 @@ {

"target": "es5",
"lib": ["es2015"],
"lib": ["es2015", "dom"],
"strict": true,

@@ -15,3 +15,3 @@ "noUnusedLocals": true,

},
"files": ["src/index.ts"]
"exclude": ["./lib/**/*", "./src/examples/**/*"]
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc