@cobol-ts/errors 0.0.0-stage → 0.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/README.md CHANGED
@@ -1,3 +1,129 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # Error-Or Monad Utility
2
+
3
+ A lightweight TypeScript utility that provides an **ErrorsOr<T>** data type and helper functions for managing operations that can succeed with a value or fail with one or more errors.
4
+
5
+ Whether you’re familiar with functional “monad” patterns or new to them, this library offers:
6
+
7
+ * **Encapsulation** of successful values and error lists in a single type
8
+ * **Type-safe** unwrapping via `valueOrThrow` and `errorsOrThrow`
9
+ * **Defaulting** with `valueOrDefault` to avoid boilerplate checks
10
+ * **Mapping** and **flatMapping** (both sync and async) over values while preserving errors
11
+ * **Recovery** with fallback logic through `recover`
12
+ * **Partitioning** a record of named results into collected values and errors
13
+
14
+ ---
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @cobol-ts/errors-monad
20
+ # or
21
+ yarn add @cobol-ts/errors-monad
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Principle Types
27
+
28
+ These core types model either a successful result or a collection of errors:
29
+
30
+ ```ts
31
+ /**
32
+ * Represents either a successful value of type T or a set of errors.
33
+ * @template T - The type of the successful value.
34
+ */
35
+ export type ErrorsOr<T> = Errors | Value<T>;
36
+
37
+ /**
38
+ * Wraps a successful value.
39
+ * @template T - The type of the wrapped value.
40
+ */
41
+ export type Value<T> = { value: T };
42
+
43
+ /**
44
+ * Represents one or more error messages, with optional debugging metadata.
45
+ */
46
+ export type Errors = {
47
+ /** The list of error messages. */
48
+ errors: string[];
49
+ /** Optional reference identifier for debugging. */
50
+ reference?: string;
51
+ /** Optional extra context or metadata. */
52
+ extras?: any;
53
+ };
54
+ ```
55
+
56
+ ---
57
+
58
+ ## Working with `mapErrorsOr` & `flatMapErrorsOr`
59
+
60
+ These helpers let you compose operations on the successful value while automatically propagating errors:
61
+
62
+ ```ts
63
+ import {
64
+ createValue,
65
+ createErrors,
66
+ mapErrorsOr,
67
+ flatMapErrorsOr
68
+ } from '@cobol-ts/errors-monad';
69
+
70
+ // A synchronous 'square root' that errors on negative input
71
+ const safeSqrt = (n: number) =>
72
+ n < 0
73
+ ? createErrors([`Cannot sqrt negative number: ${n}`])
74
+ : createValue(Math.sqrt(n));
75
+
76
+ // 1) Start with a positive
77
+ const start = createValue(16);
78
+
79
+ // 2) Apply sqrt
80
+ const afterSqrt = flatMapErrorsOr(start, safeSqrt);
81
+ // -> { value: 4 }
82
+
83
+ // 3) Then add 3
84
+ const final = mapErrorsOr(afterSqrt, (x) => x + 3);
85
+ // -> { value: 7 }
86
+
87
+ // If any step errored, the errors would bubble through unchanged
88
+ ```
89
+
90
+ ---
91
+
92
+ ## Creating Errors
93
+
94
+ ```ts
95
+ import { makeErrorFromException } from '@cobol-ts/errors-monad';
96
+
97
+ const err = makeErrorFromException('parseJson', new Error('invalid'));
98
+ // -> { errors: ['parseJson error invalid'] }
99
+ ```
100
+
101
+ ---
102
+
103
+ ## Extracting Values & Errors
104
+
105
+ * `valueOrThrow(e: ErrorsOr<T>): T` — returns the value or throws an `ErrorsException`
106
+ * `errorsOrThrow(e: ErrorsOr<T>): string[]` — returns the errors array or throws if a value is present
107
+ * `valueOrDefault(e: ErrorsOr<T>, defaultVal: T): T` — returns the value or a default on errors/nullish
108
+
109
+ ---
110
+
111
+ ## Mapping Helpers
112
+
113
+ * `mapErrorsOr(e, fn)` — applies `fn` to the value, preserves errors
114
+ * `flatMapErrorsOr(e, fn)` — applies `fn` that returns `ErrorsOr<U>`, flattens
115
+ * `mapErrorsOrK(e, asyncFn)` — async version of `mapErrorsOr`
116
+ * `flatMapErrorsOrK(e, asyncFn)` — async version of `flatMapErrorsOr`
117
+
118
+ ---
119
+
120
+ ## Recovery & Partitioning
121
+
122
+ * `recover(e, fallbackFn)` — returns the value or a fallback based on errors
123
+ * `partitionNameAndErrorsOr(records)` — splits a `Record<string, ErrorsOr<T>>` into `{ values, errors }`
124
+
125
+ ---
126
+
127
+ ## Further Reading
128
+
129
+ See the JSDoc comments in [error.monad.ts](./src/error.monad.ts) for detailed API documentation.
@@ -0,0 +1,2 @@
1
+ export * from './src/error.monad';
2
+ export * from './src/error.report';
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
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("./src/error.monad"), exports);
18
+ __exportStar(require("./src/error.report"), exports);
19
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "@cobol-ts/errors",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "description": "How errors are represented",
4
+ "version": "0.0.1",
5
+ "main": "dist/index",
6
+ "types": "dist/index",
7
+ "browserslist": {
8
+ "development": [
9
+ "last 1 chrome version",
10
+ "last 1 firefox version",
11
+ "last 1 safari version"
12
+ ],
13
+ "production": [
14
+ ">0.2%",
15
+ "not dead",
16
+ "not op_mini all"
17
+ ]
18
+ },
19
+ "dependencies": {
20
+ "@cobol-ts/records": "0.0.1"
21
+ },
22
+ "devDependencies": {
23
+ "@testing-library/jest-dom": "^6.1.4",
24
+ "@types/jest": "^29.0.0",
25
+ "@types/node": "^14.0.0",
26
+ "jest": "^29.0.0",
27
+ "ts-jest": "^29.0.0",
28
+ "typescript": "^5.2.2"
29
+ },
30
+ "eslintConfig": {
31
+ "extends": [
32
+ "react-app",
33
+ "react-app/jest"
34
+ ],
35
+ "overrides": [
36
+ {
37
+ "files": [
38
+ "**/*.stories.*"
39
+ ],
40
+ "rules": {
41
+ "import/no-anonymous-default-export": "off"
42
+ }
43
+ }
44
+ ]
45
+ },
46
+ "files": [
47
+ "dist/index.js",
48
+ "dist/index.d.ts",
49
+ "dist/src/",
50
+ "build/"
51
+ ],
52
+ "keywords": [
53
+ "indexing"
54
+ ],
55
+ "license": "MIT",
56
+ "repository": "git@github.com:intellimaintain/intellimaintain.git",
57
+ "scripts": {
58
+ "test": "jest --config jest.config.json --env node",
59
+ "test:watch": "npm run test -- --watch",
60
+ "start": "react-scripts start"
61
+ }
6
62
  }