-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseDensity.js
31 lines (28 loc) · 957 Bytes
/
parseDensity.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
import { parseNumbersUnits } from './parseNumbersUnits';
/**
*
* @param {String} string
* @param {object} [options={}]
* @param {object} [options.value={}]
* @param {string} [options.value.defaultUnits]
* @param {string} [options.value.targetUnits]
* @param {object} [options.temperature={}]
* @param {number} [options.temperature.defaultValue]
* @param {string} [options.temperature.defaultUnits]
* @param {string} [options.temperature.targetUnits]
*/
export function parseDensity(string, options = {}) {
string = String(string);
string = string.replace(/\(.*/, '');
string = string.replace('g/cu cm', 'g/cm^3');
let parts = String(string).split(/[@]|, | at /);
let value = parts[0];
let temperature = parts[1];
if (temperature) {
temperature = temperature.replace(/ *t= */, '');
}
return {
value: parseNumbersUnits(value, options.value),
temperature: parseNumbersUnits(temperature, options.temperature),
};
}