-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt.js
74 lines (67 loc) · 2.37 KB
/
pt.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
console.log('pt.js loaded');
pt={};
pt.getData=async _=>{
//pt.data = await(await fetch('https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/PeriodicTableJSON.json')).json()
pt.data = await(await fetch('PeriodicTableJSON.json')).json()
// index
pt.sbl = {}
pt.data.elements.forEach(el=>{ // index by symbol to pt.sbl
pt.sbl[el.symbol]=pt[el.symbol]=el // element both as pt[] and pt.sbl[]
})
//debugger
}
pt.dump= (parm='atomic_mass')=>{ // by creating (dumping...) variables in the global scope with names of the elements
if(!pt.sbl){
pt.getData().then(_=>{pt.dump(parm)})
}else{
// shortnames
parm = parm=='electronegativity'? 'electronegativity_pauling': parm
// ---
Object.keys(pt.sbl).forEach(k=>{
window[k]=pt.sbl[k][parm]
})
return `${parm} dumbed in global scope variables with element symbol as name`
}
} ;
pt.eval=(str,show)=>{
str = str.replace(/([A-Z])([a-z]*)(\d)/g,'$1$2*$3')
str = str.replace(/([A-Z])([A-Z])/g,'$1\+$2')
str = str.replace(/(\d)(\D)/g,'$1\+$2')
str = str.replace(/([a-z])([A-Z]+)/g,'$1\+$2')
str = str.replace(/\+\)/g,')')
str = str.replace(/^(\d+)(.*)/,'$1\*\($2\)')
str = str.replace(/\(\+/g,'(')
if(str.match(/[A-Z][A-Z]/g)){
return pt.eval(str,show)
}else{
str=str.replace(/[\+]+/g,'+')
str=str.replace(/\(\*/g,'(')
str=str.replace(/\(+/g,'(')
str=str.replace(/\)+/g,')')
str=str.replace(/([a-z,A-Z])\(/g,'$1+(')
str=str.replace(/(\))(\d)/g,'$1\*$2')
if(show){
console.log(str)
}
return eval(str)
}
}
// Constants and conversion
pt.R=8.31446261815324 // as in PV=nRT at 0 oC, in SI units
pt.A=6.02214076E23 // particles in a mole
// --- E = h · c / λ ---
pt.h=6.62607004E-34 // planks constant in SI,
pt.c=299792458 // speed of light SI
pt.f=96485.3329 // Faraday's constant
// ---|---
pt.k2c=(k=0)=>(k-273.15); //kelvin to celsius
pt.c2k=(c=0)=>(c+273.15); //celsius to kelvin
pt.p2a=(p=1)=>(p/1.01325E5); // pascal to atmospheres at 0 oC
pt.a2p=(a=1)=>(a*1.01325E5); // atmospheres to pascal at 0 oC
pt.nernst=(Eo,Q,n,T=298)=>(Eo-(pt.R*T/(pt.f*n))*Math.log(Q));
(async()=>{
await pt.dump() // dump atomic mass (default parm) as global scope variables (by default)
})();
if(typeof(define)!='undefined'){
define(pt)
}