Skip to content

Commit

Permalink
feat: add rangeLength and rangeValue
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 19, 2024
1 parent cca9d49 commit 3854202
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/assertions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export * from "./options";
export * from "./or";
export * from "./or2";
export * from "./or3";
export * from "./rangeLength";
export * from "./rangeValue";
export * from "./regex";
export * from "./startsWith";
export * from "./string";
Expand Down
5 changes: 5 additions & 0 deletions src/assertions/rangeLength/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";
import { rangeLength } from ".";

assertType<Assertion<{ length: number }>>(rangeLength(8, 16));
20 changes: 20 additions & 0 deletions src/assertions/rangeLength/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { rangeLength } from "@assertions/rangeLength";
import { fc, test } from "@fast-check/vitest";
import { expect } from "vitest";

const assertion = rangeLength(8, 16);

test.prop([fc.string({ minLength: 8, maxLength: 16 })])(
"should not throw if length of value is in the range of min and max",
(value) => {
expect(() => assertion(value)).not.toThrow();
},
);

test.prop([fc.string({ maxLength: 7 }), fc.string({ minLength: 17 })])(
"should not throw if length of value is not in the range of min and max",
(v1, v2) => {
expect(() => assertion(v1)).toThrow();
expect(() => assertion(v2)).toThrow();
},
);
23 changes: 23 additions & 0 deletions src/assertions/rangeLength/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";

/**
* Checks if length of value is in the range of min and max.
*
* @param min - Minimal value.
* @param max - Maximal value.
*
* @example
* ```ts
* const passwordLength = rangeLength(8, 16);
*
* passwordLength("hello"); // Error: rangeLength
* passwordLength("aaaaaaaaaaaaaaaaaaaa"); // Error: rangeLength
* passwordLength("Test123456"); // passes
* ```
*/
export const rangeLength =
(min: number, max: number): Assertion<{ length: number }> =>
(v) =>
((v as any).length >= min && (v as any).length <= max) ||
error("rangeLength", v, { min, max });
5 changes: 5 additions & 0 deletions src/assertions/rangeValue/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { rangeValue } from "@assertions/rangeValue";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<Assertion<number>>(rangeValue(0, 150));
20 changes: 20 additions & 0 deletions src/assertions/rangeValue/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { rangeValue } from "@assertions/rangeValue";
import { fc, test } from "@fast-check/vitest";
import { expect } from "vitest";

const assertion = rangeValue(0, 150);

test.prop([fc.integer({ min: 0, max: 150 })])(
"should not throw if value is in the range of min and max",
(value) => {
expect(() => assertion(value)).not.toThrow();
},
);

test.prop([fc.integer({ max: -1 }), fc.integer({ min: 151 })])(
"should not throw if value is not in the range of min and max",
(v1, v2) => {
expect(() => assertion(v1)).toThrow();
expect(() => assertion(v2)).toThrow();
},
);
23 changes: 23 additions & 0 deletions src/assertions/rangeValue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";

/**
* Checks if value is in the range of min and max.
*
* @param min - Minimal value.
* @param max - Maximal value.
*
* @example
* ```ts
* const alive = rangeValue(0, 150);
*
* alive(-10); // Error: rangeValue
* alive(450); // Error: rangeValue
* alive(26); // passes
* ```
*/
export const rangeValue =
<$Type>(min: $Type, max: $Type): Assertion<$Type> =>
(v) =>
((v as any) >= min && (v as any) <= max) ||
error("rangeValue", v, { min, max });

0 comments on commit 3854202

Please sign in to comment.