rsult 1.0.2 → 1.2.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/deno.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@int/rsult",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "exports": "./src/lib.ts"
5
5
  }
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
- "name": "rsult",
3
- "version": "1.0.2",
4
- "description": "",
5
- "main": "src/lib.ts",
6
- "repository": "https://github.com/indicium-ag/rsult",
7
- "keywords": [],
8
- "author": "Kenan Sulayman <kenan@sly.mn>",
9
- "license": "MIT",
10
- "devDependencies": {
11
- "@swc/core": "^1.4.12",
12
- "@swc/jest": "^0.2.36",
13
- "@types/jest": "^29.5.12",
14
- "jest": "^29.7.0"
15
- },
16
- "scripts": {
17
- "test": "echo \"Error: no test specified\" && exit 1"
18
- }
2
+ "name": "rsult",
3
+ "version": "1.2.0",
4
+ "description": "",
5
+ "main": "src/lib.ts",
6
+ "repository": "https://github.com/indicium-ag/rsult",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "keywords": [],
11
+ "author": "Kenan Sulayman <kenan@sly.mn>",
12
+ "license": "MIT",
13
+ "devDependencies": {
14
+ "@swc/core": "^1.4.12",
15
+ "@swc/jest": "^0.2.36",
16
+ "@types/jest": "^29.5.12",
17
+ "jest": "^29.7.0"
18
+ }
19
19
  }
package/readme.md CHANGED
@@ -276,6 +276,7 @@ console.log(result); // "Parsed content: {"parsed":"Resource content"}"
276
276
  #### Conversion Methods
277
277
  - `into_ok()`: Converts from `IResultCore<T, E>` to `T`.
278
278
  - `into_err()`: Converts from `IResultCore<T, E>` to `E`.
279
+ - `transmute()`: Changes the type of `Result<T, E>` to `Result<T, never>` or `Result<never, E>`, respectively. This is particularly useful when trying to forward a `ResultErr` returned by a function whose error type overlaps with the returned error type of the current function, but whose value type does not.
279
280
 
280
281
  #### Checking and Transforming Methods
281
282
  - `is_ok_and(f: (value: T) => boolean)`: Checks if the result is Ok and the contained value passes a specified condition.
@@ -301,7 +302,6 @@ console.log(result); // "Parsed content: {"parsed":"Resource content"}"
301
302
 
302
303
  #### Iteration and Flattening Methods
303
304
  - `iter()`: Returns an iterator over the potentially contained value.
304
- - `transpose()`: Attempts to transpose a `Result` of a `Promise` into a `Promise` of a `Result`.
305
305
  - `flatten()`: Flattens a nested `Result` if the contained value is itself a `Result`.
306
306
 
307
307
  ## Contributing
package/src/lib.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './result.ts';
2
- export * from './option.ts';
1
+ export * from './result';
2
+ export * from './option';
@@ -341,20 +341,6 @@ describe('Result', () => {
341
341
  });
342
342
  });
343
343
 
