resumon 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +62 -0
- package/dist/resumon.es.js +95 -0
- package/dist/resumon.umd.js +1 -0
- package/dist/types/src/err.d.ts +19 -0
- package/dist/types/src/main.d.ts +3 -0
- package/dist/types/src/ok.d.ts +19 -0
- package/dist/types/src/result.d.ts +31 -0
- package/package.json +84 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Jan Müller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
<h1 align="center">resumon</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
A tiny TypeScript result monad.
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://resumon.yeger.eu">
|
|
9
|
+
Documentation
|
|
10
|
+
</a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="https://github.com/DerYeger/resumon/actions/workflows/ci.yml">
|
|
15
|
+
<img alt="CI" src="https://img.shields.io/github/workflow/status/DerYeger/resumon/CI?label=ci&logo=github&color=#4DC71F">
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://www.npmjs.com/package/resumon">
|
|
18
|
+
<img alt="NPM" src="https://img.shields.io/npm/v/resumon?logo=npm">
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://codecov.io/gh/DerYeger/resumon">
|
|
21
|
+
<img alt="Coverage" src="https://codecov.io/gh/DerYeger/resumon/branch/master/graph/badge.svg?token=p35W6u2noe">
|
|
22
|
+
</a>
|
|
23
|
+
<a href="https://lgtm.com/projects/g/DerYeger/resumon">
|
|
24
|
+
<img alt="LGTM Grade" src="https://img.shields.io/lgtm/grade/javascript/github/DerYeger/resumon?logo=lgtm">
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://opensource.org/licenses/MIT">
|
|
27
|
+
<img alt="MIT" src="https://img.shields.io/npm/l/resumon?color=%234DC71F">
|
|
28
|
+
</a>
|
|
29
|
+
<a href="https://bundlephobia.com/package/resumon">
|
|
30
|
+
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/resumon">
|
|
31
|
+
</a>
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# yarn
|
|
38
|
+
$ yarn add resumon
|
|
39
|
+
|
|
40
|
+
# npm
|
|
41
|
+
$ npm install resumon
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Development
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# install dependencies
|
|
48
|
+
$ yarn install
|
|
49
|
+
|
|
50
|
+
# build for production
|
|
51
|
+
$ yarn build
|
|
52
|
+
|
|
53
|
+
# lint project files
|
|
54
|
+
$ yarn lint
|
|
55
|
+
|
|
56
|
+
# run tests
|
|
57
|
+
$ yarn test
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## License
|
|
61
|
+
|
|
62
|
+
[MIT](./LICENSE) - Copyright © Jan Müller
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
class IllegalStateError extends Error {
|
|
8
|
+
}
|
|
9
|
+
class Err {
|
|
10
|
+
constructor(error) {
|
|
11
|
+
__publicField(this, "isOk", false);
|
|
12
|
+
__publicField(this, "isError", true);
|
|
13
|
+
this.error = error;
|
|
14
|
+
}
|
|
15
|
+
get() {
|
|
16
|
+
throw new IllegalStateError("Cannot get data or Err");
|
|
17
|
+
}
|
|
18
|
+
getOrElse(other) {
|
|
19
|
+
return other;
|
|
20
|
+
}
|
|
21
|
+
getOrNull() {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
getOrUndefined() {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
getError() {
|
|
28
|
+
return this.error;
|
|
29
|
+
}
|
|
30
|
+
getErrorOrElse() {
|
|
31
|
+
return this.error;
|
|
32
|
+
}
|
|
33
|
+
getErrorOrNull() {
|
|
34
|
+
return this.error;
|
|
35
|
+
}
|
|
36
|
+
getErrorOrUndefined() {
|
|
37
|
+
return this.error;
|
|
38
|
+
}
|
|
39
|
+
andThen() {
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
map() {
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
mapError(map) {
|
|
46
|
+
return new Err(map(this.error));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function err(error) {
|
|
50
|
+
return new Err(error);
|
|
51
|
+
}
|
|
52
|
+
class Ok {
|
|
53
|
+
constructor(data) {
|
|
54
|
+
__publicField(this, "isOk", true);
|
|
55
|
+
__publicField(this, "isError", false);
|
|
56
|
+
this.data = data;
|
|
57
|
+
}
|
|
58
|
+
get() {
|
|
59
|
+
return this.data;
|
|
60
|
+
}
|
|
61
|
+
getOrElse() {
|
|
62
|
+
return this.data;
|
|
63
|
+
}
|
|
64
|
+
getOrNull() {
|
|
65
|
+
return this.data;
|
|
66
|
+
}
|
|
67
|
+
getOrUndefined() {
|
|
68
|
+
return this.data;
|
|
69
|
+
}
|
|
70
|
+
getError() {
|
|
71
|
+
throw new IllegalStateError("Cannot get error of Ok");
|
|
72
|
+
}
|
|
73
|
+
getErrorOrElse(other) {
|
|
74
|
+
return other;
|
|
75
|
+
}
|
|
76
|
+
getErrorOrNull() {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
getErrorOrUndefined() {
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
andThen(map) {
|
|
83
|
+
return map(this.data);
|
|
84
|
+
}
|
|
85
|
+
map(map) {
|
|
86
|
+
return new Ok(map(this.data));
|
|
87
|
+
}
|
|
88
|
+
mapError() {
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function ok(data) {
|
|
93
|
+
return new Ok(data);
|
|
94
|
+
}
|
|
95
|
+
export { Err, IllegalStateError, Ok, err, ok };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var d=Object.defineProperty;var l=(r,e,n)=>e in r?d(r,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):r[e]=n;var o=(r,e,n)=>(l(r,typeof e!="symbol"?e+"":e,n),n);(function(r,e){typeof exports=="object"&&typeof module!="undefined"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(r=typeof globalThis!="undefined"?globalThis:r||self,e(r.resumon={}))})(this,function(r){"use strict";class e extends Error{}class n{constructor(t){o(this,"isOk",!1);o(this,"isError",!0);this.error=t}get(){throw new e("Cannot get data or Err")}getOrElse(t){return t}getOrNull(){return null}getOrUndefined(){}getError(){return this.error}getErrorOrElse(){return this.error}getErrorOrNull(){return this.error}getErrorOrUndefined(){return this.error}andThen(){return this}map(){return this}mapError(t){return new n(t(this.error))}}function s(i){return new n(i)}class u{constructor(t){o(this,"isOk",!0);o(this,"isError",!1);this.data=t}get(){return this.data}getOrElse(){return this.data}getOrNull(){return this.data}getOrUndefined(){return this.data}getError(){throw new e("Cannot get error of Ok")}getErrorOrElse(t){return t}getErrorOrNull(){return null}getErrorOrUndefined(){}andThen(t){return t(this.data)}map(t){return new u(t(this.data))}mapError(){return this}}function a(i){return new u(i)}r.Err=n,r.IllegalStateError=e,r.Ok=u,r.err=s,r.ok=a,Object.defineProperty(r,"__esModule",{value:!0}),r[Symbol.toStringTag]="Module"});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Failed, Result } from './result';
|
|
2
|
+
export declare class Err<D, E> implements Result<D, E>, Failed<E> {
|
|
3
|
+
private readonly error;
|
|
4
|
+
readonly isOk = false;
|
|
5
|
+
readonly isError = true;
|
|
6
|
+
constructor(error: E);
|
|
7
|
+
get(): D;
|
|
8
|
+
getOrElse(other: D): D;
|
|
9
|
+
getOrNull(): null;
|
|
10
|
+
getOrUndefined(): undefined;
|
|
11
|
+
getError(): E;
|
|
12
|
+
getErrorOrElse(): E;
|
|
13
|
+
getErrorOrNull(): E;
|
|
14
|
+
getErrorOrUndefined(): E;
|
|
15
|
+
andThen<T>(): Result<T, E>;
|
|
16
|
+
map<T>(): Result<T, E>;
|
|
17
|
+
mapError<T, U>(map: (error: E) => U): Result<T, U>;
|
|
18
|
+
}
|
|
19
|
+
export declare function err<D, E>(error: E): Result<D, E>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Result, Succeeded } from './result';
|
|
2
|
+
export declare class Ok<D, E> implements Result<D, E>, Succeeded<D> {
|
|
3
|
+
private readonly data;
|
|
4
|
+
readonly isOk = true;
|
|
5
|
+
readonly isError = false;
|
|
6
|
+
constructor(data: D);
|
|
7
|
+
get(): D;
|
|
8
|
+
getOrElse(): D;
|
|
9
|
+
getOrNull(): D;
|
|
10
|
+
getOrUndefined(): D;
|
|
11
|
+
getError(): E;
|
|
12
|
+
getErrorOrElse(other: E): E;
|
|
13
|
+
getErrorOrNull(): null;
|
|
14
|
+
getErrorOrUndefined(): undefined;
|
|
15
|
+
andThen<T>(map: (data: D) => Result<T, E>): Result<T, E>;
|
|
16
|
+
map<T>(map: (data: D) => T): Result<T, E>;
|
|
17
|
+
mapError<U>(): Result<D, U>;
|
|
18
|
+
}
|
|
19
|
+
export declare function ok<D, E>(data: D): Result<D, E>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface Result<D, E> extends Succeedable<D>, Failable<E> {
|
|
2
|
+
readonly isOk: boolean;
|
|
3
|
+
readonly isError: boolean;
|
|
4
|
+
andThen<T>(map: (data: D) => Result<T, E>): Result<T, E>;
|
|
5
|
+
map<T>(map: (data: D) => T): Result<T, E>;
|
|
6
|
+
mapError<U>(map: (error: E) => U): Result<D, U>;
|
|
7
|
+
}
|
|
8
|
+
export interface Succeedable<D> {
|
|
9
|
+
get(): D;
|
|
10
|
+
getOrElse(other: D): D;
|
|
11
|
+
getOrNull(): D | null;
|
|
12
|
+
getOrUndefined(): D | undefined;
|
|
13
|
+
}
|
|
14
|
+
export interface Succeeded<D> extends Succeedable<D> {
|
|
15
|
+
getOrElse(): D;
|
|
16
|
+
getOrNull(): D;
|
|
17
|
+
getOrUndefined(): D;
|
|
18
|
+
}
|
|
19
|
+
export interface Failable<E> {
|
|
20
|
+
getError(): E;
|
|
21
|
+
getErrorOrElse(other: E): E;
|
|
22
|
+
getErrorOrNull(): E | null;
|
|
23
|
+
getErrorOrUndefined(): E | undefined;
|
|
24
|
+
}
|
|
25
|
+
export interface Failed<E> extends Failable<E> {
|
|
26
|
+
getErrorOrElse(): E;
|
|
27
|
+
getErrorOrNull(): E;
|
|
28
|
+
getErrorOrUndefined(): E;
|
|
29
|
+
}
|
|
30
|
+
export declare class IllegalStateError extends Error {
|
|
31
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "resumon",
|
|
3
|
+
"description": "A tiny TypeScript result monad.",
|
|
4
|
+
"version": "1.0.1",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "github:DerYeger/resumon",
|
|
7
|
+
"homepage": "https://resumon.yeger.eu",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/DerYeger/resumon/issues"
|
|
10
|
+
},
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "Jan Müller",
|
|
13
|
+
"url": "https://github.com/DerYeger"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"result",
|
|
17
|
+
"monad",
|
|
18
|
+
"typescript"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"prepare": "husky install",
|
|
22
|
+
"prebuild": "rimraf ./dist",
|
|
23
|
+
"build": "tsc --noEmit && vite build",
|
|
24
|
+
"docs:dev": "vuepress dev docs",
|
|
25
|
+
"docs:build": "vuepress build docs",
|
|
26
|
+
"lint": "yarn lint:prettier && yarn lint:eslint",
|
|
27
|
+
"lint:commits": "yarn commitlint --from=HEAD~1",
|
|
28
|
+
"lint:eslint": "eslint \"./**/*.{js,ts,vue}\"",
|
|
29
|
+
"lint:prettier": "prettier --check \"./**/*.{js,json,html,scss,ts,vue,yml}\"",
|
|
30
|
+
"fix": "yarn pretty && yarn lint:eslint --fix ",
|
|
31
|
+
"test": "vitest --api --open",
|
|
32
|
+
"test:ci": "rimraf ./coverage && c8 --reporter=lcov --reporter=text --all --src \"src\" --include \"src/*\" vitest --run",
|
|
33
|
+
"test:run": "vitest --run",
|
|
34
|
+
"pretty": "prettier --write \"./**/*.{js,json,html,scss,ts,vue,yml}\"",
|
|
35
|
+
"prepublishOnly": "yarn build"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"main": "./dist/resumon.umd.js",
|
|
41
|
+
"module": "./dist/resumon.es.js",
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"import": "./dist/resumon.es.js",
|
|
45
|
+
"require": "./dist/resumon.umd.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"types": "dist/types/src/main.d.ts",
|
|
49
|
+
"sideEffects": false,
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@commitlint/cli": "16.0.1",
|
|
52
|
+
"@commitlint/config-conventional": "16.0.0",
|
|
53
|
+
"@types/node": "16.11.17",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "5.8.1",
|
|
55
|
+
"@typescript-eslint/parser": "5.8.1",
|
|
56
|
+
"@vitest/ui": "0.0.127",
|
|
57
|
+
"@vue/component-compiler-utils": "3.3.0",
|
|
58
|
+
"@vuepress/plugin-register-components": "1.9.5",
|
|
59
|
+
"c8": "7.11.0",
|
|
60
|
+
"eslint": "8.6.0",
|
|
61
|
+
"eslint-config-prettier": "8.3.0",
|
|
62
|
+
"eslint-plugin-import": "2.25.4",
|
|
63
|
+
"eslint-plugin-prettier": "4.0.0",
|
|
64
|
+
"eslint-plugin-tsdoc": "0.2.14",
|
|
65
|
+
"eslint-plugin-unused-imports": "2.0.0",
|
|
66
|
+
"eslint-plugin-vue": "8.2.0",
|
|
67
|
+
"husky": "7.0.4",
|
|
68
|
+
"lint-staged": "12.1.4",
|
|
69
|
+
"prettier": "2.5.1",
|
|
70
|
+
"rimraf": "3.0.2",
|
|
71
|
+
"typescript": "4.5.4",
|
|
72
|
+
"vite": "2.7.10",
|
|
73
|
+
"vite-plugin-dts": "0.9.7",
|
|
74
|
+
"vitest": "0.0.125",
|
|
75
|
+
"vuepress": "2.0.0-beta.32"
|
|
76
|
+
},
|
|
77
|
+
"publishConfig": {
|
|
78
|
+
"access": "public"
|
|
79
|
+
},
|
|
80
|
+
"lint-staged": {
|
|
81
|
+
"*.{js,json,html,scss,ts,vue,yml}": "prettier --write",
|
|
82
|
+
"*.{js,json,ts,vue}": "eslint --fix"
|
|
83
|
+
}
|
|
84
|
+
}
|