-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (120 loc) · 4.92 KB
/
index.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
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
const chemicaltools = require('chemicaltools')
const i18next = require('i18next')
const format = require('string-format')
const en = require('./locales/en')
const zh = require('./locales/zh')
format.extend(String.prototype, {})
var reply = function (input) {
var s = input.split(' ')
if (s.length == 1) {
if (["help", "h", "/help", "帮助"].indexOf(input.toLowerCase()) > -1) return i18next.t("help")
if (["element", "/element", "元素"].indexOf(input.toLowerCase()) > -1) return anwserElementTable()
var result = chemicaltools.searchElement(input)
if (result) return anwserElement(result)
result = chemicaltools.calculateMass(input)
if (result) return anwserMass(result)
return i18next.t("wronginput")
} else {
if (["HA", "BOH"].indexOf(s[0]) > -1) return anwserAcid(s)
if (["p", "V", "n", "T"].indexOf(s[0]) > -1) return anwserGas(s)
return anwserDeviation(s)
}
}
var anwserElement = function (info) {
var infolist = ['name', 'symbol', 'iupac', 'number', 'mass', 'origin']
var output = ''
infolist.forEach(function (n) {
output += i18next.t("elementper").format(i18next.t("element." + n), info[n])
})
output += i18next.t("wikipedia").format(info.name, info.iupac)
return output
}
var anwserElementTable = function () {
var output = ''
chemicaltools.elementinfo.forEach(function (info) {
output += "{0}.{1}{2} {3} {4}\n".format(info.number, info.name, info.symbol, info.iupac, info.mass)
})
return output
}
var anwserMass = function (result) {
var output = i18next.t("masshead").format(result.name, parseFloat(result.mass).toFixed(2))
for (var i in result.peratom) {
output += i18next.t("massper").format(result.peratom[i].name, result.peratom[i].iupac, result.peratom[i].symbol, result.peratom[i].atomnumber, parseFloat(result.peratom[i].mass).toFixed(2), parseFloat(result.peratom[i].massper).toFixed(2))
}
return output.substring(0, output.length - 1) + i18next.t("period")
}
var anwserAcid = function (s) {
var AorB = (s[0] == "HA" ? true : false)
var result = chemicaltools.calculateAcid(parseFloat(s[1]), s.slice(2).map(parseFloat), AorB)
var output = "{0}, c={1}mol/L, ".format(s[0], s[1])
var i = 1;
s.slice(2).forEach(function (pKa) {
output += "pK{0}{1}={2}, ".format((AorB ? "a" : "b"), (s.slice(2).length > 1 ? "{0}".format(i++) : ''), pKa)
});
output += i18next.t("pH").format(result.pH.toFixed(2))
result.ion.forEach(function (ion) {
output += "\nc({0})={1}mol/L,".format(ion.name, ion.c.toExponential(2))
})
output = output.substring(0, output.length - 1) + "."
return output
}
var anwserGas = function (s) {
var keys = ["p", "V", "n", "T"]
var input = { p: null, V: null, n: null, T: null }
var unit = { p: "kPa", V: "L", n: "mol", T: "K" }
var i = 1, output = ''
for (var key in input) {
input[key] = (s[0] == key ? null : s[i++])
}
var result = chemicaltools.calculateGas(...keys.map(function (x) {
return input[x]
}))
for (var key in result) {
if (key != s[0]) output += "{0}={1}{2}, ".format(key, result[key], unit[key])
}
output += i18next.t("gas").format(s[0], result[s[0]], unit[s[0]])
return output
}
var anwserDeviation = function (x) {
var numnum = Infinity, pointnum = Infinity
x.forEach(function (xi) {
var len = xi.length
var pointlen = 0
if (xi.substr(0, 1) == "-") len--
if (xi.indexOf(".") >= 0) {
len--
var pointlen = len - xi.indexOf(".")
if (Math.abs(parseFloat(xi)) < 1) {
var zeronum = Math.floor(Math.log((Math.abs(parseFloat(xi)))) / Math.LN10)
len += zeronum
}
}
numnum = Math.min(numnum, len)
pointnum = Math.min(pointnum, pointlen)
});
var result = chemicaltools.calculateDeviation(x.map(parseFloat))
var outputinfo = [
{ name: i18next.t("deviation.input"), value: x.join(", ") },
{ name: i18next.t("deviation.average"), value: result.average.toFixed(pointnum) },
{ name: i18next.t("deviation.ad"), value: result.average_deviation.toFixed(pointnum) },
{ name: i18next.t("deviation.rad"), value: (result.relative_average_deviation * 1000).toExponential(numnum - 1) + "‰" },
{ name: i18next.t("deviation.sd"), value: result.standard_deviation.toExponential(numnum - 1) },
{ name: i18next.t("deviation.rsd"), value: (result.relative_standard_deviation * 1000).toExponential(numnum - 1) + "‰" },
]
var output = ''
outputinfo.forEach(function (info) {
output += "{0}: {1}\n".format(info.name, info.value)
});
return output
}
var replyi18 = function (input, lang = 'en') {
i18next.init({
lng: lang,
resources: {
en: en,
zh: zh
}
})
return reply(input)
}
module.exports = replyi18