-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
33 lines (32 loc) · 919 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import Guard from "../../Guard";
import TValidate from "../TValidate";
/**
* Validates equality to a literal value.
*
* @param constant - The literal to compare values against.
* Only can be a `string`, `number`, `boolean` or `bigint`.
*
* @returns
* A `Guard` which checks if the given value is equals to the `constant` literal.
*
* `guard.name: "constant(<constant.name>)"`
*
* @example
* ```ts
*
* TConstant("foo").isValid("foobar"); // false
* TConstant("foo").isValid("bar"); // false
* TConstant("foo").isValid("foo"); // true
*
* TConstant("foo").name === 'constant("foo")'; // true
* TConstant(2).name === 'constant(2)'; // true
* ```
*/
export default function TConstant<T extends string | number | boolean | BigInt>(
constant: T
): Guard<T> {
return TValidate<T>(
`constant(${typeof constant === "string" ? `"${constant}"` : constant})`,
(value) => value === constant
);
}