-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforced-string.test.ts
69 lines (62 loc) · 1.86 KB
/
enforced-string.test.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { EnforcedString, TestType } from 'src';
import { test, expect } from 'vitest';
/**
* Enforces a string to start with a specific prefix and end with a specific suffix.
* Defaults for both prefix and suffix are empty strings.
*
* @example
* ```ts
* type Result = EnforcedString<'pk_123'>; // Result: 'pk_123'
* type ResultWithPrefix = EnforcedString<'pk_123', 'pk_'>; // Result: 'pk_123'
* type ResultWithSuffix = EnforcedString<'123', '', '123'>; // Result: '123'
* type InvalidResult = EnforcedString<'123_pk', 'pk_', '123'>; // Error: Type '123_pk' does not satisfy the constraint
* ```
*/
test('valid string with default prefix and suffix', () => {
const result: TestType<EnforcedString<'pk_123'>, 'pk_123', true> = true;
expect(result).toBe(true);
});
test('valid string with specified prefix', () => {
const result: TestType<
EnforcedString<'pk_123', 'pk_'>,
'pk_123',
true
> = true;
expect(result).toBe(true);
});
test('valid string with specified suffix', () => {
const result: TestType<EnforcedString<'123', '', '123'>, '123', true> = true;
expect(result).toBe(true);
});
test('valid string with both prefix and suffix', () => {
const result: TestType<
EnforcedString<'pk_123', 'pk_', '123'>,
'pk_123',
true
> = true;
expect(result).toBe(true);
});
test('invalid string with incorrect prefix', () => {
const result: TestType<
EnforcedString<'123_pk', 'pk_', '123'>,
never,
true
> = true;
expect(result).toBe(true);
});
test('invalid string with incorrect suffix', () => {
const result: TestType<
EnforcedString<'pk_123', 'pk_', '456'>,
never,
true
> = true;
expect(result).toBe(true);
});
test('invalid string with both incorrect prefix and suffix', () => {
const result: TestType<
EnforcedString<'123_pk', 'pk_', '456'>,
never,
true
> = true;
expect(result).toBe(true);
});