forked from dzmitry-duboyski/2captcha-ts
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgeneric.ts
39 lines (36 loc) · 982 Bytes
/
generic.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
/*
A file fill of generic utility functions.
*/
/**
* Changes 0 and 1 falsy/truthy values into a boolean.
* @private
* @param input boolean or number
*/
export function castBool(input: boolean | number): 0 | 1 {
if (input == false) return 0
if (input == true) return 1
if (input != 0 && input != 1) return 0
else return input
}
/**
* Constructs uri parameters from an object
* @private
* @param input The input object
*/
export function objectToURI(input: {[key: string]: string | number | boolean} | any): string {
let res = "?"
const keys = Object.keys(input)
keys.forEach((key, index) => {
res += encodeURIComponent(key) + "=" + encodeURIComponent(input[key])
if (index + 1 != keys.length) res += "&"
})
return res
}
/**
* Returns a promise that resolves after x ms
* @private
* @param ms time to sleep for
*/
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}