-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.ts
79 lines (73 loc) · 2.26 KB
/
models.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
// This defines my expectations of the parse function provide by JSON, yaml and xml.
type ObjectParseType = (arg1: string, arg2?: any) => {[key: string]: any} | unknown;
// FeedSource describes the source of a feed. It includes the URL,
// an optional label, user agent string.
export interface FeedSourceInterface {
url: string;
label: string;
userAgent: string;
fromObject: (args1: {[key: string]: any}) => boolean;
parseWith: (args1: string, fn: ObjectParseType) => boolean;
}
export class FeedSource implements FeedSourceInterface {
url: string = '';
label: string = '';
userAgent: string = '';
fromObject(o: {[key: string]: any}): boolean {
if (o.url !== undefined && o.url !== '') {
this.url = o.url;
}
if (o.label !== undefined && o.label !== '') {
this.label = o.label;
}
if (o.userAgent !== undefined && o.userAgent !== '') {
this.userAgent = o.userAgent;
}
return true;
};
parseWith(s: string, fn: ObjectParseType): boolean {
return this.fromObject(fn(s) as unknown as {[key: string]: any});
};
}
// ParseURLList takes a filename and byte slice source, parses the contents
// returning a map of urls to labels and an error value.
export async function ParseURLList(fName: string, src:string ): Promise<{[key: string]: FeedSource}> {
let urls : {[key: string]: FeedSource} = {};
/*
// Parse the url value collecting our keys and values
s := bufio.NewScanner(bytes.NewBuffer(src))
key, val, userAgent := "", "", ""
line := 1
for s.Scan() {
txt := strings.TrimSpace(s.Text())
if strings.HasPrefix(txt, "#") {
txt = ""
}
if txt != "" {
parts := strings.SplitN(txt, ` "`, 2)
switch len(parts) {
case 1:
key, val, userAgent = parts[0], "", ""
case 2:
key, val, userAgent = parts[0], parts[1], ""
pos := strings.LastIndex(val, `"`)
if pos > -1 {
if len(val) > pos {
userAgent = strings.TrimSpace(val[pos+1:])
}
val = strings.TrimSpace(val[0:pos])
}
val = strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(val, `~`), `"`))
}
urls[key] = &FeedSource{
Url: key,
Label: val,
UserAgent: userAgent,
}
}
line++
}
return urls, nil
*/
return urls;
}