[fix] metrics

This commit is contained in:
kwaroran
2023-12-06 05:28:04 +09:00
parent f3894a26d0
commit bcab72b801

View File

@@ -1,15 +1,15 @@
const convertion:[string,string,number][] = [ const convertion:[string,string,number, boolean][] = [
['kg', 'lbs', 2.20462], ['kg', 'lbs', 2.20462, true],
['m', 'ft', 3.28084], ['m', 'ft', 3.28084, true],
['cm', 'in', 0.393701], ['cm', 'inch', 0.393701, false],
['mm', 'in', 0.0393701], ['mm', 'inch', 0.0393701, false],
['km', 'mi', 0.621371], ['km', 'mi', 0.621371, true],
['killogram', 'pound', 2.20462], ['killogram', 'pound', 2.20462, true],
['meter', 'foot', 3.28084], ['meter', 'foot', 3.28084, true],
['centimeter', 'inch', 0.393701], ['centimeter', 'inch', 0.393701, true],
['millimeter', 'inch', 0.0393701], ['millimeter', 'inch', 0.0393701, true],
['kilometer', 'mile', 0.621371], ['kilometer', 'mile', 0.621371, true],
] ]
export function metricaPlugin(data:string, toSystem:'metrics'|'imperial'){ export function metricaPlugin(data:string, toSystem:'metrics'|'imperial'){
@@ -20,12 +20,23 @@ export function metricaPlugin(data:string, toSystem:'metrics'|'imperial'){
[from, to] = [to, from]; [from, to] = [to, from];
ratio = 1 / ratio; ratio = 1 / ratio;
} }
if(!c[i][3]){
if(toSystem === 'metrics'){
continue;
}
}
const reg = new RegExp(`(\\d+(?:\\.\\d+)?)\\s*${from}`, 'g'); const reg = new RegExp(`(\\d+(?:\\.\\d+)?)\\s*${from}`, 'g');
data = data.replace(reg, (_, value) => { data = data.replace(reg, (_, value) => {
const result = parseFloat(value) * ratio; const result = parseFloat(value) * ratio;
return `${result.toFixed(2)} ${to}`; return `${result.toFixed(2)} ${to}`;
}); });
} }
//convert height like 5' 11'' to 180 cm
const reg = /(\d+)'?\s*(\d+)"?/g;
data = data.replace(reg, (_, feet, inch) => {
const result = parseFloat(feet) * 30.48 + parseFloat(inch) * 2.54;
return `${result.toFixed(2)} cm`;
});
return data return data
} }