344
- describe('transpose', () => {
345
- it('returns ResultOk<number, _> for Result<number, _>', () => {
346
- const resultPromise = Ok(5);
347
- const transposed = resultPromise.transpose();
348
- expect(transposed).toEqual(Ok(5));
349
- });
350
-
351
- it('returns ResultErr<_, Error> for Result<_, Error>', () => {
352
- const resultPromise = Err(new Error("Error"));
353
- const transposed = resultPromise.transpose();
354
- expect(transposed).toEqual(Err(new Error("Error")));
355
- });
356
- });
357
-
358
344
  describe('flatten', () => {
359
345
  it('flattens nested Ok Result', () => {
360
346
  const nestedOk = Ok(Ok(5));
package/src/result.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Option, None, Some } from './option.ts';
1
+ import { Option, None, Some } from './option';
2
2
 
3
3
  export type Result<T, E> =
4
4
  ResultOk<T, E> | ResultErr<T, E>;
@@ -121,6 +121,30 @@ export interface IResultCore<T, E> {
121
121
  * console.log(result.into_err());
122
122
  */
123
123
  into_err(): E;
124
+
125
+ /**
126
+ * Converts between different forms of `Result`, allowing the error type to be widened.
127
+ *
128
+ * If called on a `ResultOk<T, E>`, it will return a `Result<T, never>`, effectively discarding
129
+ * the error type. This is useful when you want to use the `Result` in a context that expects
130
+ * a `Result` with a specific error type, but you know that the `Result` is an `Ok` variant.
131
+ *
132
+ * If called on a `ResultErr<T, E>`, it will return a `Result<never, E>`, effectively discarding
133
+ * the value type. This is useful when you want to use the `Result` in a context that expects
134
+ * a `Result` with a specific value type, but you know that the `Result` is an `Err` variant.
135
+ *
136
+ * This is particularly useful when trying to forward a ResultErr returned by a function whose
137
+ * error type overlaps with the returned error type of the current function, but whose value type
138
+ * does not.
139
+ *
140
+ * Usage Example:
141
+ * const result: Result<number, Error> = Ok<number, Error>(5);
142
+ * const transmuted: Result<number, never> = result.transmute();
143
+ *
144
+ * const result: Result<number, Error> = Err<number, Error>(new Error("Failure"));
145
+ * const transmuted: Result<never, Error> = result.transmute();
146
+ */
147
+ transmute(): Result<T, never> | Result<never, E>;
124
148
  }
125
149
 
126
150
  export interface IResultExt<T, E> extends IResultCore<T, E> {
@@ -324,19 +348,6 @@ export interface IResultIteration<T, E> extends IResultCore<T, E> {
324
348
  */
325
349
  iter(): IterableIterator<T>;
326
350
 
327
- /**
328
- * Attempts to transpose a `Result` of a `Promise` into a `Promise` of a `Result`.
329
- * @returns A Promise of a Result if the inner value is a Promise, null otherwise.
330
- *
331
- * Usage Example:
332
- * async function example() {
333
- * const resultPromise = Ok(Promise.resolve(5));
334
- * const transposed = resultPromise.transpose(); // Result<Promise<5>, E> -> Promise<Result<5, E>> | null
335
- * console.log(await transposed); // Prints Ok(5) if the promise resolves successfully.
336
- * }
337
- */
338
- transpose(): Result<T, E>;
339
-
340
351
  /**
341
352
  * Flattens a nested `Result` if the contained value is itself a `Result`.
342
353
  * @returns A single-layer `Result`, by stripping one layer of `Result` container.
@@ -472,10 +483,6 @@ export class ResultOk<T, E> implements IResult<T, E> {
472
483
  return this.value;
473
484
  }
474
485
 
475
- transpose(): Result<T, E> {
476
- return new ResultOk<T, E>(this.value);
477
- }
478
-
479
486
  flatten(): Result<UnwrapResult<T>, E> {
480
487
  if (this.value instanceof ResultOk || this.value instanceof ResultErr) {
481
488
  return this.value;
@@ -493,6 +500,10 @@ export class ResultOk<T, E> implements IResult<T, E> {
493
500
  into_err(): never {
494
501
  throw new Error('Called Result.into_err() on an Ok value: ' + this.value);
495
502
  }
503
+
504
+ transmute(): Result<T, never> {
505
+ return this as any;
506
+ }
496
507
  }
497
508
 
498
509
  export class ResultErr<T, E> implements IResult<T, E> {
@@ -602,12 +613,8 @@ export class ResultErr<T, E> implements IResult<T, E> {
602
613
  return fn(this.value);
603
614
  }
604
615
 
605
- transpose(): Result<T, E> {
606
- return new ResultErr<T, E>(this.value);
607
- }
608
-
609
616
  flatten(): Result<UnwrapResult<T>, E> {
610
- return this.transpose() as Result<never, E>;
617
+ return new ResultErr(this.value) as Result<never, E>;
611
618
  }
612
619
 
613
620
  into_ok(): T {
@@ -617,6 +624,10 @@ export class ResultErr<T, E> implements IResult<T, E> {
617
624
  into_err(): E {
618
625
  return this.value;
619
626
  }
627
+
628
+ transmute(): Result<never, E> {
629
+ return this as any;
630
+ }
620
631
  }
621
632
  export const Ok = <T, E>(val: T): Result<T, never> => {
622
633
  return new ResultOk<T, E>(val) as Result<T, never>;
package/tsconfig.json CHANGED
@@ -2,6 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "module": "commonjs",
4
4
  "target": "es2020",
5
+ "outDir": "./dist",
5
6
  "lib": [
6
7
  "es6",
7
8
  "es7",
@@ -10,7 +11,7 @@
10
11
  "allowJs": false,
11
12
  "moduleResolution": "node",
12
13
  "forceConsistentCasingInFileNames": true,
13
- "allowImportingTsExtensions": true,
14
+ "emitDeclarationOnly": true,
14
15
  "noImplicitReturns": true,
15
16
  "noImplicitThis": true,
16
17
  "noImplicitAny": true,
@@ -30,6 +31,8 @@
30
31
  "src/*.ts"
31
32
  ],
32
33
  "exclude": [
33
- "node_modules/"
34
+ "node_modules/",
35
+ "src/**/*.test.ts",
36
+ "src/*.test.ts"
34
37
  ]
35
38
  }