-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc.htm.vtl
112 lines (108 loc) · 3.26 KB
/
src.htm.vtl
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
<!DOCTYPE html>
<title>Market Summaries</title>
<style>
td+td{border-left:1px solid}
table{border-collapse:collapse}
tbody>tr:nth-child(odd){background-color:silver}
</style>
<input autofocus type=search>
<table>
<thead>
<tr>
<th>Symbol
<th>High
<th>Low
<th>Volume
<th>Quote Volume
<th>Percent Change
<th>Updated At
<tbody>
#foreach($result in $input.path('$'))
<tr>
<td>$result.symbol
<td>$result.high
<td>$result.low
<td>$result.volume
<td>$result.quoteVolume
<td>$result.percentChange
<td>$result.updatedAt
#end
</table>
<script>
"use strict"
Array.prototype.sort = function(compareFunction){
let index = 0
for(const element of sort(compareFunction, this)) this[index++] = element
return this
}
for(const td of document.getElementsByTagName("td")) if(+td.textContent) td.textContent = (+td.textContent).toLocaleString("fullwide",{useGrouping:false,maximumFractionDigits:20})
const INPUT = document.querySelector("input")
const TBODY = document.querySelector("tbody")
const ROWS = [...TBODY.rows]
INPUT.oninput = function(){
const searches = this.value.trim().toUpperCase().split(/[^-\w]+/)
TBODY.innerHTML = ""
for(const row of ROWS){
const text = row.cells[0].textContent.trim()
const index = text.indexOf("-")
const market1 = text.substring(0, index)
const market2 = text.substring(index + 1)
const matches = ["", text, market1, market2, market2 + "-" + market1]
loops: for(const search of searches) for(const match of matches) if(search === match){
TBODY.appendChild(row)
break loops
}
}
}
for(const th of document.getElementsByTagName("th")) th.onclick = function(){
const index = this.cellIndex
let sorted = true
for(let i = ROWS.length - 2; i >= 0; --i){
if(compare(ROWS[i].cells[index].textContent, ROWS[i+1].cells[index].textContent) > 0){
sorted = false
break
}
}
if(sorted) ROWS.reverse()
else ROWS.sort((a, b) => compare(a.cells[index].textContent, b.cells[index].textContent))
INPUT.oninput()
}
function compare(a,b){
const numA = +a
const numB = +b
if(!isNaN(numA)) a = numA
if(!isNaN(numB)) b = numB
if(a<b) return -1
if(a>b) return 1
return 0
}
function*sort(compareFunction, arrayLikeObject, start = 0, end = arrayLikeObject.length){
if(start >= end) return
if(end - start <= 1) yield*merge(compareFunction, [arrayLikeObject[start]])
else yield*merge(compareFunction,
sort(compareFunction, arrayLikeObject, start, Math.trunc((start+end)/2)),
sort(compareFunction, arrayLikeObject, Math.trunc((start+end)/2), end)
)
}
function*merge(compareFunction, ...iterables){
const generators = iterables.map(iterable => {
const generator = function*(it){yield*it}(iterable)
const next = generator.next()
generator.done = next.done
generator.value = next.value
return generator
}).filter(gen => !gen.done || gen.value !== undefined)
while(generators.length){
generators[0].index = 0
const min = generators.reduce((min, current, index) => {
current.index = index
return compareFunction(min.value, current.value) > 0 ? current : min
})
yield min.value
const next = min.next()
min.done = next.done
min.value = next.value
if(min.done && min.value === undefined) generators.splice(min.index, 1)
}
}
</script>