@resulted/results 0.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.md ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2026 Josh <josh@bashed.sh>
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,78 @@
1
+ # `@resulted/results`
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@resulted/results.svg)](https://www.npmjs.com/package/@resulted/results)
4
+ [![JSR](https://jsr.io/badges/@resulted/results)](https://jsr.io/@resulted/results)
5
+ [![ISC License](https://img.shields.io/badge/license-ISC-blue.svg)](LICENSE.md)
6
+
7
+ A TypeScript library for handling results and errors in a functional programming style.
8
+
9
+ ## Installation
10
+
11
+ NPM:
12
+ ```bash
13
+ npm install @resulted/results
14
+ yarn add @resulted/results
15
+ pnpm add @resulted/results
16
+ ```
17
+
18
+ JSR:
19
+ ```bash
20
+ deno add jsr:@resulted/results
21
+ pnpm add jsr:@resulted/results
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```typescript
27
+ import { Result } from '@resulted/results';
28
+
29
+ function divide(a: number, b: number): Result<number, 'ZeroDivisionError'> {
30
+ if (b === 0) {
31
+ return Result.err('ZeroDivisionError');
32
+ }
33
+ return Result.ok(a / b);
34
+ }
35
+
36
+ function doMath(): Result<number, 'DivisionError'> {
37
+ const randomNum = Math.floor(Math.random() * 10);
38
+ return divide(10, randomNum).mapErr(() => 'DivisionError');
39
+ }
40
+
41
+ function main() {
42
+ const result = doMath();
43
+ if (result.isOk()) {
44
+ console.log('Result:', result.value);
45
+ } else {
46
+ console.error('Error:', result.error);
47
+ }
48
+ }
49
+ ```
50
+
51
+ ## Contributing
52
+
53
+ Contributions are welcome! This project uses [pnpm](https://pnpm.io/) as its package manager.
54
+
55
+ ### Setup
56
+
57
+ ```bash
58
+ git clone https://github.com/resulted-io/results.git
59
+ cd results
60
+ pnpm install
61
+ ```
62
+
63
+ ### Scripts
64
+
65
+ - `pnpm run build`: Build CJS and ESM outputs
66
+ - `pnpm run typecheck`: Run TypeScript type checking
67
+ - `pnpm run lint`: Run Biome linter
68
+ - `pnpm run format`: Auto-fix lint and formatting issues
69
+
70
+ ### Submitting Changes
71
+
72
+ 1. Fork the repository and create a feature branch.
73
+ 2. Make your changes and ensure `pnpm run lint` and `pnpm run typecheck` pass.
74
+ 3. Open a pull request with a clear description of your changes.
75
+
76
+ ## License
77
+
78
+ This project is licensed under the ISC License. See [LICENSE.md](LICENSE.md) for details.
package/dist/index.cjs ADDED
@@ -0,0 +1,88 @@
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.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ Result: () => Result
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var resultProto = Object.freeze({
27
+ isErr() {
28
+ return this.type === "err";
29
+ },
30
+ isOk() {
31
+ return this.type === "ok";
32
+ },
33
+ map(fn) {
34
+ if (this.type === "ok")
35
+ return createResult({
36
+ type: "ok",
37
+ value: fn(this.value)
38
+ });
39
+ else
40
+ return createResult({ error: this.error, type: "err" });
41
+ },
42
+ mapErr(fn) {
43
+ if (this.type === "err")
44
+ return createResult({
45
+ error: fn(this.error),
46
+ type: "err"
47
+ });
48
+ else return createResult({ type: "ok", value: this.value });
49
+ }
50
+ });
51
+ var createResult = (res) => {
52
+ const obj = Object.create(resultProto);
53
+ if (res.type === "ok") {
54
+ obj.type = "ok";
55
+ obj.value = res.value;
56
+ } else {
57
+ obj.type = "err";
58
+ obj.error = res.error;
59
+ }
60
+ return obj;
61
+ };
62
+ var ok = (value) => createResult({ type: "ok", value });
63
+ var err = (error) => createResult({ error, type: "err" });
64
+ var trySync = (fn) => {
65
+ try {
66
+ return ok(fn());
67
+ } catch (e) {
68
+ return err(e);
69
+ }
70
+ };
71
+ var tryAsync = async (promise) => {
72
+ try {
73
+ const value = await promise;
74
+ return ok(value);
75
+ } catch (e) {
76
+ return err(e);
77
+ }
78
+ };
79
+ var Result = {
80
+ err,
81
+ ok,
82
+ try: tryAsync,
83
+ trySync
84
+ };
85
+ // Annotate the CommonJS export names for ESM import in node:
86
+ 0 && (module.exports = {
87
+ Result
88
+ });
@@ -0,0 +1,51 @@
1
+ /**
2
+ * An Ok type representing a successful result.
3
+ */
4
+ type OkBase<Ok> = {
5
+ type: 'ok';
6
+ value: Ok;
7
+ };
8
+ /**
9
+ * An Err type representing a failed result.
10
+ */
11
+ type ErrBase<Err> = {
12
+ type: 'err';
13
+ error: Err;
14
+ };
15
+ /**
16
+ * A simple ResultBase type to represent success or failure.
17
+ */
18
+ type ResultBase<Ok, Err> = OkBase<Ok> | ErrBase<Err>;
19
+ /**
20
+ * A Result type that includes utility methods.
21
+ */
22
+ type ResultProto<Ok, Err> = {
23
+ isOk(): this is {
24
+ type: 'ok';
25
+ value: Ok;
26
+ };
27
+ isErr(): this is {
28
+ type: 'err';
29
+ error: Err;
30
+ };
31
+ map<NewOk>(fn: (value: Ok) => NewOk): Result<NewOk, Err>;
32
+ mapErr<NewErr>(fn: (error: Err) => NewErr): Result<Ok, NewErr>;
33
+ };
34
+ /**
35
+ * A Result type that can represent either a successful value or an error.
36
+ *
37
+ * When the `type` is 'ok', the `value` property will contain the successful result.
38
+ * When the `type` is 'err', the `error` property will contain the error information.
39
+ */
40
+ type Result<Ok, Err> = (OkBase<Ok> & ResultProto<Ok, never>) | (ErrBase<Err> & ResultProto<never, Err>);
41
+ /**
42
+ * Result functions.
43
+ */
44
+ declare const Result: {
45
+ err: <Ok, Err>(error: Err) => Result<Ok, Err>;
46
+ ok: <Ok, Err>(value: Ok) => Result<Ok, Err>;
47
+ try: <Ok, Err = unknown>(promise: Promise<Ok>) => Promise<Result<Ok, Err>>;
48
+ trySync: <Ok, Err = unknown>(fn: () => Ok) => Result<Ok, Err>;
49
+ };
50
+
51
+ export { Result, type ResultBase, type ResultProto };
@@ -0,0 +1,51 @@
1
+ /**
2
+ * An Ok type representing a successful result.
3
+ */
4
+ type OkBase<Ok> = {
5
+ type: 'ok';
6
+ value: Ok;
7
+ };
8
+ /**
9
+ * An Err type representing a failed result.
10
+ */
11
+ type ErrBase<Err> = {
12
+ type: 'err';
13
+ error: Err;
14
+ };
15
+ /**
16
+ * A simple ResultBase type to represent success or failure.
17
+ */
18
+ type ResultBase<Ok, Err> = OkBase<Ok> | ErrBase<Err>;
19
+ /**
20
+ * A Result type that includes utility methods.
21
+ */
22
+ type ResultProto<Ok, Err> = {
23
+ isOk(): this is {
24
+ type: 'ok';
25
+ value: Ok;
26
+ };
27
+ isErr(): this is {
28
+ type: 'err';
29
+ error: Err;
30
+ };
31
+ map<NewOk>(fn: (value: Ok) => NewOk): Result<NewOk, Err>;
32
+ mapErr<NewErr>(fn: (error: Err) => NewErr): Result<Ok, NewErr>;
33
+ };
34
+ /**
35
+ * A Result type that can represent either a successful value or an error.
36
+ *
37
+ * When the `type` is 'ok', the `value` property will contain the successful result.
38
+ * When the `type` is 'err', the `error` property will contain the error information.
39
+ */
40
+ type Result<Ok, Err> = (OkBase<Ok> & ResultProto<Ok, never>) | (ErrBase<Err> & ResultProto<never, Err>);
41
+ /**
42
+ * Result functions.
43
+ */
44
+ declare const Result: {
45
+ err: <Ok, Err>(error: Err) => Result<Ok, Err>;
46
+ ok: <Ok, Err>(value: Ok) => Result<Ok, Err>;
47
+ try: <Ok, Err = unknown>(promise: Promise<Ok>) => Promise<Result<Ok, Err>>;
48
+ trySync: <Ok, Err = unknown>(fn: () => Ok) => Result<Ok, Err>;
49
+ };
50
+
51
+ export { Result, type ResultBase, type ResultProto };
package/dist/index.js ADDED
@@ -0,0 +1,63 @@
1
+ // src/index.ts
2
+ var resultProto = Object.freeze({
3
+ isErr() {
4
+ return this.type === "err";
5
+ },
6
+ isOk() {
7
+ return this.type === "ok";
8
+ },
9
+ map(fn) {
10
+ if (this.type === "ok")
11
+ return createResult({
12
+ type: "ok",
13
+ value: fn(this.value)
14
+ });
15
+ else
16
+ return createResult({ error: this.error, type: "err" });
17
+ },
18
+ mapErr(fn) {
19
+ if (this.type === "err")
20
+ return createResult({
21
+ error: fn(this.error),
22
+ type: "err"
23
+ });
24
+ else return createResult({ type: "ok", value: this.value });
25
+ }
26
+ });
27
+ var createResult = (res) => {
28
+ const obj = Object.create(resultProto);
29
+ if (res.type === "ok") {
30
+ obj.type = "ok";
31
+ obj.value = res.value;
32
+ } else {
33
+ obj.type = "err";
34
+ obj.error = res.error;
35
+ }
36
+ return obj;
37
+ };
38
+ var ok = (value) => createResult({ type: "ok", value });
39
+ var err = (error) => createResult({ error, type: "err" });
40
+ var trySync = (fn) => {
41
+ try {
42
+ return ok(fn());
43
+ } catch (e) {
44
+ return err(e);
45
+ }
46
+ };
47
+ var tryAsync = async (promise) => {
48
+ try {
49
+ const value = await promise;
50
+ return ok(value);
51
+ } catch (e) {
52
+ return err(e);
53
+ }
54
+ };
55
+ var Result = {
56
+ err,
57
+ ok,
58
+ try: tryAsync,
59
+ trySync
60
+ };
61
+ export {
62
+ Result
63
+ };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "author": "Josh <josh@bashed.sh>",
3
+ "description": "A TypeScript library for handling results and errors in a functional programming style.",
4
+ "devDependencies": {
5
+ "@biomejs/biome": "^2.4.4",
6
+ "tsup": "^8.5.1",
7
+ "typescript": "^5.9.3"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "default": "./dist/index.js",
13
+ "types": "./dist/index.d.ts"
14
+ },
15
+ "require": {
16
+ "default": "./dist/index.cjs",
17
+ "types": "./dist/index.d.cts"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "src",
24
+ "LICENSE.md"
25
+ ],
26
+ "keywords": [
27
+ "result",
28
+ "error-handling",
29
+ "typescript",
30
+ "functional",
31
+ "monad"
32
+ ],
33
+ "license": "ISC",
34
+ "main": "dist/index.cjs",
35
+ "module": "dist/index.js",
36
+ "name": "@resulted/results",
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/JoshBashed/resulted-results.git"
43
+ },
44
+ "type": "module",
45
+ "types": "dist/index.d.ts",
46
+ "version": "0.0.0",
47
+ "scripts": {
48
+ "build": "tsup src/index.ts --format esm,cjs --dts",
49
+ "format": "biome check --write",
50
+ "lint": "biome check",
51
+ "typecheck": "tsc --noEmit"
52
+ }
53
+ }
package/src/index.ts ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * An Ok type representing a successful result.
3
+ */
4
+ type OkBase<Ok> = { type: 'ok'; value: Ok };
5
+
6
+ /**
7
+ * An Err type representing a failed result.
8
+ */
9
+ type ErrBase<Err> = { type: 'err'; error: Err };
10
+
11
+ /**
12
+ * A simple ResultBase type to represent success or failure.
13
+ */
14
+ export type ResultBase<Ok, Err> = OkBase<Ok> | ErrBase<Err>;
15
+
16
+ /**
17
+ * A Result type that can represent either a successful value or an error.
18
+ *
19
+ * When the `type` is 'ok', the `value` property will contain the successful result.
20
+ * When the `type` is 'err', the `error` property will contain the error information.
21
+ */
22
+ export type Result<Ok, Err> =
23
+ | (OkBase<Ok> & ResultProto<Ok, never>)
24
+ | (ErrBase<Err> & ResultProto<never, Err>);
25
+
26
+ /**
27
+ * A Result type that includes utility methods.
28
+ */
29
+ export type ResultProto<Ok, Err> = {
30
+ isOk(): this is { type: 'ok'; value: Ok };
31
+ isErr(): this is { type: 'err'; error: Err };
32
+ map<NewOk>(fn: (value: Ok) => NewOk): Result<NewOk, Err>;
33
+ mapErr<NewErr>(fn: (error: Err) => NewErr): Result<Ok, NewErr>;
34
+ };
35
+
36
+ const resultProto = Object.freeze({
37
+ isErr<Ok, Err>(this: Result<Ok, Err>): this is { type: 'err'; error: Err } {
38
+ return this.type === 'err';
39
+ },
40
+ isOk<Ok, Err>(this: Result<Ok, Err>): this is { type: 'ok'; value: Ok } {
41
+ return this.type === 'ok';
42
+ },
43
+ map<Ok, Err, NewOk>(
44
+ this: Result<Ok, Err>,
45
+ fn: (value: Ok) => NewOk,
46
+ ): Result<NewOk, Err> {
47
+ if (this.type === 'ok')
48
+ return createResult<NewOk, Err>({
49
+ type: 'ok',
50
+ value: fn(this.value),
51
+ });
52
+ else
53
+ return createResult<NewOk, Err>({ error: this.error, type: 'err' });
54
+ },
55
+ mapErr<Ok, Err, NewErr>(
56
+ this: Result<Ok, Err>,
57
+ fn: (error: Err) => NewErr,
58
+ ): Result<Ok, NewErr> {
59
+ if (this.type === 'err')
60
+ return createResult<Ok, NewErr>({
61
+ error: fn(this.error),
62
+ type: 'err',
63
+ });
64
+ else return createResult<Ok, NewErr>({ type: 'ok', value: this.value });
65
+ },
66
+ });
67
+
68
+ /**
69
+ * Helper function to create a Result object with the appropriate prototype.
70
+ * @param res The base result object containing either an Ok value or an Err error.
71
+ * @returns A Result object with utility methods attached.
72
+ */
73
+ const createResult = <Ok, Err>(res: ResultBase<Ok, Err>): Result<Ok, Err> => {
74
+ const obj = Object.create(resultProto) as Result<Ok, Err>;
75
+ if (res.type === 'ok') {
76
+ obj.type = 'ok';
77
+ (obj as OkBase<Ok>).value = res.value;
78
+ } else {
79
+ obj.type = 'err';
80
+ (obj as ErrBase<Err>).error = res.error;
81
+ }
82
+ return obj;
83
+ };
84
+
85
+ /**
86
+ * Create an Ok result.
87
+ * @param value The value to wrap in an Ok result.
88
+ * @returns A Result object representing success.
89
+ */
90
+ const ok = <Ok, Err>(value: Ok): Result<Ok, Err> =>
91
+ createResult({ type: 'ok', value });
92
+
93
+ /**
94
+ * Create an Err result.
95
+ * @param error The error to wrap in an Err result.
96
+ * @returns A Result object representing failure.
97
+ */
98
+ const err = <Ok, Err>(error: Err): Result<Ok, Err> =>
99
+ createResult({ error, type: 'err' });
100
+
101
+ /**
102
+ * Execute a synchronous function and capture its result as a Result type.
103
+ * @param fn The function to execute, which may throw an error.
104
+ * @returns The result of Ok or Err.
105
+ */
106
+ const trySync = <Ok, Err = unknown>(fn: () => Ok): Result<Ok, Err> => {
107
+ try {
108
+ return ok<Ok, Err>(fn());
109
+ } catch (e) {
110
+ return err<Ok, Err>(e as Err);
111
+ }
112
+ };
113
+
114
+ /**
115
+ * Execute an asynchronous function and capture its result as a Result type.
116
+ * @param promise The promise to execute, which may reject with an error.
117
+ * @returns The result of Ok or Err depending.
118
+ */
119
+ const tryAsync = async <Ok, Err = unknown>(
120
+ promise: Promise<Ok>,
121
+ ): Promise<Result<Ok, Err>> => {
122
+ try {
123
+ const value = await promise;
124
+ return ok<Ok, Err>(value);
125
+ } catch (e) {
126
+ return err<Ok, Err>(e as Err);
127
+ }
128
+ };
129
+
130
+ /**
131
+ * Result functions.
132
+ */
133
+ export const Result = {
134
+ err,
135
+ ok,
136
+ try: tryAsync,
137
+ trySync,
138
+ };