/
webSegment.ts
36 lines (34 loc) · 1.29 KB
/
webSegment.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
import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';
import { buildAlphaNumericPercentArbitrary } from './_internals/builders/CharacterRangeArbitraryBuilder';
import { stringOf } from './stringOf';
import { SizeForArbitrary } from './_internals/helpers/MaxLengthFromMinLength';
/**
* Constraints to be applied on {@link webSegment}
* @remarks Since 2.22.0
* @public
*/
export interface WebSegmentConstraints {
/**
* Define how large the generated values should be (at max)
* @remarks Since 2.22.0
*/
size?: Exclude<SizeForArbitrary, 'max'>;
}
/**
* For internal segment of an URI (web included)
*
* According to {@link https://www.ietf.org/rfc/rfc3986.txt | RFC 3986}
*
* eg.: In the url `https://github.com/dubzzz/fast-check/`, `dubzzz` and `fast-check` are segments
*
* @param constraints - Constraints to apply when building instances (since 2.22.0)
*
* @remarks Since 1.14.0
* @public
*/
export function webSegment(constraints: WebSegmentConstraints = {}): Arbitrary<string> {
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
// segment = *pchar
const others = ['-', '.', '_', '~', '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=', ':', '@'];
return stringOf(buildAlphaNumericPercentArbitrary(others), { size: constraints.size });
}