-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
57 lines (45 loc) · 1.71 KB
/
utils.js
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
import Config from './config.js'
export const deepClone = (object) => JSON.parse(JSON.stringify(object))
export const get_max = (data) => Math.max(...data.map((element) => element.value))
export const get_min = (data) => Math.min(...data.map((element) => element.value))
export const sort_ascending_by_value = (array) => array.sort((a, b) => a.value - b.value)
export const sort_descending_by_value = (array) => array.sort((a, b) => b.value - a.value)
export const sum = (data) =>
deepClone(data)
.map((element) => parseFloat(element.value))
.reduce((a, b) => a + b, 0)
export const quantile = (array, percentage) => {
const sorted = sort_ascending_by_value(deepClone(array))
const position = (sorted.length - 1) * percentage
const base = Math.floor(position)
const rest = position - base
if (sorted[base + 1] !== undefined) {
return (
parseFloat(sorted[base].value) +
rest * (sorted[base + 1].value - sorted[base].value)
).toFixed(2)
}
return sorted[base].value
}
export function remove_children(object) {
while (object.firstChild) {
object.removeChild(object.lastChild)
}
}
export const transpose = (array) => array[0].map((_, colIndex) => array.map((row) => row[colIndex]))
export function get_parameters(url) {
const parameter_strings = url.split('?')[1].split('&')
const parameters = {}
parameter_strings.forEach((string) => {
const pair = string.split('=')
parameters[pair[0]] = pair[1]
})
return parameters
}
export function create_default_option(text) {
const option = new Option(text)
option.selected = true
option.disabled = true
option.value = 'none'
return option
}