1
1
import * as streams from "https://deno.land/std@0.183.0/streams/mod.ts" ;
2
2
3
+ /**
4
+ * Downloads a text file and returns the contents.
5
+ * Throws error if fetch fails.
6
+ */
3
7
export async function downloadString ( url : string ) : Promise < string > {
4
8
const textDecoder = new TextDecoder ( ) ;
5
9
const response = await fetch ( url ) ;
@@ -10,24 +14,32 @@ export async function downloadString(url: string): Promise<string> {
10
14
return textDecoder . decode ( await streams . readAll ( reader ) ) ;
11
15
}
12
16
17
+ /**
18
+ * Find the position of the first pattern match.
19
+ * Search from the specified position, or from the beginning if not specified.
20
+ * Returns -1 if the pattern not match.
21
+ */
13
22
export function regexIndexOf ( s : string , pattern : RegExp , offset = 0 ) : number {
14
23
const index = s . slice ( offset ) . search ( pattern ) ;
15
24
return index < 0 ? index : index + offset ;
16
25
}
17
26
27
+ /** Count the number of occurrences of a name. */
18
28
export class Counter {
19
29
#map: Map < string , number > ;
20
30
21
31
constructor ( ) {
22
32
this . #map = new Map ( ) ;
23
33
}
24
34
35
+ /** Increments the count number of the name and returns it. */
25
36
count ( name : string ) : number {
26
37
const value = this . get ( name ) + 1 ;
27
38
this . #map. set ( name , value ) ;
28
39
return value ;
29
40
}
30
41
42
+ /** Returns the count number of the name. */
31
43
get ( name : string ) : number {
32
44
return this . #map. get ( name ) ?? 0 ;
33
45
}
0 commit comments