11import { encode , decode } from 'uint8-to-base64' ;
2+ import type { IssueData } from './types.ts' ;
23
34// transform /path/to/file.ext to file.ext
4- export function basename ( path ) {
5+ export function basename ( path : string ) : string {
56 const lastSlashIndex = path . lastIndexOf ( '/' ) ;
67 return lastSlashIndex < 0 ? path : path . substring ( lastSlashIndex + 1 ) ;
78}
89
910// transform /path/to/file.ext to .ext
10- export function extname ( path ) {
11+ export function extname ( path : string ) : string {
1112 const lastSlashIndex = path . lastIndexOf ( '/' ) ;
1213 const lastPointIndex = path . lastIndexOf ( '.' ) ;
1314 if ( lastSlashIndex > lastPointIndex ) return '' ;
1415 return lastPointIndex < 0 ? '' : path . substring ( lastPointIndex ) ;
1516}
1617
1718// test whether a variable is an object
18- export function isObject ( obj ) {
19+ export function isObject ( obj : any ) : boolean {
1920 return Object . prototype . toString . call ( obj ) === '[object Object]' ;
2021}
2122
2223// returns whether a dark theme is enabled
23- export function isDarkTheme ( ) {
24+ export function isDarkTheme ( ) : boolean {
2425 const style = window . getComputedStyle ( document . documentElement ) ;
2526 return style . getPropertyValue ( '--is-dark-theme' ) . trim ( ) . toLowerCase ( ) === 'true' ;
2627}
2728
2829// strip <tags> from a string
29- export function stripTags ( text ) {
30+ export function stripTags ( text : string ) : string {
3031 return text . replace ( / < [ ^ > ] * > ? / g, '' ) ;
3132}
3233
33- export function parseIssueHref ( href ) {
34+ export function parseIssueHref ( href : string ) : IssueData {
3435 const path = ( href || '' ) . replace ( / [ # ? ] .* $ / , '' ) ;
3536 const [ _ , owner , repo , type , index ] = / ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ ( i s s u e s | p u l l s ) \/ ( [ 0 - 9 ] + ) / . exec ( path ) || [ ] ;
3637 return { owner, repo, type, index} ;
3738}
3839
3940// parse a URL, either relative '/path' or absolute 'https://localhost/path'
40- export function parseUrl ( str ) {
41+ export function parseUrl ( str : string ) : URL {
4142 return new URL ( str , str . startsWith ( 'http' ) ? undefined : window . location . origin ) ;
4243}
4344
4445// return current locale chosen by user
45- export function getCurrentLocale ( ) {
46+ export function getCurrentLocale ( ) : string {
4647 return document . documentElement . lang ;
4748}
4849
4950// given a month (0-11), returns it in the documents language
50- export function translateMonth ( month ) {
51+ export function translateMonth ( month : number ) {
5152 return new Date ( Date . UTC ( 2022 , month , 12 ) ) . toLocaleString ( getCurrentLocale ( ) , { month : 'short' , timeZone : 'UTC' } ) ;
5253}
5354
5455// 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 ) {
5657 return new Date ( Date . UTC ( 2022 , 7 , day ) ) . toLocaleString ( getCurrentLocale ( ) , { weekday : 'short' , timeZone : 'UTC' } ) ;
5758}
5859
5960// convert a Blob to a DataURI
60- export function blobToDataURI ( blob ) {
61+ export function blobToDataURI ( blob : Blob ) : Promise < string > {
6162 return new Promise ( ( resolve , reject ) => {
6263 try {
6364 const reader = new FileReader ( ) ;
6465 reader . addEventListener ( 'load' , ( e ) => {
65- resolve ( e . target . result ) ;
66+ resolve ( e . target . result as string ) ;
6667 } ) ;
6768 reader . addEventListener ( 'error' , ( ) => {
6869 reject ( new Error ( 'FileReader failed' ) ) ;
@@ -75,7 +76,7 @@ export function blobToDataURI(blob) {
7576}
7677
7778// convert image Blob to another mime-type format.
78- export function convertImage ( blob , mime ) {
79+ export function convertImage ( blob : Blob , mime : string ) : Promise < Blob > {
7980 return new Promise ( async ( resolve , reject ) => {
8081 try {
8182 const img = new Image ( ) ;
@@ -104,7 +105,7 @@ export function convertImage(blob, mime) {
104105 } ) ;
105106}
106107
107- export function toAbsoluteUrl ( url ) {
108+ export function toAbsoluteUrl ( url : string ) : string {
108109 if ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) {
109110 return url ;
110111 }
@@ -118,15 +119,15 @@ export function toAbsoluteUrl(url) {
118119}
119120
120121// Encode an ArrayBuffer into a URLEncoded base64 string.
121- export function encodeURLEncodedBase64 ( arrayBuffer ) {
122+ export function encodeURLEncodedBase64 ( arrayBuffer : ArrayBuffer ) : string {
122123 return encode ( arrayBuffer )
123124 . replace ( / \+ / g, '-' )
124125 . replace ( / \/ / g, '_' )
125126 . replace ( / = / g, '' ) ;
126127}
127128
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 {
130131 return decode ( base64url
131132 . replace ( / _ / g, '/' )
132133 . replace ( / - / g, '+' ) ) ;
@@ -135,20 +136,22 @@ export function decodeURLEncodedBase64(base64url) {
135136const domParser = new DOMParser ( ) ;
136137const xmlSerializer = new XMLSerializer ( ) ;
137138
138- export function parseDom ( text , contentType ) {
139+ export function parseDom ( text : string , contentType : DOMParserSupportedType ) : Document {
139140 return domParser . parseFromString ( text , contentType ) ;
140141}
141142
142- export function serializeXml ( node ) {
143+ export function serializeXml ( node : Element | Node ) : string {
143144 return xmlSerializer . serializeToString ( node ) ;
144145}
145146
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+ }
147150
148- export function isImageFile ( { name, type} ) {
151+ export function isImageFile ( { name, type} : { name : string , type ?: string } ) : boolean {
149152 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/' ) ;
150153}
151154
152- export function isVideoFile ( { name, type} ) {
155+ export function isVideoFile ( { name, type} : { name : string , type ?: string } ) : boolean {
153156 return / \. ( m p e ? g | m p 4 | m k v | w e b m ) $ / i. test ( name || '' ) || type ?. startsWith ( 'video/' ) ;
154157}
0 commit comments