-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathField.ts
80 lines (71 loc) · 1.74 KB
/
Field.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
70
71
72
73
74
75
76
77
78
79
80
import { FieldPosition } from "./FieldPosition";
export type FieldOptions = {
name: string;
kind?: FieldPosition
values?: string[];
};
/**
* A name-value pair representing a single field
* transmitted in an HTTP Request or Response.
*
* The kind will dictate metadata placement within
* an HTTP message.
*
* All field names are case insensitive and
* case-variance must be treated as equivalent.
* Names MAY be normalized but SHOULD be preserved
* for accuracy during transmission.
*/
export class Field {
public readonly name: string;
public readonly kind: FieldPosition;
public values: string[];
constructor({ name, kind = FieldPosition.HEADER, values = [] }: FieldOptions) {
this.name = name;
this.kind = kind;
this.values = values;
}
/**
* Appends a value to the field.
*
* @param value The value to append.
*/
public add(value: string): void {
this.values.push(value);
}
/**
* Overwrite existing field values.
*
* @param values The new field values.
*/
public set(values: string[]): void {
this.values = values;
}
/**
* Remove all matching entries from list.
*
* @param value Value to remove.
*/
public remove(value: string): void {
this.values = this.values.filter((v) => v !== value);
}
/**
* Get comma-delimited string.
*
* @returns String representation of {@link Field}.
*/
public toString(): string {
// Values with spaces or commas MUST be double-quoted
return this.values
.map((v) => (v.includes(",") || v.includes(" ") ? `"${v}"` : v))
.join(", ");
}
/**
* Get string values as a list
*
* @returns Values in {@link Field} as a list.
*/
public get(): string[] {
return this.values;
}
}