generic-result-type 1.0.0
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.txt +15 -0
- package/README.md +38 -0
- package/dist/index.cjs +76 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dave Welsh
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# generic-result-type
|
|
2
|
+
|
|
3
|
+
A small, dependency free Result type for TypeScript and JavaScript.
|
|
4
|
+
|
|
5
|
+
It provides a simple way to represent success or failure values without throwing exceptions.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i generic-result-type
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Module support
|
|
14
|
+
|
|
15
|
+
This package supports *both ESM and CommonJS* out of the box.
|
|
16
|
+
|
|
17
|
+
ESM:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Result } from "generic-result-type"
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
CommonJS:
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const { Result } = require("generic-result-type");
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
TypeScript typings are included.
|
|
30
|
+
|
|
31
|
+
## No dependencies
|
|
32
|
+
|
|
33
|
+
This package has zero runtime dependencies.
|
|
34
|
+
It does not rely on any external libraries.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
ISC
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.mts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Result: () => Result,
|
|
24
|
+
isErrorResult: () => isErrorResult,
|
|
25
|
+
isSuccessResult: () => isSuccessResult
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/errorResult.mts
|
|
30
|
+
var ErrorResult = class {
|
|
31
|
+
constructor(error) {
|
|
32
|
+
this.error = error;
|
|
33
|
+
this.success = false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/successResult.mts
|
|
38
|
+
var SuccessResult = class {
|
|
39
|
+
constructor(value) {
|
|
40
|
+
this.value = value;
|
|
41
|
+
this.success = true;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/result.mts
|
|
46
|
+
var Result = class _Result {
|
|
47
|
+
static success(value) {
|
|
48
|
+
return new SuccessResult(value);
|
|
49
|
+
}
|
|
50
|
+
static fail(error) {
|
|
51
|
+
return new ErrorResult(error);
|
|
52
|
+
}
|
|
53
|
+
static combine(results) {
|
|
54
|
+
for (const result of results) {
|
|
55
|
+
if (!result.success) {
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return _Result.success(void 0);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/index.mts
|
|
64
|
+
var isSuccessResult = (result) => {
|
|
65
|
+
return result.success;
|
|
66
|
+
};
|
|
67
|
+
var isErrorResult = (result) => {
|
|
68
|
+
return !result.success;
|
|
69
|
+
};
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
Result,
|
|
73
|
+
isErrorResult,
|
|
74
|
+
isSuccessResult
|
|
75
|
+
});
|
|
76
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.mts","../src/errorResult.mts","../src/successResult.mts","../src/result.mts"],"sourcesContent":["import type { IErrorResult } from './errorResult.mjs';\nimport type { ISuccessResult } from './successResult.mjs';\n\nexport { Result } from './result.mjs';\nexport type { ISuccessResult };\nexport type { IErrorResult };\n\nexport type ResultType<T> = ISuccessResult<T> | IErrorResult;\n\n// eslint-disable-next-line @stylistic/comma-dangle\nexport const isSuccessResult = <T,>(result: ResultType<T>): result is ISuccessResult<T> => {\n return result.success;\n};\n\n// eslint-disable-next-line @stylistic/comma-dangle\nexport const isErrorResult = <T,>(result: ResultType<T>): result is IErrorResult => {\n return !result.success;\n};\n","export interface IErrorResult {\n readonly success: false;\n readonly error: Error;\n}\n\nexport class ErrorResult implements IErrorResult {\n public readonly success = false as const;\n\n public constructor(public readonly error: Error) { /* empty */ }\n}\n","export interface ISuccessResult<T> {\n readonly success: true;\n readonly value: T;\n}\n\nexport class SuccessResult<T> implements ISuccessResult<T> {\n public readonly success = true as const;\n\n public constructor(public readonly value: T) { /* empty */ }\n}\n","import type { IErrorResult } from './errorResult.mjs';\nimport { ErrorResult } from './errorResult.mjs';\nimport type { ResultType } from './index.mjs';\nimport type { ISuccessResult } from './successResult.mjs';\nimport { SuccessResult } from './successResult.mjs';\n\n// eslint-disable-next-line @typescript-eslint/no-extraneous-class\nexport abstract class Result {\n\n public static success<T>(value: T): ISuccessResult<T> {\n return new SuccessResult(value);\n }\n\n public static fail(error: Error): IErrorResult {\n return new ErrorResult(error);\n }\n\n public static combine<T>(results: ResultType<T>[]): ResultType<T | undefined> {\n for (const result of results) {\n if (!result.success) {\n return result;\n }\n }\n return Result.success<undefined>(undefined);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKO,IAAM,cAAN,MAA0C;AAAA,EAGxC,YAA4B,OAAc;AAAd;AAFnC,SAAgB,UAAU;AAAA,EAEqC;AACjE;;;ACJO,IAAM,gBAAN,MAAoD;AAAA,EAGlD,YAA4B,OAAU;AAAV;AAFnC,SAAgB,UAAU;AAAA,EAEiC;AAC7D;;;ACFO,IAAe,SAAf,MAAe,QAAO;AAAA,EAE3B,OAAc,QAAW,OAA6B;AACpD,WAAO,IAAI,cAAc,KAAK;AAAA,EAChC;AAAA,EAEA,OAAc,KAAK,OAA4B;AAC7C,WAAO,IAAI,YAAY,KAAK;AAAA,EAC9B;AAAA,EAEA,OAAc,QAAW,SAAqD;AAC5E,eAAW,UAAU,SAAS;AAC5B,UAAI,CAAC,OAAO,SAAS;AACnB,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO,QAAO,QAAmB,MAAS;AAAA,EAC5C;AACF;;;AHfO,IAAM,kBAAkB,CAAK,WAAuD;AACzF,SAAO,OAAO;AAChB;AAGO,IAAM,gBAAgB,CAAK,WAAkD;AAClF,SAAO,CAAC,OAAO;AACjB;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface IErrorResult {
|
|
2
|
+
readonly success: false;
|
|
3
|
+
readonly error: Error;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface ISuccessResult<T> {
|
|
7
|
+
readonly success: true;
|
|
8
|
+
readonly value: T;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare abstract class Result {
|
|
12
|
+
static success<T>(value: T): ISuccessResult<T>;
|
|
13
|
+
static fail(error: Error): IErrorResult;
|
|
14
|
+
static combine<T>(results: ResultType<T>[]): ResultType<T | undefined>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ResultType<T> = ISuccessResult<T> | IErrorResult;
|
|
18
|
+
declare const isSuccessResult: <T>(result: ResultType<T>) => result is ISuccessResult<T>;
|
|
19
|
+
declare const isErrorResult: <T>(result: ResultType<T>) => result is IErrorResult;
|
|
20
|
+
|
|
21
|
+
export { type IErrorResult, type ISuccessResult, Result, type ResultType, isErrorResult, isSuccessResult };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
interface IErrorResult {
|
|
2
|
+
readonly success: false;
|
|
3
|
+
readonly error: Error;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface ISuccessResult<T> {
|
|
7
|
+
readonly success: true;
|
|
8
|
+
readonly value: T;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare abstract class Result {
|
|
12
|
+
static success<T>(value: T): ISuccessResult<T>;
|
|
13
|
+
static fail(error: Error): IErrorResult;
|
|
14
|
+
static combine<T>(results: ResultType<T>[]): ResultType<T | undefined>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ResultType<T> = ISuccessResult<T> | IErrorResult;
|
|
18
|
+
declare const isSuccessResult: <T>(result: ResultType<T>) => result is ISuccessResult<T>;
|
|
19
|
+
declare const isErrorResult: <T>(result: ResultType<T>) => result is IErrorResult;
|
|
20
|
+
|
|
21
|
+
export { type IErrorResult, type ISuccessResult, Result, type ResultType, isErrorResult, isSuccessResult };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/errorResult.mts
|
|
2
|
+
var ErrorResult = class {
|
|
3
|
+
constructor(error) {
|
|
4
|
+
this.error = error;
|
|
5
|
+
this.success = false;
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// src/successResult.mts
|
|
10
|
+
var SuccessResult = class {
|
|
11
|
+
constructor(value) {
|
|
12
|
+
this.value = value;
|
|
13
|
+
this.success = true;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/result.mts
|
|
18
|
+
var Result = class _Result {
|
|
19
|
+
static success(value) {
|
|
20
|
+
return new SuccessResult(value);
|
|
21
|
+
}
|
|
22
|
+
static fail(error) {
|
|
23
|
+
return new ErrorResult(error);
|
|
24
|
+
}
|
|
25
|
+
static combine(results) {
|
|
26
|
+
for (const result of results) {
|
|
27
|
+
if (!result.success) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return _Result.success(void 0);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/index.mts
|
|
36
|
+
var isSuccessResult = (result) => {
|
|
37
|
+
return result.success;
|
|
38
|
+
};
|
|
39
|
+
var isErrorResult = (result) => {
|
|
40
|
+
return !result.success;
|
|
41
|
+
};
|
|
42
|
+
export {
|
|
43
|
+
Result,
|
|
44
|
+
isErrorResult,
|
|
45
|
+
isSuccessResult
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errorResult.mts","../src/successResult.mts","../src/result.mts","../src/index.mts"],"sourcesContent":["export interface IErrorResult {\n readonly success: false;\n readonly error: Error;\n}\n\nexport class ErrorResult implements IErrorResult {\n public readonly success = false as const;\n\n public constructor(public readonly error: Error) { /* empty */ }\n}\n","export interface ISuccessResult<T> {\n readonly success: true;\n readonly value: T;\n}\n\nexport class SuccessResult<T> implements ISuccessResult<T> {\n public readonly success = true as const;\n\n public constructor(public readonly value: T) { /* empty */ }\n}\n","import type { IErrorResult } from './errorResult.mjs';\nimport { ErrorResult } from './errorResult.mjs';\nimport type { ResultType } from './index.mjs';\nimport type { ISuccessResult } from './successResult.mjs';\nimport { SuccessResult } from './successResult.mjs';\n\n// eslint-disable-next-line @typescript-eslint/no-extraneous-class\nexport abstract class Result {\n\n public static success<T>(value: T): ISuccessResult<T> {\n return new SuccessResult(value);\n }\n\n public static fail(error: Error): IErrorResult {\n return new ErrorResult(error);\n }\n\n public static combine<T>(results: ResultType<T>[]): ResultType<T | undefined> {\n for (const result of results) {\n if (!result.success) {\n return result;\n }\n }\n return Result.success<undefined>(undefined);\n }\n}\n","import type { IErrorResult } from './errorResult.mjs';\nimport type { ISuccessResult } from './successResult.mjs';\n\nexport { Result } from './result.mjs';\nexport type { ISuccessResult };\nexport type { IErrorResult };\n\nexport type ResultType<T> = ISuccessResult<T> | IErrorResult;\n\n// eslint-disable-next-line @stylistic/comma-dangle\nexport const isSuccessResult = <T,>(result: ResultType<T>): result is ISuccessResult<T> => {\n return result.success;\n};\n\n// eslint-disable-next-line @stylistic/comma-dangle\nexport const isErrorResult = <T,>(result: ResultType<T>): result is IErrorResult => {\n return !result.success;\n};\n"],"mappings":";AAKO,IAAM,cAAN,MAA0C;AAAA,EAGxC,YAA4B,OAAc;AAAd;AAFnC,SAAgB,UAAU;AAAA,EAEqC;AACjE;;;ACJO,IAAM,gBAAN,MAAoD;AAAA,EAGlD,YAA4B,OAAU;AAAV;AAFnC,SAAgB,UAAU;AAAA,EAEiC;AAC7D;;;ACFO,IAAe,SAAf,MAAe,QAAO;AAAA,EAE3B,OAAc,QAAW,OAA6B;AACpD,WAAO,IAAI,cAAc,KAAK;AAAA,EAChC;AAAA,EAEA,OAAc,KAAK,OAA4B;AAC7C,WAAO,IAAI,YAAY,KAAK;AAAA,EAC9B;AAAA,EAEA,OAAc,QAAW,SAAqD;AAC5E,eAAW,UAAU,SAAS;AAC5B,UAAI,CAAC,OAAO,SAAS;AACnB,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO,QAAO,QAAmB,MAAS;AAAA,EAC5C;AACF;;;ACfO,IAAM,kBAAkB,CAAK,WAAuD;AACzF,SAAO,OAAO;AAChB;AAGO,IAAM,gBAAgB,CAAK,WAAkD;AAClF,SAAO,CAAC,OAAO;AACjB;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "generic-result-type",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A generic result wrapper",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "Dave Welsh",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/move-zig/generic-result-type.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"require": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"typecheck": "tsc -p tsconfig.build.json --noEmit",
|
|
32
|
+
"test": "jest --verbose",
|
|
33
|
+
"test:watch": "jest --verbose --watch",
|
|
34
|
+
"lint": "eslint src"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@eslint/compat": "^2.0.0",
|
|
38
|
+
"@eslint/js": "^9.39.2",
|
|
39
|
+
"@faker-js/faker": "^10.2.0",
|
|
40
|
+
"@stylistic/eslint-plugin": "^5.6.1",
|
|
41
|
+
"@swc/core": "^1.15.8",
|
|
42
|
+
"@swc/jest": "^0.2.39",
|
|
43
|
+
"@types/jest": "^30.0.0",
|
|
44
|
+
"@types/node": "^24.10.4",
|
|
45
|
+
"eslint": "^9.39.2",
|
|
46
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
47
|
+
"eslint-plugin-import": "^2.32.0",
|
|
48
|
+
"eslint-plugin-jest": "^29.12.1",
|
|
49
|
+
"jest": "^30.2.0",
|
|
50
|
+
"jiti": "^2.6.1",
|
|
51
|
+
"tsup": "^8.5.1",
|
|
52
|
+
"tsx": "^4.21.0",
|
|
53
|
+
"typescript": "^5.9.3",
|
|
54
|
+
"typescript-eslint": "^8.52.0"
|
|
55
|
+
}
|
|
56
|
+
}
|