Skip to content

Commit

Permalink
feat(and): improve represenation
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 26, 2023
1 parent a81bd14 commit 92fa79f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
6 changes: 6 additions & 0 deletions validators/operators/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ export class AndValidator<In, In_ extends Via, Via extends In = In & In_>
return;
}
}

override toString(): string {
const intl = new Intl.ListFormat("en");

return intl.format(this.validators.map(String));
}
}
5 changes: 3 additions & 2 deletions validators/operators/or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This module is browser compatible.

import { isEmpty, maxBy } from "../../deps.ts";
import { displayOr } from "../utils.ts";
import {
Reporter,
ValidationContext,
Expand Down Expand Up @@ -53,6 +52,8 @@ export class OrValidator<in In = unknown, In_ extends In = In>
}

override toString(): string {
return displayOr(...this.validators);
const intl = new Intl.ListFormat("en", { type: "disjunction" });

return intl.format(this.validators.map(String));
}
}
14 changes: 0 additions & 14 deletions validators/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

export function displayOr(
v1: unknown,
v2: unknown,
...values: readonly unknown[]
): string;
export function displayOr(
...args: readonly [unknown, unknown, ...readonly unknown[]]
): string {
const head = args.slice(0, -1);
const last = args.slice(-1)[0];

return head.join(", ") + " or " + last;
}

/**
* @throws {RangeError} If max less than or equal to min.
*/
Expand Down

0 comments on commit 92fa79f

Please sign in to comment.