-
-
Notifications
You must be signed in to change notification settings - Fork 735
/
Copy pathfunctions.ts
118 lines (103 loc) · 3.31 KB
/
functions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Calculates the square root of a number.
*
* @param x the number to calculate the root of.
* @returns the square root if `x` is non-negative or `NaN` if `x` is negative.
*/
export function sqrt(x: number): number {
return Math.sqrt(x);
}
/**
* Calculates the square root of a number.
*
* `sqrtArrowFunction` is defined using a variable declaration:
*
* ```
* export const sqrtArrowFunction = (x: number): number => Math.sqrt(x);
* ```
*
* TypeDoc is smart and documents `sqrtArrowFunction` as a function rather than a variable.
*
* @param x the number do calculate the root of.
* @returns the square root if `x` is non-negative or `NaN` if `x` is negative.
*/
export const sqrtArrowFunction = (x: number): number => Math.sqrt(x);
/**
* A simple generic function that concatenates two arrays.
*
* Use [`@typeParam <param name>`](https://typedoc.org/tags/typeParam/)
* to document generic type parameters, e.g.
*
* ```text
* @typeParam T the element type of the arrays
* ```
*
* @typeParam T the element type of the arrays
*/
export function concat<T>(array1: T[], array2: T[]): T[] {
return array1.concat(array2);
}
/**
* The options type for {@link makeHttpCallA}.
*/
export interface MakeHttpCallAOptions {
url: string;
/** e.g. GET, POST, PUT, DELETE */
method: string;
/** e.g. `{ 'Authorization': 'Bearer <access token>' }` */
headers: Record<string, string>;
body: string | Blob | FormData;
mode: "cors" | "no-cors" | "same-origin";
}
/**
* A function that takes in an options object that is defined as a separate
* interface and makes an HTTP call.
*
* **Make sure to export the options type when using this pattern.** Otherwise,
* TypeDoc will not document the options.
*/
export function makeHttpCallA(
options: MakeHttpCallAOptions,
): Promise<Response> {
const { url, method, headers, body, mode } = options;
return fetch(url, { method, headers, body, mode });
}
/**
* A function that takes in an options object and makes an HTTP call.
*
* The options type is written directly in the function definition.
*/
export function makeHttpCallB(options: {
url: string;
/** e.g. GET, POST, PUT, DELETE */
method: string;
/** e.g. `{ 'Authorization': 'Bearer <access token>' }` */
headers: Record<string, string>;
body: string | Blob | FormData;
mode: "cors" | "no-cors" | "same-origin";
}): Promise<Response> {
const { url, method, headers, body, mode } = options;
return fetch(url, { method, headers, body, mode });
}
/**
* Stringifies and concatenates two numbers into a single string.
*
* The documentation site allows you to toggle between the different overloads
* of a function. The implementation signature of the overloaded function is not
* included in the documentation.
*/
export function overloadedFunction(a: number, b: number): string;
/**
* Concatenates two strings.
*
* The documentation site allows you to toggle between the different overloads
* of a function. The implementation signature of the overloaded function is not
* included in the documentation.
*/
export function overloadedFunction(a: string, b: string): string;
export function overloadedFunction(a: unknown, b: unknown): string {
return (
(a as { toString(): string }).toString() +
(b as { toString(): string }).toString()
);
}