New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@funkia/io

Package Overview
Dependencies
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@funkia/io - npm Package Compare versions

Comparing version

to
0.0.5

75

dist/defs/index.d.ts

@@ -1,60 +0,25 @@

import { Freer } from "./freer";
export declare type F0<Z> = () => Z;
export declare type F1<A, Z> = (a: A) => Z;
export declare type F2<A, B, Z> = (a: A, b: B) => Z;
export declare type F3<A, B, C, Z> = (a: A, b: B, c: C) => Z;
export declare type F4<A, B, C, D, Z> = (a: A, b: B, c: C, d: D) => Z;
export declare type F5<A, B, C, D, E, Z> = (a: A, b: B, c: C, d: D, e: E) => Z;
export declare type IOValue<A> = Call | CallP | ThrowE | CatchE;
export declare class Call {
fn: Function;
args: any[];
type: "call";
constructor(fn: Function, args: any[]);
}
export declare class CallP {
fn: Function;
args: any[];
type: "callP";
constructor(fn: Function, args: any[]);
}
export declare class ThrowE {
declare type TestValue<A> = {
value: A;
} | {
error: any;
type: "throwE";
constructor(error: any);
};
declare type TestResult<A> = [TestValue<A>, number];
export declare abstract class IO<A> {
abstract run(): Promise<A>;
abstract test(mocks: [IO<any>, any][], idx: number): TestResult<A>;
static of<A>(a: A): IO<A>;
of<A>(a: A): IO<A>;
map<B>(f: (a: A) => B): IO<B>;
chain<B>(f: (a: A) => IO<B>): IO<B>;
flatMap<B>(f: (a: A) => IO<B>): IO<B>;
}
export declare class CatchE {
handler: (error: any) => IO<any>;
io: IO<any>;
type: "catchE";
constructor(handler: (error: any) => IO<any>, io: IO<any>);
}
export declare type IO<A> = Freer<IOValue<any>, A>;
export declare const IO: typeof Freer;
export declare function withEffects<A, Z>(f: F1<A, Z>): (a: A) => IO<Z>;
export declare function withEffects<A, B, Z>(f: F2<A, B, Z>): (a: A, b: B) => IO<Z>;
export declare function withEffects<A, B, C, Z>(f: F3<A, B, C, Z>): (a: A, b: B, c: C) => IO<Z>;
export declare function withEffects<A, B, C, D, Z>(f: F4<A, B, C, D, Z>): (a: A, b: B, c: C, d: D) => IO<Z>;
export declare function withEffects<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Z>): (a: A, b: B, c: C, d: D, e: E) => IO<Z>;
export declare function withEffectsP<A, Z>(f: F1<A, Promise<Z>>): (a: A) => IO<Z>;
export declare function withEffectsP<A, B, Z>(f: F2<A, B, Promise<Z>>): (a: A, b: B) => IO<Z>;
export declare function withEffectsP<A, B, C, Z>(f: F3<A, B, C, Promise<Z>>): (a: A, b: B, c: C) => IO<Z>;
export declare function withEffectsP<A, B, C, D, Z>(f: F4<A, B, C, D, Promise<Z>>): (a: A, b: B, c: C, d: D) => IO<Z>;
export declare function withEffectsP<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Promise<Z>>): (a: A, b: B, c: C, d: D, e: E) => IO<Z>;
export declare function call<Z>(f: F0<Z>): IO<Z>;
export declare function call<A, Z>(f: F1<A, Z>, a: A): IO<Z>;
export declare function call<A, B, Z>(f: F2<A, B, Z>, a: A, b: B): IO<Z>;
export declare function call<A, B, C, Z>(f: F3<A, B, C, Z>, a: A, b: B, c: C): IO<Z>;
export declare function call<A, B, C, D, Z>(f: F4<A, B, C, D, Z>, a: A, b: B, c: C, d: D): IO<Z>;
export declare function call<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Z>, a: A, b: B, c: C, d: D, e: E): IO<Z>;
export declare function callP<Z>(f: F0<Z>): IO<Z>;
export declare function callP<A, Z>(f: F1<A, Promise<Z>>, a: A): IO<Z>;
export declare function callP<A, B, Z>(f: F2<A, B, Promise<Z>>, a: A, b: B): IO<Z>;
export declare function callP<A, B, C, Z>(f: F3<A, B, C, Promise<Z>>, a: A, b: B, c: C): IO<Z>;
export declare function callP<A, B, C, D, Z>(f: F4<A, B, C, D, Promise<Z>>, a: A, b: B, c: C, d: D): IO<Z>;
export declare function callP<A, B, C, D, E, Z>(f: F5<A, B, C, D, E, Promise<Z>>, a: A, b: B, c: C, d: D, e: E): IO<Z>;
export declare function map<A, B>(f: (a: A) => B, io: IO<A>): IO<B>;
export declare function withEffects<A, P extends any[]>(f: (...args: P) => A): (...args: P) => IO<A>;
export declare function withEffectsP<A, P extends any[]>(f: (...args: P) => Promise<A>): (...args: P) => IO<A>;
export declare function call<A, P extends any[]>(f: (...args: P) => A, ...args: P): IO<A>;
export declare function callP<A, P extends any[]>(f: (...args: P) => Promise<A>, ...args: P): IO<A>;
export declare function throwE(error: any): IO<any>;
export declare function catchE(errorHandler: (error: any) => IO<any>, io: IO<any>): IO<any>;
export declare function doRunIO<A>(e: IO<A>): Promise<A>;
export declare function runIO<A>(e: IO<A>): Promise<A>;
export declare function testIO<A>(e: IO<A>, arr: any[], a: A): void;
export declare function testIO<A>(io: IO<A>, mocks: any[], expectedResult: A): void;
export {};

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

