diff --git a/README.md b/README.md index f849625..e5f5a0d 100644 --- a/README.md +++ b/README.md @@ -108,10 +108,10 @@ function readFile(path: string): Result { const result = readFile('test.txt'); if (result.ok) { // text contains the file's content - const text = result.val; + const text = result.value; } else { // err equals 'invalid path' - const err = result.val; + const err = result.error; } ``` @@ -178,19 +178,19 @@ _Note: Typescript currently has a [bug](https://github.com/microsoft/TypeScript/ ```typescript let result: Result = Ok(1); if (result.ok) { - // Typescript knows that result.val is a number because result.ok was true - let number = result.val + 1; + // Typescript knows that result.value is a number because result.ok was true + let number = result.value + 1; } else { - // Typescript knows that result.val is an `Error` because result.ok was false - console.error(result.val.message); + // Typescript knows that result.error is an `Error` because result.ok was false + console.error(result.error.message); } if (result.err) { - // Typescript knows that result.val is an `Error` because result.err was true - console.error(result.val.message); + // Typescript knows that result.error is an `Error` because result.err was true + console.error(result.error.message); } else { - // Typescript knows that result.val is a number because result.err was false - let number = result.val + 1; + // Typescript knows that result.value is a number because result.err was false + let number = result.value + 1; } ``` @@ -396,9 +396,9 @@ const greaterThanZero = obs$.pipe( greaterThanZero.subscribe((result) => { if (result.ok) { - console.log('Was greater than zero: ' + result.val); + console.log('Was greater than zero: ' + result.value); } else { - console.log('Got Error Message: ' + result.val); + console.log('Got Error Message: ' + result.error); } }); @@ -502,9 +502,9 @@ const test$ = obs$.pipe( test$.subscribe((result) => { if (result.ok) { - console.log('Got string: ' + result.val); + console.log('Got string: ' + result.value); } else { - console.log('Got error: ' + result.val.message); + console.log('Got error: ' + result.error.message); } }); diff --git a/src/result.ts b/src/result.ts index 59df5e4..1dd6081 100644 --- a/src/result.ts +++ b/src/result.ts @@ -162,7 +162,7 @@ export class ErrImpl implements BaseResult { readonly ok!: false; readonly err!: true; - readonly val!: E; + readonly error!: E; private readonly _stack!: string; @@ -181,7 +181,7 @@ export class ErrImpl implements BaseResult { this.ok = false; this.err = true; - this.val = val; + this.error = val; const stackLines = new Error().stack!.split('\n').slice(2); if (stackLines && stackLines.length > 0 && stackLines[0].includes('ErrImpl')) { @@ -207,22 +207,22 @@ export class ErrImpl implements BaseResult { // The cause casting required because of the current TS definition being overly restrictive // (the definition says it has to be an Error while it can be anything). // See https://github.com/microsoft/TypeScript/issues/45167 - throw new Error(`${msg} - Error: ${toString(this.val)}\n${this._stack}`, { cause: this.val as any }); + throw new Error(`${msg} - Error: ${toString(this.error)}\n${this._stack}`, { cause: this.error as any }); } expectErr(_msg: string): E { - return this.val + return this.error } unwrap(): never { // The cause casting required because of the current TS definition being overly restrictive // (the definition says it has to be an Error while it can be anything). // See https://github.com/microsoft/TypeScript/issues/45167 - throw new Error(`Tried to unwrap Error: ${toString(this.val)}\n${this._stack}`, { cause: this.val as any }); + throw new Error(`Tried to unwrap Error: ${toString(this.error)}\n${this._stack}`, { cause: this.error as any }); } unwrapErr(): E { - return this.val; + return this.error; } map(_mapper: unknown): Err { @@ -234,7 +234,7 @@ export class ErrImpl implements BaseResult { } mapErr(mapper: (err: E) => E2): Err { - return new Err(mapper(this.val)); + return new Err(mapper(this.error)); } mapOr(default_: U, _mapper: unknown): U { @@ -242,7 +242,7 @@ export class ErrImpl implements BaseResult { } mapOrElse(default_: (error: E) => U, _mapper: unknown): U { - return default_(this.val); + return default_(this.error); } or(other: Result): Result { @@ -250,7 +250,7 @@ export class ErrImpl implements BaseResult { } orElse(other: (error: E) => Result): Result { - return other(this.val); + return other(this.error); } toOption(): Option { @@ -258,7 +258,7 @@ export class ErrImpl implements BaseResult { } toString(): string { - return `Err(${toString(this.val)})`; + return `Err(${toString(this.error)})`; } get stack(): string | undefined { @@ -278,13 +278,13 @@ export class OkImpl implements BaseResult { readonly ok!: true; readonly err!: false; - readonly val!: T; + readonly value!: T; /** * Helper function if you know you have an Ok and T is iterable */ [Symbol.iterator](): Iterator ? U : never> { - const obj = Object(this.val) as Iterable; + const obj = Object(this.value) as Iterable; return Symbol.iterator in obj ? obj[Symbol.iterator]() @@ -302,7 +302,7 @@ export class OkImpl implements BaseResult { this.ok = true; this.err = false; - this.val = val; + this.value = val; } /** @@ -310,15 +310,15 @@ export class OkImpl implements BaseResult { * @deprecated in favor of unwrapOr */ else(_val: unknown): T { - return this.val; + return this.value; } unwrapOr(_val: unknown): T { - return this.val; + return this.value; } expect(_msg: string): T { - return this.val; + return this.value; } expectErr(msg: string): never { @@ -326,25 +326,25 @@ export class OkImpl implements BaseResult { } unwrap(): T { - return this.val; + return this.value; } unwrapErr(): never { // The cause casting required because of the current TS definition being overly restrictive // (the definition says it has to be an Error while it can be anything). // See https://github.com/microsoft/TypeScript/issues/45167 - throw new Error(`Tried to unwrap Ok: ${toString(this.val)}`, { cause: this.val as any }); + throw new Error(`Tried to unwrap Ok: ${toString(this.value)}`, { cause: this.value as any }); } map(mapper: (val: T) => T2): Ok { - return new Ok(mapper(this.val)); + return new Ok(mapper(this.value)); } andThen(mapper: (val: T) => Ok): Ok; andThen(mapper: (val: T) => Err): Result; andThen(mapper: (val: T) => Result): Result; andThen(mapper: (val: T) => Result): Result { - return mapper(this.val); + return mapper(this.value); } mapErr(_mapper: unknown): Ok { @@ -352,11 +352,11 @@ export class OkImpl implements BaseResult { } mapOr(_default_: U, mapper: (val: T) => U): U { - return mapper(this.val); + return mapper(this.value); } mapOrElse(_default_: (_error: never) => U, mapper: (val: T) => U): U { - return mapper(this.val); + return mapper(this.value); } or(_other: Result): Result { @@ -368,7 +368,7 @@ export class OkImpl implements BaseResult { } toOption(): Option { - return Some(this.val); + return Some(this.value); } /** @@ -381,11 +381,11 @@ export class OkImpl implements BaseResult { * (this is the `into_ok()` in rust) */ safeUnwrap(): T { - return this.val; + return this.value; } toString(): string { - return `Ok(${toString(this.val)})`; + return `Ok(${toString(this.value)})`; } } @@ -416,7 +416,7 @@ export namespace Result { const okResult = []; for (let result of results) { if (result.ok) { - okResult.push(result.val); + okResult.push(result.value); } else { return result as Err[number]>; } @@ -439,7 +439,7 @@ export namespace Result { if (result.ok) { return result as Ok[number]>; } else { - errResult.push(result.val); + errResult.push(result.error); } } diff --git a/src/rxjs-operators/index.ts b/src/rxjs-operators/index.ts index da51b60..34bb325 100644 --- a/src/rxjs-operators/index.ts +++ b/src/rxjs-operators/index.ts @@ -31,9 +31,9 @@ export function elseMap(mapper: (val: E) => E2): OperatorFunction { if (result.err) { - return mapper(result.val); + return mapper(result.error); } else { - return result.val; + return result.value; } }), ); @@ -47,7 +47,7 @@ export function elseMapTo(value: E2): OperatorFunction, T if (result.err) { return value; } else { - return result.val; + return result.value; } }), ); @@ -67,7 +67,7 @@ export function resultSwitchMap( return source.pipe( switchMap((result) => { if (result.ok) { - return mapper(result.val); + return mapper(result.value); } else { return of(result); } @@ -96,7 +96,7 @@ export function resultMergeMap( return source.pipe( mergeMap((result) => { if (result.ok) { - return mapper(result.val); + return mapper(result.value); } else { return of(result); } @@ -116,7 +116,7 @@ export function filterResultOk(): OperatorFunction, T> { return (source) => { return source.pipe( filter((result): result is Ok => result.ok), - map((result) => result.val), + map((result) => result.value), ); }; } @@ -125,7 +125,7 @@ export function filterResultErr(): OperatorFunction, E> { return (source) => { return source.pipe( filter((result): result is Err => result.err), - map((result) => result.val), + map((result) => result.error), ); }; } @@ -135,7 +135,7 @@ export function tapResultErr(tapFn: (err: E) => void): MonoTypeOperatorFun return source.pipe( tap((r) => { if (!r.ok) { - tapFn(r.val); + tapFn(r.error); } }), ); @@ -147,7 +147,7 @@ export function tapResultOk(tapFn: (val: T) => void): MonoTypeOperatorFunc return source.pipe( tap((r) => { if (r.ok) { - tapFn(r.val); + tapFn(r.value); } }), ); diff --git a/test/err.test.ts b/test/err.test.ts index 2302826..d1814df 100644 --- a/test/err.test.ts +++ b/test/err.test.ts @@ -32,13 +32,13 @@ test('ok, err, and val', () => { expect(err.ok).toBe(false); assert(false); - expect(err.val).toBe(32); - eq(true); + expect(err.error).toBe(32); + eq(true); }); test('static EMPTY', () => { expect(Err.EMPTY).toBeInstanceOf(Err); - expect(Err.EMPTY.val).toBe(undefined); + expect(Err.EMPTY.error).toBe(undefined); eq>(true); }); diff --git a/test/ok.test.ts b/test/ok.test.ts index b1bbfe5..0893bde 100644 --- a/test/ok.test.ts +++ b/test/ok.test.ts @@ -33,13 +33,13 @@ test('ok, err, and val', () => { expect(err.ok).toBe(true); assert(true); - expect(err.val).toBe(32); - eq(true); + expect(err.value).toBe(32); + eq(true); }); test('static EMPTY', () => { expect(Ok.EMPTY).toBeInstanceOf(Ok); - expect(Ok.EMPTY.val).toBe(undefined); + expect(Ok.EMPTY.value).toBe(undefined); eq>(true); }); diff --git a/test/result.test.ts b/test/result.test.ts index 9214284..67f8887 100644 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -115,7 +115,7 @@ test('Result.all', () => { eq>(true); const all3 = Result.all(err0, err1); - expect(all3).toMatchResult(Err(err0.val)); + expect(all3).toMatchResult(Err(err0.error)); eq>(true); const all4 = Result.all(...([] as Result[])); @@ -143,7 +143,7 @@ test('Result.any', () => { eq>(true); const any3 = Result.any(err0, err1); - expect(any3).toMatchResult(Err([err0.val, err1.val])); + expect(any3).toMatchResult(Err([err0.error, err1.error])); eq>(true); const any4 = Result.any(...([] as Result[])); diff --git a/test/util.ts b/test/util.ts index 44f9ba7..15ff146 100644 --- a/test/util.ts +++ b/test/util.ts @@ -23,11 +23,13 @@ declare global { expect.extend({ toMatchResult(received: Result, result: Result) { let pass = true; + const receivedInner = 'value' in received ? received.value : received.error + const resultInner = 'value' in result ? result.value : result.error try { expect(received.ok).toBe(result.ok); - if (received.val !== result.val) { - expect(received.val).toMatchObject(result.val); + if (receivedInner !== resultInner) { + expect(receivedInner).toMatchObject(resultInner); } } catch (e) { pass = false; @@ -35,8 +37,8 @@ expect.extend({ const type = received.ok ? 'Ok' : 'Err'; const expectedType = received.ok ? 'Ok' : 'Err'; - const val = JSON.stringify(received.val); - const expectedVal = JSON.stringify(result.val); + const val = JSON.stringify(receivedInner); + const expectedVal = JSON.stringify(resultInner); return { message: () => `expected ${type}(${val}) ${pass ? '' : 'not '}to equal ${expectedType}(${expectedVal})`, @@ -47,13 +49,15 @@ expect.extend({ let pass = true; let received: Result | undefined; + let receivedInner + const resultInner = 'value' in result ? result.value : result.error try { - obs.subscribe((val) => (received = val)).unsubscribe(); + obs.subscribe((val) => {received = val; receivedInner = 'value' in received ? received.value : received.error}).unsubscribe(); expect(received?.ok).toBe(result.ok); - if (received?.val !== result.val) { - expect(received?.val).toMatchObject(result.val); + if (receivedInner !== resultInner) { + expect(receivedInner).toMatchObject(resultInner); } } catch (e) { pass = false; @@ -61,8 +65,8 @@ expect.extend({ const type = received?.ok ? 'Ok' : 'Err'; const expectedType = received?.ok ? 'Ok' : 'Err'; - const val = JSON.stringify(received?.val); - const expectedVal = JSON.stringify(result.val); + const val = JSON.stringify(receivedInner); + const expectedVal = JSON.stringify(resultInner); return { message: () => `expected ${type}(${val}) ${pass ? '' : 'not '}to equal ${expectedType}(${expectedVal})`,