ts-results
Advanced tools
Comparing version 3.2.1 to 3.3.0
import { toString } from './utils'; | ||
import { Ok, Err } from './result'; | ||
/** | ||
@@ -32,2 +33,5 @@ * Contains the None value | ||
}; | ||
NoneImpl.prototype.toResult = function (error) { | ||
return Err(error); | ||
}; | ||
NoneImpl.prototype.toString = function () { | ||
@@ -81,2 +85,5 @@ return 'None'; | ||
}; | ||
SomeImpl.prototype.toResult = function (error) { | ||
return Ok(this.val); | ||
}; | ||
/** | ||
@@ -83,0 +90,0 @@ * Returns the contained `Some` value, but never throws. |
import { toString } from './utils'; | ||
import { None, Some } from './option'; | ||
/** | ||
@@ -13,2 +14,7 @@ * Contains the error value | ||
this.val = val; | ||
var stackLines = new Error().stack.split('\n').slice(2); | ||
if (stackLines && stackLines.length > 0 && stackLines[0].includes('ErrImpl')) { | ||
stackLines.shift(); | ||
} | ||
this._stack = stackLines.join('\n'); | ||
} | ||
@@ -33,6 +39,6 @@ ErrImpl.prototype[Symbol.iterator] = function () { | ||
ErrImpl.prototype.expect = function (msg) { | ||
throw new Error(msg + " - Error: " + toString(this.val)); | ||
throw new Error(msg + " - Error: " + toString(this.val) + "\n" + this._stack); | ||
}; | ||
ErrImpl.prototype.unwrap = function () { | ||
throw new Error("Tried to unwrap Error: " + toString(this.val)); | ||
throw new Error("Tried to unwrap Error: " + toString(this.val) + "\n" + this._stack); | ||
}; | ||
@@ -48,5 +54,15 @@ ErrImpl.prototype.map = function (_mapper) { | ||
}; | ||
ErrImpl.prototype.toOption = function () { | ||
return None; | ||
}; | ||
ErrImpl.prototype.toString = function () { | ||
return "Err(" + toString(this.val) + ")"; | ||
}; | ||
Object.defineProperty(ErrImpl.prototype, "stack", { | ||
get: function () { | ||
return this + "\n" + this._stack; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
/** An empty Err */ | ||
@@ -109,2 +125,5 @@ ErrImpl.EMPTY = new ErrImpl(undefined); | ||
}; | ||
OkImpl.prototype.toOption = function () { | ||
return Some(this.val); | ||
}; | ||
/** | ||
@@ -111,0 +130,0 @@ * Returns the contained `Ok` value, but never throws. |
@@ -0,1 +1,2 @@ | ||
import { Result, Ok, Err } from './result'; | ||
interface BaseOption<T> extends Iterable<T extends Iterable<infer U> ? U : never> { | ||
@@ -29,3 +30,3 @@ /** `true` when the Option is Some */ readonly some: boolean; | ||
/** | ||
* Maps a `Option<T, E>` to `Option<U, E>` by applying a function to a contained `Some` value, | ||
* Maps an `Option<T>` to `Option<U>` by applying a function to a contained `Some` value, | ||
* leaving a `None` value untouched. | ||
@@ -36,2 +37,6 @@ * | ||
map<U>(mapper: (val: T) => U): Option<U>; | ||
/** | ||
* Maps an `Option<T>` to a `Result<T, E>`. | ||
*/ | ||
toResult<E>(error: E): Result<T, E>; | ||
} | ||
@@ -50,2 +55,3 @@ /** | ||
andThen<T2>(op: unknown): None; | ||
toResult<E>(error: E): Err<E>; | ||
toString(): string; | ||
@@ -73,2 +79,3 @@ } | ||
andThen<T2>(mapper: (val: T) => Option<T2>): Option<T2>; | ||
toResult<E>(error: E): Ok<T>; | ||
/** | ||
@@ -75,0 +82,0 @@ * Returns the contained `Some` value, but never throws. |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./utils"], factory); | ||
define(["require", "exports", "./utils", "./result"], factory); | ||
} | ||
@@ -15,2 +15,3 @@ })(function (require, exports) { | ||
var utils_1 = require("./utils"); | ||
var result_1 = require("./result"); | ||
/** | ||
@@ -46,2 +47,5 @@ * Contains the None value | ||
}; | ||
NoneImpl.prototype.toResult = function (error) { | ||
return result_1.Err(error); | ||
}; | ||
NoneImpl.prototype.toString = function () { | ||
@@ -95,2 +99,5 @@ return 'None'; | ||
}; | ||
SomeImpl.prototype.toResult = function (error) { | ||
return result_1.Ok(this.val); | ||
}; | ||
/** | ||
@@ -97,0 +104,0 @@ * Returns the contained `Some` value, but never throws. |
{ | ||
"name": "ts-results", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "A typescript implementation of Rust's Result and Option objects.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -151,6 +151,3 @@ # ts-results | ||
let okResult: Result<number, Error> = Ok(10); | ||
let okResult2 = Ok<number, Error>(10); // Exact same as above | ||
let errorResult: Result<number, Error> = Ok(new Error('bad number!')); | ||
let errorResult2 = Ok<number, Error>(new Error('bad number!')); // Exact same as above | ||
let errorResult: Result<number, Error> = Err(new Error('bad number!')); | ||
``` | ||
@@ -161,3 +158,3 @@ | ||
```typescript | ||
let result = Ok<number, Error>(1); | ||
let result: Result<number, Error> = Ok(1); | ||
if (result.ok) { | ||
@@ -167,3 +164,3 @@ // Typescript knows that result.val is a number because result.ok was true | ||
} else { | ||
// Typescript knows that result.val is an Error because result.ok was false | ||
// Typescript knows that result.val is an `Error` because result.ok was false | ||
console.error(result.val.message); | ||
@@ -173,3 +170,3 @@ } | ||
if (result.err) { | ||
// Typescript knows that result.val is an Error because result.err was true | ||
// Typescript knows that result.val is an `Error` because result.err was true | ||
console.error(result.val.message); | ||
@@ -182,2 +179,11 @@ } else { | ||
### Stack Trace | ||
A stack trace is generated when an `Err` is created. | ||
```typescript | ||
let error = Err('Uh Oh'); | ||
let stack = error.stack; | ||
``` | ||
#### Unwrap | ||
@@ -196,4 +202,4 @@ | ||
```typescript | ||
let goodResult = Ok<number, Error>(1); | ||
let badResult = Err<number, Error>(new Error('something went wrong')); | ||
let goodResult = Ok(1); | ||
let badResult = Err(new Error('something went wrong')); | ||
@@ -200,0 +206,0 @@ goodResult.expect('goodResult should be a number'); // 1 |
@@ -0,1 +1,2 @@ | ||
import { Option } from './option'; | ||
interface BaseResult<T, E> extends Iterable<T extends Iterable<infer U> ? U : never> { | ||
@@ -52,2 +53,8 @@ /** `true` when the result is Ok */ readonly ok: boolean; | ||
mapErr<F>(mapper: (val: E) => F): Result<T, F>; | ||
/** | ||
* Converts from `Result<T, E>` to `Option<T>`, discarding the error if any | ||
* | ||
* Similar to rust's `ok` method | ||
*/ | ||
toOption(): Option<T>; | ||
} | ||
@@ -63,2 +70,3 @@ /** | ||
readonly val: E; | ||
private readonly _stack; | ||
[Symbol.iterator](): Iterator<never, never, any>; | ||
@@ -77,3 +85,5 @@ constructor(val: E); | ||
mapErr<E2>(mapper: (err: E) => E2): Err<E2>; | ||
toOption(): Option<never>; | ||
toString(): string; | ||
get stack(): string | undefined; | ||
} | ||
@@ -108,2 +118,3 @@ export declare const Err: typeof ErrImpl & (<E>(err: E) => Err<E>); | ||
mapErr(_mapper: unknown): Ok<T>; | ||
toOption(): Option<T>; | ||
/** | ||
@@ -110,0 +121,0 @@ * Returns the contained `Ok` value, but never throws. |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./utils"], factory); | ||
define(["require", "exports", "./utils", "./option"], factory); | ||
} | ||
@@ -15,2 +15,3 @@ })(function (require, exports) { | ||
var utils_1 = require("./utils"); | ||
var option_1 = require("./option"); | ||
/** | ||
@@ -27,2 +28,7 @@ * Contains the error value | ||
this.val = val; | ||
var stackLines = new Error().stack.split('\n').slice(2); | ||
if (stackLines && stackLines.length > 0 && stackLines[0].includes('ErrImpl')) { | ||
stackLines.shift(); | ||
} | ||
this._stack = stackLines.join('\n'); | ||
} | ||
@@ -47,6 +53,6 @@ ErrImpl.prototype[Symbol.iterator] = function () { | ||
ErrImpl.prototype.expect = function (msg) { | ||
throw new Error(msg + " - Error: " + utils_1.toString(this.val)); | ||
throw new Error(msg + " - Error: " + utils_1.toString(this.val) + "\n" + this._stack); | ||
}; | ||
ErrImpl.prototype.unwrap = function () { | ||
throw new Error("Tried to unwrap Error: " + utils_1.toString(this.val)); | ||
throw new Error("Tried to unwrap Error: " + utils_1.toString(this.val) + "\n" + this._stack); | ||
}; | ||
@@ -62,5 +68,15 @@ ErrImpl.prototype.map = function (_mapper) { | ||
}; | ||
ErrImpl.prototype.toOption = function () { | ||
return option_1.None; | ||
}; | ||
ErrImpl.prototype.toString = function () { | ||
return "Err(" + utils_1.toString(this.val) + ")"; | ||
}; | ||
Object.defineProperty(ErrImpl.prototype, "stack", { | ||
get: function () { | ||
return this + "\n" + this._stack; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
/** An empty Err */ | ||
@@ -123,2 +139,5 @@ ErrImpl.EMPTY = new ErrImpl(undefined); | ||
}; | ||
OkImpl.prototype.toOption = function () { | ||
return option_1.Some(this.val); | ||
}; | ||
/** | ||
@@ -125,0 +144,0 @@ * Returns the contained `Ok` value, but never throws. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97267
1387
460