Skip to content

Commit

Permalink
feat(core): Add normalized() helper to Assertion base class (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion authored Aug 30, 2023
1 parent 01e0aeb commit a30ae44
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/core/src/lib/Assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ export class Assertion<T> {
this.not = new Proxy(this, { get: this.proxyInverter(true) });
}

/**
* A convenience method to normalize the assertion instance. If it was
* inverted with `.not`, it'll return it back to the previous non-inverted
* state. Otherwise, it returns the same instance.
*
* @returns the normalized assertion instance
*/
protected normalized(): this {
return this.inverted
? new Proxy(this, { get: this.proxyInverter(false) })
: this;
}

/**
* A convenience method to execute the assertion. The inversion logic for
* `.not` is already embedded in this method, so this should always be used
Expand All @@ -79,9 +92,7 @@ export class Assertion<T> {
throw invertedError;
}

return this.inverted
? new Proxy(this, { get: this.proxyInverter(false) })
: this;
return this.normalized();
}

private proxyInverter(isInverted: boolean): ProxyHandler<this>["get"] {
Expand Down

0 comments on commit a30ae44

Please sign in to comment.