@guillaume-docquier/tools-ts 7.3.2 → 7.4.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/README.md CHANGED
@@ -13,16 +13,16 @@ modules contain the deeper API examples and implementation notes.
13
13
  Use these tools when code needs to distinguish expected failures from broken
14
14
  program assumptions.
15
15
 
16
- | Export | What it is | Use it when |
17
- | ------------------------------ | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
18
- | `Result`, `Success`, `Failure` | A typed success/failure return shape plus helper constructors and guards. | You want expected failures to be returned and handled explicitly instead of thrown. Prefer this for recoverable application outcomes. |
19
- | `Result.tryCatch` | A wrapper that converts thrown or rejected non-fatal errors into `Failure`. | You are calling code that may throw, especially third-party APIs, and want to bring it back into the `Result` flow. |
20
- | `Assert` | Runtime assertion helpers that throw `AssertionError` and narrow TypeScript types. | TypeScript cannot express an invariant strongly enough, or test code needs a clear fatal assertion. |
21
- | `AssertionError` | A fatal error for violated assumptions, with structured context. | A program invariant is broken and continuing would hide a bug or corrupt later state. Usually use `Assert` unless you need a custom assertion. |
22
- | `FatalError` | Base fatal error class with an `isFatal` flag and structured context. | An error should crash or escape normal expected-error handling. Extend this for domain-specific fatal errors. |
23
- | `Rethrow` | Helpers for catch blocks that should not swallow fatal errors. | You catch `unknown` but only intend to handle recoverable `Error` instances. |
24
- | `NotImplementedError` | A fatal placeholder error with tracking context. | A code path intentionally exists before the feature is implemented, and the missing work should be traceable. |
25
- | `isNodeJSError` | Type guard for Node-style `ErrnoException` properties on `Error`. | You need to inspect fields like `code`, `path`, `errno`, or `syscall` after catching a Node.js error. |
16
+ | Export | What it is | Use it when |
17
+ | ------------------------------ | ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
18
+ | `Result`, `Success`, `Failure` | A typed success/failure return shape plus helper constructors, guards, and mapping. | You want expected failures to be returned and handled explicitly instead of thrown. Prefer this for recoverable application outcomes. |
19
+ | `Result.tryCatch` | A wrapper that converts thrown or rejected non-fatal errors into `Failure`. | You are calling code that may throw, especially third-party APIs, and want to bring it back into the `Result` flow. |
20
+ | `Assert` | Runtime assertion helpers that throw `AssertionError` and narrow TypeScript types. | TypeScript cannot express an invariant strongly enough, or test code needs a clear fatal assertion. |
21
+ | `AssertionError` | A fatal error for violated assumptions, with structured context. | A program invariant is broken and continuing would hide a bug or corrupt later state. Usually use `Assert` unless you need a custom assertion. |
22
+ | `FatalError` | Base fatal error class with an `isFatal` flag and structured context. | An error should crash or escape normal expected-error handling. Extend this for domain-specific fatal errors. |
23
+ | `Rethrow` | Helpers for catch blocks that should not swallow fatal errors. | You catch `unknown` but only intend to handle recoverable `Error` instances. |
24
+ | `NotImplementedError` | A fatal placeholder error with tracking context. | A code path intentionally exists before the feature is implemented, and the missing work should be traceable. |
25
+ | `isNodeJSError` | Type guard for Node-style `ErrnoException` properties on `Error`. | You need to inspect fields like `code`, `path`, `errno`, or `syscall` after catching a Node.js error. |
26
26
 
27
27
  ### Type And Value Guards
28
28
 
package/dist/Result.d.ts CHANGED
@@ -40,8 +40,28 @@ export declare const Result: {
40
40
  * A Failure type guard
41
41
  */
42
42
  isFailure: <TError>(maybeFailure: Result<unknown, TError>) => maybeFailure is Failure<TError>;
43
+ map: typeof map;
43
44
  tryCatch: typeof tryCatch;
44
45
  };
46
+ /**
47
+ * Maps a Result's Success value and Failure error.
48
+ */
49
+ declare function map<TValue, TError, TSuccess, TFailure>(result: Result<TValue, TError>, options: {
50
+ success: (value: TValue) => TSuccess;
51
+ failure: (error: TError) => TFailure;
52
+ }): Result<TSuccess, TFailure>;
53
+ /**
54
+ * Maps a Result's Success value.
55
+ */
56
+ declare function map<TValue, TError, TSuccess>(result: Result<TValue, TError>, options: {
57
+ success: (value: TValue) => TSuccess;
58
+ }): Result<TSuccess, TError>;
59
+ /**
60
+ * Maps a Result's Failure error.
61
+ */
62
+ declare function map<TValue, TError, TFailure>(result: Result<TValue, TError>, options: {
63
+ failure: (error: TError) => TFailure;
64
+ }): Result<TValue, TFailure>;
45
65
  /**
46
66
  * Creates a Result from the result of a promise.
47
67
  * If the promise resolves, you will get a {@link Result.Success}.
package/dist/Result.js CHANGED
@@ -34,8 +34,16 @@ export const Result = {
34
34
  return maybeFailure.type === "Failure";
35
35
  },
36
36
  // You can't overload while defining object properties, which is why the function implementation is not inlined
37
+ map,
38
+ // You can't overload while defining object properties, which is why the function implementation is not inlined
37
39
  tryCatch,
38
40
  };
41
+ function map(result, options) {
42
+ if (Result.isFailure(result)) {
43
+ return Result.Failure(options.failure !== undefined ? options.failure(result.error) : result.error);
44
+ }
45
+ return Result.Success(options.success !== undefined ? options.success(result.value) : result.value);
46
+ }
39
47
  function tryCatch(op) {
40
48
  if (TypeGuard.isPromiseLike(op)) {
41
49
  return tryCatchPromise(op);
@@ -1 +1 @@
1
- {"version":3,"file":"Result.js","sourceRoot":"","sources":["../src/Result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAkB1C;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;IAED;;OAEG;IACH,OAAO,EAAE,CAAS,KAAa,EAAmB,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;IAEjF;;OAEG;IACH,SAAS,EAAE,CAAS,YAAqC,EAAmC,EAAE;QAC5F,OAAO,YAAY,CAAC,IAAI,KAAK,SAAS,CAAA;IACxC,CAAC;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AAC,MAAM,CAAC,CAAA;QAChC,CAAC;QAED,gBAAgB;QAChB,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC/B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC;AAED,KAAK,UAAU,eAAe,CAAI,OAAuB;IACvD,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QACtB,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guillaume-docquier/tools-ts",
3
- "version": "7.3.2",
3
+ "version": "7.4.0",
4
4
  "description": "My personal collection of typescript tools",
5
5
  "homepage": "https://github.com/Guillaume-Docquier/tools-ts",
6
6
  "license": "MIT",