import { Freer, liftF } from "./freer";
function deepEqual(a, b) {

@@ -12,2 +11,5 @@ if (typeof a === "object" && typeof b === "object") {

}
else if (typeof a === "function" && typeof b === "function") {
return true;
}
else {

@@ -17,90 +19,129 @@ return a === b;

}
export class Call {
constructor(fn, args) {
this.fn = fn;
this.args = args;
this.type = "call";
export class IO {
static of(a) {
return new PureIO(a);
}
}
export class CallP {
constructor(fn, args) {
this.fn = fn;
this.args = args;
this.type = "callP";
of(a) {
return new PureIO(a);
}
map(f) {
return new FlatMapIO(this, (a) => IO.of(f(a)));
}
chain(f) {
return new FlatMapIO(this, f);
}
flatMap(f) {
return new FlatMapIO(this, f);
}
}
export class ThrowE {
constructor(error) {
this.error = error;
this.type = "throwE";
class PureIO extends IO {
constructor(a) {
super();
this.a = a;
}
run() {
return Promise.resolve(this.a);
}
test(_mocks, idx) {
return [{ value: this.a }, idx];
}
}
export class CatchE {
constructor(handler, io) {
this.handler = handler;
class FlatMapIO extends IO {
constructor(io, f) {
super();
this.io = io;
this.type = "catchE";
this.f = f;
}
run() {
return this.io.run().then((a) => this.f(a).run());
}
test(mocks, idx) {
const [value, newIdx] = this.io.test(mocks, idx);
if ("value" in value) {
return this.f(value.value).test(mocks, newIdx);
}
else {
return [value, newIdx];
}
}
}
export const IO = Freer;
export function withEffects(fn) {
return (...args) => liftF(new Call(fn, args));
export function map(f, io) {
return new FlatMapIO(io, (a) => IO.of(f(a)));
}
export function withEffectsP(fn) {
return (...args) => liftF(new CallP(fn, args));
class CallPromiseIO extends IO {
constructor(f, args) {
super();
this.f = f;
this.args = args;
}
run() {
return this.f(...this.args);
}
test(mocks, idx) {
if (!deepEqual(this, mocks[idx][0])) {
throw new Error(`Value invalid, expected ${mocks[idx][0]} but saw ${this}`);
}
return [{ value: mocks[idx][1] }, idx + 1];
}
}
export function call(fn, ...args) {
return liftF(new Call(fn, args));
// in the IO monad
export function withEffects(f) {
return (...args) => new CallPromiseIO((...a) => Promise.resolve(f(...a)), args);
}
export function callP(fn, ...args) {
return liftF(new CallP(fn, args));
export function withEffectsP(f) {
return (...args) => new CallPromiseIO(f, args);
}
export function call(f, ...args) {
return new CallPromiseIO((...a) => Promise.resolve(f(...a)), args);
}
export function callP(f, ...args) {
return new CallPromiseIO(f, args);
}
class ThrowErrorIO extends IO {
constructor(error) {
super();
this.error = error;
}
run() {
return Promise.reject(this.error);
}
test(_mocks, idx) {
return [{ error: this.error }, idx];
}
}
export function throwE(error) {
return liftF(new ThrowE(error));
return new ThrowErrorIO(error);
}
class CatchErrorIO extends IO {
constructor(io, errorHandler) {
super();
this.io = io;
this.errorHandler = errorHandler;
}
run() {
return this.io.run().catch((err) => this.errorHandler(err).run());
}
test(mocks, idx) {
const [value, nextIdx] = this.io.test(mocks, idx);
if ("value" in value) {
return [value, nextIdx];
}
else {
return this.errorHandler(value.error).test(mocks, nextIdx);
}
}
}
export function catchE(errorHandler, io) {
return liftF(new CatchE(errorHandler, io));
return new CatchErrorIO(io, errorHandler);
}
export function doRunIO(e) {
return e.match({
pure: (a) => Promise.resolve(a),
bind: (io, cont) => {
switch (io.type) {
case "call":
return runIO(cont(io.fn(...io.args)));
case "callP":
return io.fn(...io.args).then((a) => runIO(cont(a)));
case "catchE":
return doRunIO(io.io)
.then((a) => runIO(cont(a)))
.catch((err) => doRunIO(io.handler(err)));
case "throwE":
return Promise.reject(io.error);
}
}
});
}
export function runIO(e) {
return doRunIO(e);
return e.run();
}
function doTestIO(e, arr, ending, idx) {
e.match({
pure: (a2) => {
if (ending !== a2) {
throw new Error(`Pure value invalid, expected ${ending} but saw ${a2}`);
}
},
bind: (io, cont) => {
const [{ val: io2 }, a] = arr[idx];
if (!deepEqual(io, io2)) {
throw new Error(`Value invalid, expected ${io2} but saw ${io}`);
}
else {
doTestIO(cont(a), arr, ending, idx + 1);
}
export function testIO(io, mocks, expectedResult) {
const [value] = io.test(mocks, 0);
if ("value" in value) {
if (!deepEqual(value.value, expectedResult)) {
throw new Error(`Value invalid, expected ${expectedResult} but saw ${value.value}`);
}
});
}
}
export function testIO(e, arr, a) {
doTestIO(e, arr, a, 0);
}
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var freer_1 = require("./freer");
function deepEqual(a, b) {

@@ -15,2 +27,5 @@ if (typeof a === "object" && typeof b === "object") {

}
else if (typeof a === "function" && typeof b === "function") {
return true;
}
else {

@@ -20,39 +35,86 @@ return a === b;

}
var Call = /** @class */ (function () {
function Call(fn, args) {
this.fn = fn;
this.args = args;
this.type = "call";
var IO = /** @class */ (function () {
function IO() {
}
return Call;
IO.of = function (a) {
return new PureIO(a);
};
IO.prototype.of = function (a) {
return new PureIO(a);
};
IO.prototype.map = function (f) {
return new FlatMapIO(this, function (a) { return IO.of(f(a)); });
};
IO.prototype.chain = function (f) {
return new FlatMapIO(this, f);
};
IO.prototype.flatMap = function (f) {
return new FlatMapIO(this, f);
};
return IO;
}());
exports.Call = Call;
var CallP = /** @class */ (function () {
function CallP(fn, args) {
this.fn = fn;
this.args = args;
this.type = "callP";
exports.IO = IO;
var PureIO = /** @class */ (function (_super) {
__extends(PureIO, _super);
function PureIO(a) {
var _this = _super.call(this) || this;
_this.a = a;
return _this;
}
return CallP;
}());
exports.CallP = CallP;
var ThrowE = /** @class */ (function () {
function ThrowE(error) {
this.error = error;
this.type = "throwE";
PureIO.prototype.run = function () {
return Promise.resolve(this.a);
};
PureIO.prototype.test = function (_mocks, idx) {
return [{ value: this.a }, idx];
};
return PureIO;
}(IO));
var FlatMapIO = /** @class */ (function (_super) {
__extends(FlatMapIO, _super);
function FlatMapIO(io, f) {
var _this = _super.call(this) || this;
_this.io = io;
_this.f = f;
return _this;
}
return ThrowE;
}());
exports.ThrowE = ThrowE;
var CatchE = /** @class */ (function () {
function CatchE(handler, io) {
this.handler = handler;
this.io = io;
this.type = "catchE";
FlatMapIO.prototype.run = function () {
var _this = this;
return this.io.run().then(function (a) { return _this.f(a).run(); });
};
FlatMapIO.prototype.test = function (mocks, idx) {
var _a = this.io.test(mocks, idx), value = _a[0], newIdx = _a[1];
if ("value" in value) {
return this.f(value.value).test(mocks, newIdx);
}
else {
return [value, newIdx];
}
};
return FlatMapIO;
}(IO));
function map(f, io) {
return new FlatMapIO(io, function (a) { return IO.of(f(a)); });
}
exports.map = map;
var CallPromiseIO = /** @class */ (function (_super) {
__extends(CallPromiseIO, _super);
function CallPromiseIO(f, args) {
var _this = _super.call(this) || this;
_this.f = f;
_this.args = args;
return _this;
}
return CatchE;
}());
exports.CatchE = CatchE;
exports.IO = freer_1.Freer;
function withEffects(fn) {
CallPromiseIO.prototype.run = function () {
return this.f.apply(this, this.args);
};
CallPromiseIO.prototype.test = function (mocks, idx) {
if (!deepEqual(this, mocks[idx][0])) {
throw new Error("Value invalid, expected " + mocks[idx][0] + " but saw " + this);
}
return [{ value: mocks[idx][1] }, idx + 1];
};
return CallPromiseIO;
}(IO));
// in the IO monad
function withEffects(f) {
return function () {

@@ -63,7 +125,13 @@ var args = [];

}
return freer_1.liftF(new Call(fn, args));
return new CallPromiseIO(function () {
var a = [];
for (var _i = 0; _i < arguments.length; _i++) {
a[_i] = arguments[_i];
}
return Promise.resolve(f.apply(void 0, a));
}, args);
};
}
exports.withEffects = withEffects;
function withEffectsP(fn) {
function withEffectsP(f) {
return function () {

@@ -74,7 +142,7 @@ var args = [];

}
return freer_1.liftF(new CallP(fn, args));
return new CallPromiseIO(f, args);
};
}
exports.withEffectsP = withEffectsP;
function call(fn) {
function call(f) {
var args = [];

@@ -84,6 +152,12 @@ for (var _i = 1; _i < arguments.length; _i++) {

}
return freer_1.liftF(new Call(fn, args));
return new CallPromiseIO(function () {
var a = [];
for (var _i = 0; _i < arguments.length; _i++) {
a[_i] = arguments[_i];
}
return Promise.resolve(f.apply(void 0, a));
}, args);
}
exports.call = call;
function callP(fn) {
function callP(f) {
var args = [];

@@ -93,58 +167,63 @@ for (var _i = 1; _i < arguments.length; _i++) {

}
return freer_1.liftF(new CallP(fn, args));
return new CallPromiseIO(f, args);
}
exports.callP = callP;
var ThrowErrorIO = /** @class */ (function (_super) {
__extends(ThrowErrorIO, _super);
function ThrowErrorIO(error) {
var _this = _super.call(this) || this;
_this.error = error;
return _this;
}
ThrowErrorIO.prototype.run = function () {
return Promise.reject(this.error);
};
ThrowErrorIO.prototype.test = function (_mocks, idx) {
return [{ error: this.error }, idx];
};
return ThrowErrorIO;
}(IO));
function throwE(error) {
return freer_1.liftF(new ThrowE(error));
return new ThrowErrorIO(error);
}
exports.throwE = throwE;
var CatchErrorIO = /** @class */ (function (_super) {
__extends(CatchErrorIO, _super);
function CatchErrorIO(io, errorHandler) {
var _this = _super.call(this) || this;
_this.io = io;
_this.errorHandler = errorHandler;
return _this;
}
CatchErrorIO.prototype.run = function () {
var _this = this;
return this.io.run().catch(function (err) { return _this.errorHandler(err).run(); });
};
CatchErrorIO.prototype.test = function (mocks, idx) {
var _a = this.io.test(mocks, idx), value = _a[0], nextIdx = _a[1];
if ("value" in value) {
return [value, nextIdx];
}
else {
return this.errorHandler(value.error).test(mocks, nextIdx);
}
};
return CatchErrorIO;
}(IO));
function catchE(errorHandler, io) {
return freer_1.liftF(new CatchE(errorHandler, io));
return new CatchErrorIO(io, errorHandler);
}
exports.catchE = catchE;
function doRunIO(e) {
return e.match({
pure: function (a) { return Promise.resolve(a); },
bind: function (io, cont) {
switch (io.type) {
case "call":
return runIO(cont(io.fn.apply(io, io.args)));
case "callP":
return io.fn.apply(io, io.args).then(function (a) { return runIO(cont(a)); });
case "catchE":
return doRunIO(io.io)
.then(function (a) { return runIO(cont(a)); })
.catch(function (err) { return doRunIO(io.handler(err)); });
case "throwE":
return Promise.reject(io.error);
}
}
});
}
exports.doRunIO = doRunIO;
function runIO(e) {
return doRunIO(e);
return e.run();
}
exports.runIO = runIO;
function doTestIO(e, arr, ending, idx) {
e.match({
pure: function (a2) {
if (ending !== a2) {
throw new Error("Pure value invalid, expected " + ending + " but saw " + a2);
}
},
bind: function (io, cont) {
var _a = arr[idx], io2 = _a[0].val, a = _a[1];
if (!deepEqual(io, io2)) {
throw new Error("Value invalid, expected " + io2 + " but saw " + io);
}
else {
doTestIO(cont(a), arr, ending, idx + 1);
}
function testIO(io, mocks, expectedResult) {
var value = io.test(mocks, 0)[0];
if ("value" in value) {
if (!deepEqual(value.value, expectedResult)) {
throw new Error("Value invalid, expected " + expectedResult + " but saw " + value.value);
}
});
}
}
function testIO(e, arr, a) {
doTestIO(e, arr, a, 0);
}
exports.testIO = testIO;
{
"name": "@funkia/io",
"version": "0.0.4",
"version": "0.0.5",
"description": "A library that turns impure code pure.",

@@ -44,17 +44,17 @@ "main": "dist/index.js",

"dependencies": {
"@funkia/jabz": "0.0.24",
"prettier": "^1.13.7",
"tslib": "^1.9.3"
"tslib": "^1.10.0"
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/mocha": "^5.2.4",
"chai": "^4.1.2",
"codecov": "^3.0.2",
"mocha": "^5.2.0",
"np": "^3.0.4",
"nyc": "^12.0.2",
"source-map-support": "^0.5.6",
"ts-node": "^7.0.0",
"typescript": "^2.9.2"
"@funkia/jabz": "0.0.24",
"@types/chai": "^4.2.0",
"@types/mocha": "^5.2.7",
"chai": "^4.2.0",
"codecov": "^3.5.0",
"mocha": "^6.2.0",
"np": "^5.0.3",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"source-map-support": "^0.5.13",
"ts-node": "^8.3.0",
"typescript": "^3.5.3"
},

@@ -61,0 +61,0 @@ "nyc": {