-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rangeLength and rangeValue
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |