Skip to content

Commit

Permalink
feat(count): improve display format
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 20, 2023
1 parent 04621d8 commit 83e9bbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions validators/iterable/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
import { getSize } from "./utils.ts";
import { format, ScalarValidator } from "../../utils.ts";
import error from "../error.json" assert { type: "json" };
import plural from "../plural.json" assert { type: "json" };

export class CountValidator extends ScalarValidator<Iterable<unknown>> {
constructor(public size: number) {
constructor(public count: number) {
super();
super.expect(({ input }) =>
format(error.should_be_but, this, getSize(input))
);
}

override is(input: Iterable<unknown>): boolean {
return getSize(input) === this.size;
return getSize(input) === this.count;
}

toString(): string {
return `number of ${this.size}`;
const pr = new Intl.PluralRules("en-US");
const suffix = plural[pr.select(this.count)];

return `${this.count} item${suffix}`;
}
}
8 changes: 8 additions & 0 deletions validators/plural.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"zero": "",
"one": "",
"many": "s",
"other": "s",
"two": "s",
"few": "s"
}

0 comments on commit 83e9bbf

Please sign in to comment.