-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.js
89 lines (80 loc) · 2.34 KB
/
calc.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
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
function yeastCalc(amount, fromUnit, fromType, toUnit = "grams", toType = fromType) {
const factors = {
instant: {
ratio: 1,
units: {
grams: 1,
tsp: 3.15,
tbsp: 9.45,
cups: 151.2
}
},
fresh: {
ratio: 3,
units: {
grams: 1,
tsp: 2.835,
tbsp: 8.504,
cups: 136.08
}
},
active_dry: {
ratio: 1.5,
units: {
grams: 1,
tsp: 2.835,
tbsp: 8.504,
cups: 136.08
}
}
}
if (typeof amount === 'number' && amount > 0 && fromType in factors && toType in factors) {
let val = amount
val = val*factors[toType].ratio/factors[fromType].ratio
if ((fromUnit in factors[fromType]["units"] || fromUnit === "oz") &&
(toUnit in factors[toType]["units"] || toUnit === "oz")) {
val = val*factors[fromType]["units"][fromUnit]/factors[toType]["units"][toUnit]
}
return val
}
}
function displayNumber(num) {
if (typeof num === 'number') {
const lang = window.navigator.userLanguage || window.navigator.language
let txt = num.toString()
if (num > 9999) {
// panic about K/M/G suffixes
// or just return in scientific notation
// probably nobody gets here anyway
return num.toPrecision(2)
} else if (num <= 9999 && Number.isInteger(num)) {
return num
} else {
return Number(num.toPrecision(4)).toLocaleString(lang)
}
} else if (typeof num === 'undefined') {return '⏳'}
}
function yeastConverter() {
const sel = document.querySelectorAll('.selected')
let s = {}
sel.forEach(e => {
s[e.parentNode.getAttribute('class')] = e.innerHTML.replace(" ","_")
//e.removeAttribute('class')
})
s["amount"] = document.getElementsByTagName('input')[0].valueAsNumber
const num = yeastCalc(s.amount, s.fromUnit, s.fromType, s.toUnit, s.toType)
const outNum = displayNumber(num)
document.getElementsByTagName('output')[0].value = outNum
}
function updater(e) {
if (e.tagName === 'BUTTON' && e.getAttribute('class') !== 'selected') {
const choice = e.parentNode.getAttribute('class')
document.querySelector('.'+choice+' > .selected').removeAttribute('class')
e.setAttribute('class','selected')
yeastConverter()
} else if (e.tagName === 'INPUT') {
yeastConverter()
}
}
document.querySelectorAll('button').forEach(e => e.setAttribute('onclick','updater(this)'))
yeastConverter()