-
Notifications
You must be signed in to change notification settings - Fork 0
/
example3.ts
103 lines (99 loc) · 2.53 KB
/
example3.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
function parseINIFileSimple(text) {
const lines = text.split("\n");
const result = {};
for (const line of lines) {
const [key, value] = line.split("=", 2);
result[key.trim()] = value.trim();
}
return result;
}
function parseINIFileCorrect(text) {
const lines = text.split("\n");
const result = {};
let section = {};
for (const line of lines) {
const match: RegExpMatchArray | null = line.match(/^(\w+)=(.*)$/);
if (match !== null) {
const [, key, value] = match;
section[key] = value;
} else if (line.match(/^\s*(;.*)?$/)) {
continue;
} else if (line.match(/^\[(.*)\]$/)) {
section = {};
result[RegExp.$1] = section;
} else {
throw new Error(`Line '${line}' is not valid.`);
}
}
return result;
}
interface Person {
name: string;
}
class C implements Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
function bifilter<T>(xs: T[], predicate: (t: T) => boolean): T[][] {
let yeses = []
let nos = []
for (const x of xs) {
const newLocal = predicate(x);
if (newLocal) {
yeses.push(x)
} else {
nos.push(x)
}
}
return [yeses, nos]
}
interface Channel {
id: number
name: string
url: URL
subscribers: Subscriber[]
}
interface Subscriber {
id: number
name: string
email: string
follows: Channel[]
}
/** this is a test of the extract function refactor
*
* Finds the subscriber with the earliest id.
* Then it transforms that to a formatted list.
*/
function showEarliestSubscriber(subs: Subscriber[]) {
let earliest = subs[0];
let earliestId = Number.MAX_SAFE_INTEGER;
for (const sub of subs) {
if (sub.id < earliestId) {
earliest = sub;
earliestId = sub.id;
}
}
let formatted = `${earliest.name} <${earliest.email}>`
for (const follow of earliest.follows) {
formatted += `\n- ${follow.name} (${follow.url})`
}
return formatted
}
/** This a test of the implement-missing-function codefix
*
* It asks copilot to provide an implementation of findFollowersExcept.
*/
const testSubscribers: Subscriber[] = []
const channel: Channel = {
id: 11001,
name: "TypeScript",
url: new URL("https://www.youtube.com/channel/TYPESCRIPT_URL"),
subscribers: testSubscribers
}
showEarliestSubscriber(findMatchingFollowers(channel, testSubscribers));
/** This is a test of suggesting names for just-generated parameter names
* (It's only offered for the one-at-a-time fix, but usually renames everything anyway)
*/
type ChannelConstructor = (number, string, URL) => Channel