@j2blasco/ts-result 0.0.7 → 0.0.8
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 +20 -20
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/result-functions.js +10 -0
- package/dist/cjs/result.js +95 -0
- package/dist/cjs/utils/test/assert-type.js +8 -0
- package/package.json +39 -36
- package/dist/result.test.d.ts +0 -1
- package/dist/result.test.js +0 -55
- /package/dist/{index.js → esm/index.js} +0 -0
- /package/dist/{result-functions.js → esm/result-functions.js} +0 -0
- /package/dist/{result.js → esm/result.js} +0 -0
- /package/dist/{utils → esm/utils}/test/assert-type.js +0 -0
- /package/{dist → types}/index.d.ts +0 -0
- /package/{dist → types}/result-functions.d.ts +0 -0
- /package/{dist → types}/result.d.ts +0 -0
- /package/{dist → types}/utils/test/assert-type.d.ts +0 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 [Your Name]
|
|
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
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 [Your Name]
|
|
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
21
|
SOFTWARE.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./result"), exports);
|
|
18
|
+
__exportStar(require("./result-functions"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.andThen = andThen;
|
|
4
|
+
exports.andThenAsync = andThenAsync;
|
|
5
|
+
function andThen(callback) {
|
|
6
|
+
return (result) => result.andThen(callback);
|
|
7
|
+
}
|
|
8
|
+
function andThenAsync(callback) {
|
|
9
|
+
return (result) => result.andThenAsync(callback);
|
|
10
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resultError = void 0;
|
|
4
|
+
exports.unwrapSuccessResult = unwrapSuccessResult;
|
|
5
|
+
exports.errorWithCode = errorWithCode;
|
|
6
|
+
exports.errorUnkown = errorUnkown;
|
|
7
|
+
exports.resultSuccessVoid = resultSuccessVoid;
|
|
8
|
+
exports.resultSuccess = resultSuccess;
|
|
9
|
+
exports.makeSafePromise = makeSafePromise;
|
|
10
|
+
function unwrapSuccessResult(result) {
|
|
11
|
+
if (result instanceof ResultSuccessImp) {
|
|
12
|
+
return result['value'];
|
|
13
|
+
}
|
|
14
|
+
throw new Error('Unreachable code. Result is an error');
|
|
15
|
+
}
|
|
16
|
+
function errorWithCode(code, data) {
|
|
17
|
+
return {
|
|
18
|
+
code,
|
|
19
|
+
data,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function errorUnkown(message) {
|
|
23
|
+
return {
|
|
24
|
+
code: 'unknown',
|
|
25
|
+
data: { message },
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function resultSuccessVoid() {
|
|
29
|
+
return new ResultSuccessImp(undefined);
|
|
30
|
+
}
|
|
31
|
+
function resultSuccess(value) {
|
|
32
|
+
return new ResultSuccessImp(value);
|
|
33
|
+
}
|
|
34
|
+
class resultError {
|
|
35
|
+
static unknown(message) {
|
|
36
|
+
return new ResultErrorImp(errorUnkown(message));
|
|
37
|
+
}
|
|
38
|
+
static withCode(code, data = {}) {
|
|
39
|
+
return new ResultErrorImp(errorWithCode(code, data));
|
|
40
|
+
}
|
|
41
|
+
static fromError(error) {
|
|
42
|
+
return new ResultErrorImp(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.resultError = resultError;
|
|
46
|
+
class ResultSuccessImp {
|
|
47
|
+
value;
|
|
48
|
+
constructor(value) {
|
|
49
|
+
this.value = value;
|
|
50
|
+
}
|
|
51
|
+
andThen(f) {
|
|
52
|
+
return f(this.value);
|
|
53
|
+
}
|
|
54
|
+
async andThenAsync(f) {
|
|
55
|
+
return await f(this.value);
|
|
56
|
+
}
|
|
57
|
+
catchError(_f) {
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
async catchErrorAsync(_f) {
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
unwrapOrThrow(_errorCallback) {
|
|
64
|
+
return this.value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
class ResultErrorImp {
|
|
68
|
+
error;
|
|
69
|
+
constructor(error) {
|
|
70
|
+
this.error = error;
|
|
71
|
+
}
|
|
72
|
+
andThen(_f) {
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
async andThenAsync(_f) {
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
catchError(f) {
|
|
79
|
+
return f(this.error);
|
|
80
|
+
}
|
|
81
|
+
async catchErrorAsync(f) {
|
|
82
|
+
return await f(this.error);
|
|
83
|
+
}
|
|
84
|
+
unwrapOrThrow(errorCallback) {
|
|
85
|
+
if (errorCallback !== undefined) {
|
|
86
|
+
errorCallback(this.error);
|
|
87
|
+
}
|
|
88
|
+
throw this.error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function makeSafePromise(promise) {
|
|
92
|
+
return promise
|
|
93
|
+
.then((value) => resultSuccess(value))
|
|
94
|
+
.catch((error) => resultError.unknown(error.message));
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@j2blasco/ts-result",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Result Monad for TypeScript",
|
|
5
|
-
"
|
|
6
|
-
"types": "
|
|
7
|
-
"exports": {
|
|
8
|
-
".": {
|
|
9
|
-
"types": "./
|
|
10
|
-
"import": "./dist/index.js",
|
|
11
|
-
"require": "./dist/index.js"
|
|
12
|
-
}
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
"dist",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"scripts": {
|
|
20
|
-
"test": "jest",
|
|
21
|
-
"build": "
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
}
|
|
36
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@j2blasco/ts-result",
|
|
3
|
+
"version": "0.0.8",
|
|
4
|
+
"description": "Result Monad for TypeScript", "main": "./dist/cjs/index.js",
|
|
5
|
+
"module": "./dist/esm/index.js",
|
|
6
|
+
"types": "./types/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
10
|
+
"import": "./dist/esm/index.js",
|
|
11
|
+
"require": "./dist/cjs/index.js"
|
|
12
|
+
}
|
|
13
|
+
}, "files": [
|
|
14
|
+
"dist/cjs",
|
|
15
|
+
"dist/esm",
|
|
16
|
+
"types",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
], "scripts": {
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
22
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
23
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
24
|
+
"clean": "rimraf dist types"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [],
|
|
27
|
+
"author": "",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"type": "module", "devDependencies": {
|
|
30
|
+
"@j2blasco/ts-pipe": "^0.0.4",
|
|
31
|
+
"@types/jest": "^29.5.14",
|
|
32
|
+
"jest": "^29.7.0",
|
|
33
|
+
"rimraf": "^5.0.0",
|
|
34
|
+
"ts-jest": "^29.3.2"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/dist/result.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/result.test.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { resultSuccess, resultError, unwrapSuccessResult, resultSuccessVoid, } from "./result";
|
|
2
|
-
import { assertType, expectToCompile } from "./utils/test/assert-type";
|
|
3
|
-
function createResult() {
|
|
4
|
-
return resultSuccessVoid();
|
|
5
|
-
}
|
|
6
|
-
describe("Result Monad", () => {
|
|
7
|
-
test("resultSuccess should create a success result", () => {
|
|
8
|
-
const success = resultSuccess("test");
|
|
9
|
-
expect(unwrapSuccessResult(success)).toBe("test");
|
|
10
|
-
});
|
|
11
|
-
test("resultError.unknown should create an unknown error result", () => {
|
|
12
|
-
const error = resultError.unknown("An error occurred");
|
|
13
|
-
try {
|
|
14
|
-
error.unwrapOrThrow();
|
|
15
|
-
}
|
|
16
|
-
catch (e) {
|
|
17
|
-
expect(e).toMatchObject({
|
|
18
|
-
code: "unknown",
|
|
19
|
-
data: { message: "An error occurred" },
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
test("resultError.withCode should create a coded error result", () => {
|
|
24
|
-
const error = resultError.withCode("test-code", {
|
|
25
|
-
detail: "Some details",
|
|
26
|
-
});
|
|
27
|
-
try {
|
|
28
|
-
error.unwrapOrThrow();
|
|
29
|
-
}
|
|
30
|
-
catch (e) {
|
|
31
|
-
expect(e).toMatchObject({
|
|
32
|
-
code: "test-code",
|
|
33
|
-
data: { detail: "Some details" },
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
test("Result Monad success should be chainable with inferred types - string -> int", () => {
|
|
38
|
-
const resultString = createResult();
|
|
39
|
-
const resultInt = resultString.andThen((value) => {
|
|
40
|
-
assertType(true);
|
|
41
|
-
return resultSuccess(42);
|
|
42
|
-
});
|
|
43
|
-
assertType(true);
|
|
44
|
-
expectToCompile();
|
|
45
|
-
});
|
|
46
|
-
test("Result Monad success should be chainable with inferred types - obj -> obj", () => {
|
|
47
|
-
const resultString = createResult();
|
|
48
|
-
const resultInt = resultString.andThen((value) => {
|
|
49
|
-
assertType(true);
|
|
50
|
-
return resultSuccess({ test2: "test" });
|
|
51
|
-
});
|
|
52
|
-
assertType(true);
|
|
53
|
-
expectToCompile();
|
|
54
|
-
});
|
|
55
|
-
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|