-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.js
More file actions
144 lines (128 loc) · 4.22 KB
/
stdlib.js
File metadata and controls
144 lines (128 loc) · 4.22 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// The Standard Library, provides all useful pre-defined functions and methods in WebScript
import { XMLHttpRequest } from "xmlhttprequest"
export class WebScriptError extends Error {
constructor(msg) {
super()
this.message = msg
}
toString() {
return this.message
}
}
// XMLHttpRequest Request Object
export class Request {
constructor(url) {
this.url = url
}
// Send a GET request
get(headers=[]) {
const HTTPRequest = new XMLHttpRequest()
HTTPRequest.open("GET", this.url, false)
for (let i = 0; i < headers.length; i++) {
HTTPRequest.setRequestHeader(headers[i][0], headers[i][1])
}
HTTPRequest.send(null)
return HTTPRequest
}
// Send a POST request
post(args=[]) {
// May be unstable - has not yet been tested
let body = null
let headers = []
if (args.length >= 1) body = args[0]
if (args.length >= 2) headers = args[1]
const HTTPRequest = new XMLHttpRequest()
HTTPRequest.open("POST", this.url, false)
for (let i = 0; i < headers.length; i++) {
HTTPRequest.setRequestHeader(headers[i][0], headers[i][1])
}
HTTPRequest.setRequestHeader()
HTTPRequest.send(body)
return HTTPRequest
}
// Send a PUT request
put(args=[]) {
// May be unstable - has not yet been tested
let body = null
let headers = []
if (args.length >= 1) body = args[0]
if (args.length >= 2) headers = args[1]
const HTTPRequest = new XMLHttpRequest()
HTTPRequest.open("PUT", this.url, false)
for (let i = 0; i < headers.length; i++) {
HTTPRequest.setRequestHeader(headers[i][0], headers[i][1])
}
HTTPRequest.send(body)
return HTTPRequest
}
// Send a DELETE request
delete(args=[]) {
// May be unstable - has not yet been tested
let body = null
let headers = []
if (args.length >= 1) body = args[0]
if (args.length >= 2) headers = args[1]
const HTTPRequest = new XMLHttpRequest()
HTTPRequest.open("DELETE", this.url, false)
for (let i = 0; i < headers.length; i++) {
HTTPRequest.setRequestHeader(headers[i][0], headers[i][1])
}
HTTPRequest.send(body)
return HTTPRequest
}
}
export class BlockchairAPI {
getPrice(args=[]) {
// Get price of crypto in USD
let convertTo = ""
let request
if (this.key) {
request = new Request(`https://api.blockchair.com/${args[0]}/stats?key=${this.key}`)
} else {
request = new Request(`https://api.blockchair.com/${args[0]}/stats`)
}
let data = request.get()
if (args.length > 1) convertTo = args[1]
if (convertTo == "bitcoin" || convertTo == "btc") {
return JSON.parse(data.responseText).data.market_price_btc
} else {
return JSON.parse(data.responseText).data.market_price_usd
}
}
getStats(crypto) {
// Get crypto stats
let request
if (this.key) {
request = new Request(`https://api.blockchair.com/${crypto}/stats?key=${this.key}`)
} else {
request = new Request(`https://api.blockchair.com/${crypto}/stats`)
}
data = request.get()
return JSON.parse(data.responseText).data
}
getOther(url) {
// Get JSON data
let request = new Request(url)
data = request.get()
return JSON.parse(data.responseText).data
}
setAPIKey(key) {
this.key = key
}
}
// Export the standard library
export default {
// request makes a new request object
request: url => new Request(url),
// crypto makes a new BlockchairAPI object
crypto: new BlockchairAPI(),
// display() prints to the console
display: args => console.log(...args),
// random() generates a random number within a min and max
random: ([min, max]) => {
if (min >= 0 && max <= 1) return Math.random()
return Math.random() * (max - min + 1) + min
},
// round() rounds a float/double to the nearest whole number
round: number => Math.round(number)
}