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

fast-check-io-ts

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-check-io-ts - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

4

lib/index.d.ts
import * as fc from 'fast-check';
import * as O from 'fp-ts/lib/Option';
import * as t from 'io-ts';
export declare type SupportedType = t.StringType | t.VoidType | t.ArrayType<any> | t.BooleanType | t.ExactType<any> | t.KeyofType<any> | t.NullType | t.TupleType<Array<t.Any>> | t.UnknownType | t.UndefinedType | t.LiteralType<string | number | boolean> | t.NumberType | t.InterfaceType<unknown> | t.PartialType<unknown> | t.UnionType<Array<any>> | t.RefinementType<t.Any> | t.IntersectionType<Array<t.Any>>;
export declare function getArbitrary<T extends t.Mixed>(codec: T): fc.Arbitrary<T['_A']>;
export declare function getArbitrary<T extends t.Any>(codec: T, customArbitrary?: <C extends t.Any>(codec: C) => O.Option<fc.Arbitrary<C['_A']>>): fc.Arbitrary<T['_A']>;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fc = require("fast-check");
var O = require("fp-ts/lib/Option");
var pipeable_1 = require("fp-ts/lib/pipeable");
var R = require("fp-ts/lib/Record");
var t = require("io-ts");
var objectTypes = ['ExactType', 'InterfaceType', 'PartialType'];
function getArbitrary(codec) {
var type = codec;
switch (type._tag) {
case 'StringType':
return fc.string();
case 'UndefinedType':
return fc.constant(undefined);
case 'NumberType':
return fc.float();
case 'BooleanType':
return fc.boolean();
case 'LiteralType':
return fc.constant(type.value);
case 'InterfaceType':
return fc.record(R.record.map(type.props, getArbitrary));
case 'PartialType':
return fc.record(R.record.map(type.props, function (type) { return getArbitrary(t.union([t.undefined, type])); }));
case 'UnionType':
return fc.oneof.apply(fc, type.types.map(getArbitrary));
case 'KeyofType':
return fc.oneof.apply(fc, Object.keys(type.keys).map(fc.constant));
case 'NullType':
return fc.constant(null);
case 'TupleType':
return fc.tuple.apply(fc, type.types.map(getArbitrary));
case 'UnknownType':
return fc.anything();
case 'VoidType':
return fc.constant(undefined);
case 'ArrayType':
return fc.array(getArbitrary(type.type));
case 'ExactType':
return getArbitrary(type.type);
case 'RefinementType':
return getArbitrary(type.type).filter(type.predicate);
case 'IntersectionType':
var isObjectIntersection = objectTypes.includes(type.types[0]._tag);
return isObjectIntersection
? fc.tuple.apply(fc, type.types.map(getArbitrary)).map(function (values) { return Object.assign.apply(Object, [{}].concat(values)); })
.filter(type.is)
: fc.oneof.apply(fc, type.types.map(getArbitrary)).filter(type.is);
}
throw new Error("Codec not supported: " + codec);
function getArbitrary(codec, customArbitrary) {
return pipeable_1.pipe(O.fromNullable(customArbitrary), O.chain(function (ca) { return ca(codec); }), O.getOrElse(function () {
var type = codec;
switch (type._tag) {
case 'StringType':
return fc.string();
case 'UndefinedType':
return fc.constant(undefined);
case 'NumberType':
return fc.float();
case 'BooleanType':
return fc.boolean();
case 'LiteralType':
return fc.constant(type.value);
case 'InterfaceType':
return fc.record(R.record.map(type.props, function (prop) { return getArbitrary(prop, customArbitrary); }));
case 'PartialType':
return fc.record(R.record.map(type.props, function (type) {
return getArbitrary(t.union([t.undefined, type]), customArbitrary);
}));
case 'UnionType':
return fc.oneof.apply(fc, type.types.map(function (t) { return getArbitrary(t, customArbitrary); }));
case 'KeyofType':
return fc.oneof.apply(fc, Object.keys(type.keys).map(fc.constant));
case 'NullType':
return fc.constant(null);
case 'TupleType':
return fc.tuple.apply(fc, type.types.map(function (t) { return getArbitrary(t, customArbitrary); }));
case 'UnknownType':
return fc.anything();
case 'VoidType':
return fc.constant(undefined);
case 'ArrayType':
return fc.array(getArbitrary(type.type, customArbitrary));
case 'ExactType':
return getArbitrary(type.type, customArbitrary);
case 'RefinementType':
return getArbitrary(type.type, customArbitrary).filter(type.predicate);
case 'IntersectionType':
var isObjectIntersection = objectTypes.includes(type.types[0]._tag);
return isObjectIntersection
? fc.tuple.apply(fc, type.types.map(function (t) { return getArbitrary(t, customArbitrary); })).map(function (values) { return Object.assign.apply(Object, [{}].concat(values)); })
.filter(type.is)
: fc.oneof.apply(fc, type.types.map(function (t) { return getArbitrary(t, customArbitrary); })).filter(type.is);
}
throw new Error("Codec not supported: " + codec);
}));
}
exports.getArbitrary = getArbitrary;
{
"name": "fast-check-io-ts",
"version": "0.2.1",
"version": "0.3.0",
"description": "io-ts codec to fast-check arbitrary mapping",

@@ -5,0 +5,0 @@ "files": [

@@ -8,2 +8,4 @@ # fast-check-io-ts

```ts
import * as fc from 'fast-check';
import * as t from 'io-ts';
import { getArbitrary } from 'fast-check-io-ts';

@@ -26,1 +28,35 @@

```
## Usage with custom io-ts codecs
```ts
import * as fc from 'fast-check';
import * as t from 'io-ts';
import { getArbitrary } from 'fast-check-io-ts';
const DateFromString = new t.Type<Date, string, unknown>(
'DateFromString',
(u): u is Date => u instanceof Date,
(u, c) =>
either.chain(t.string.validate(u, c), s => {
const d = new Date(s);
return isNaN(d.getTime()) ? t.failure(u, c) : t.success(d);
}),
a => a.toISOString()
);
function customDateFromStringArbitrary<C extends t.Any>(codec: C): Option<fc.Arbitrary<C['_A']>> {
return codec.name === 'DateFromString'
? some(
fc
.integer()
.filter(n => n > 0)
.map(n => new Date(n))
)
: none;
}
const dateFromStringArb = getArbitrary(DateFromString, customDateFromStringArbitrary);
console.log(fc.sample(dateFromStringArb, 1)[0]); // 1970-01-08T07:46:47.384Z
```
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