1
1
import { encode , decode } from 'uint8-to-base64' ;
2
+ import type { IssueData } from './types.ts' ;
2
3
3
4
// transform /path/to/file.ext to file.ext
4
- export function basename ( path ) {
5
+ export function basename ( path : string ) : string {
5
6
const lastSlashIndex = path . lastIndexOf ( '/' ) ;
6
7
return lastSlashIndex < 0 ? path : path . substring ( lastSlashIndex + 1 ) ;
7
8
}
8
9
9
10
// transform /path/to/file.ext to .ext
10
- export function extname ( path ) {
11
+ export function extname ( path : string ) : string {
11
12
const lastSlashIndex = path . lastIndexOf ( '/' ) ;
12
13
const lastPointIndex = path . lastIndexOf ( '.' ) ;
13
14
if ( lastSlashIndex > lastPointIndex ) return '' ;
14
15
return lastPointIndex < 0 ? '' : path . substring ( lastPointIndex ) ;
15
16
}
16
17
17
18
// test whether a variable is an object
18
- export function isObject ( obj ) {
19
+ export function isObject ( obj : any ) : boolean {
19
20
return Object . prototype . toString . call ( obj ) === '[object Object]' ;
20
21
}
21
22
22
23
// returns whether a dark theme is enabled
23
- export function isDarkTheme ( ) {
24
+ export function isDarkTheme ( ) : boolean {
24
25
const style = window . getComputedStyle ( document . documentElement ) ;
25
26
return style . getPropertyValue ( '--is-dark-theme' ) . trim ( ) . toLowerCase ( ) === 'true' ;
26
27
}
27
28
28
29
// strip <tags> from a string
29
- export function stripTags ( text ) {
30
+ export function stripTags ( text : string ) : string {
30
31
return text . replace ( / < [ ^ > ] * > ? / g, '' ) ;
31
32
}
32
33
33
- export function parseIssueHref ( href ) {
34
+ export function parseIssueHref ( href : string ) : IssueData {
34
35
const path = ( href || '' ) . replace ( / [ # ? ] .* $ / , '' ) ;
35
36
const [ _ , owner , repo , type , index ] = / ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ ( i s s u e s | p u l l s ) \/ ( [ 0 - 9 ] + ) / . exec ( path ) || [ ] ;
36
37
return { owner, repo, type, index} ;
37
38
}
38
39
39
40
// parse a URL, either relative '/path' or absolute 'https://localhost/path'
40
- export function parseUrl ( str ) {
41
+ export function parseUrl ( str : string ) : URL {
41
42
return new URL ( str , str . startsWith ( 'http' ) ? undefined : window . location . origin ) ;
42
43
}
43
44
44
45
// return current locale chosen by user
45
- export function getCurrentLocale ( ) {
46
+ export function getCurrentLocale ( ) : string {
46
47
return document . documentElement . lang ;
47
48
}
48
49
49
50
// given a month (0-11), returns it in the documents language
50
- export function translateMonth ( month ) {
51
+ export function translateMonth ( month : number ) {
51
52
return new Date ( Date . UTC ( 2022 , month , 12 ) ) . toLocaleString ( getCurrentLocale ( ) , { month : 'short' , timeZone : 'UTC' } ) ;
52
53
}
53
54
54
55
// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
55
- export function translateDay ( day ) {
56
+ export function translateDay ( day : number ) {
56
57
return new Date ( Date . UTC ( 2022 , 7 , day ) ) . toLocaleString ( getCurrentLocale ( ) , { weekday : 'short' , timeZone : 'UTC' } ) ;
57
58
}
58
59
59
60
// convert a Blob to a DataURI
60
- export function blobToDataURI ( blob ) {
61
+ export function blobToDataURI ( blob : Blob ) : Promise < string > {
61
62
return new Promise ( ( resolve , reject ) => {
62
63
try {
63
64
const reader = new FileReader ( ) ;
64
65
reader . addEventListener ( 'load' , ( e ) => {
65
- resolve ( e . target . result ) ;
66
+ resolve ( e . target . result as string ) ;
66
67
} ) ;
67
68
reader . addEventListener ( 'error' , ( ) => {
68
69
reject ( new Error ( 'FileReader failed' ) ) ;
@@ -75,7 +76,7 @@ export function blobToDataURI(blob) {
75
76
}
76
77
77
78
// convert image Blob to another mime-type format.
78
- export function convertImage ( blob , mime ) {
79
+ export function convertImage ( blob : Blob , mime : string ) : Promise < Blob > {
79
80
return new Promise ( async ( resolve , reject ) => {
80
81
try {
81
82
const img = new Image ( ) ;
@@ -104,7 +105,7 @@ export function convertImage(blob, mime) {
104
105
} ) ;
105
106
}
106
107
107
- export function toAbsoluteUrl ( url ) {
108
+ export function toAbsoluteUrl ( url : string ) : string {
108
109
if ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) {
109
110
return url ;
110
111
}
@@ -118,15 +119,15 @@ export function toAbsoluteUrl(url) {
118
119
}
119
120
120
121
// Encode an ArrayBuffer into a URLEncoded base64 string.
121
- export function encodeURLEncodedBase64 ( arrayBuffer ) {
122
+ export function encodeURLEncodedBase64 ( arrayBuffer : ArrayBuffer ) : string {
122
123
return encode ( arrayBuffer )
123
124
. replace ( / \+ / g, '-' )
124
125
. replace ( / \/ / g, '_' )
125
126
. replace ( / = / g, '' ) ;
126
127
}
127
128
128
- // Decode a URLEncoded base64 to an ArrayBuffer string .
129
- export function decodeURLEncodedBase64 ( base64url ) {
129
+ // Decode a URLEncoded base64 to an ArrayBuffer.
130
+ export function decodeURLEncodedBase64 ( base64url : string ) : ArrayBuffer {
130
131
return decode ( base64url
131
132
. replace ( / _ / g, '/' )
132
133
. replace ( / - / g, '+' ) ) ;
@@ -135,20 +136,22 @@ export function decodeURLEncodedBase64(base64url) {
135
136
const domParser = new DOMParser ( ) ;
136
137
const xmlSerializer = new XMLSerializer ( ) ;
137
138
138
- export function parseDom ( text , contentType ) {
139
+ export function parseDom ( text : string , contentType : DOMParserSupportedType ) : Document {
139
140
return domParser . parseFromString ( text , contentType ) ;
140
141
}
141
142
142
- export function serializeXml ( node ) {
143
+ export function serializeXml ( node : Element | Node ) : string {
143
144
return xmlSerializer . serializeToString ( node ) ;
144
145
}
145
146
146
- export const sleep = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
147
+ export function sleep ( ms : number ) : Promise < void > {
148
+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
149
+ }
147
150
148
- export function isImageFile ( { name, type} ) {
151
+ export function isImageFile ( { name, type} : { name : string , type ?: string } ) : boolean {
149
152
return / \. ( j p e ? g | p n g | g i f | w e b p | s v g | h e i c ) $ / i. test ( name || '' ) || type ?. startsWith ( 'image/' ) ;
150
153
}
151
154
152
- export function isVideoFile ( { name, type} ) {
155
+ export function isVideoFile ( { name, type} : { name : string , type ?: string } ) : boolean {
153
156
return / \. ( m p e ? g | m p 4 | m k v | w e b m ) $ / i. test ( name || '' ) || type ?. startsWith ( 'video/' ) ;
154
157
}
0 commit comments