[fix] metric plugin
This commit is contained in:
@@ -14,55 +14,18 @@ const convertion:[string,string,number][] = [
|
|||||||
|
|
||||||
export function metricaPlugin(data:string, toSystem:'metrics'|'imperial'){
|
export function metricaPlugin(data:string, toSystem:'metrics'|'imperial'){
|
||||||
const c = convertion.sort((a,b) => b[0].length - a[0].length);
|
const c = convertion.sort((a,b) => b[0].length - a[0].length);
|
||||||
|
for(let i = 0; i < c.length; i++){
|
||||||
for(let i=0;i<data.length;i++){
|
let [from, to, ratio] = c[i];
|
||||||
function extractNumber(data:string, i:number){
|
if(toSystem === 'imperial'){
|
||||||
let number = '';
|
[from, to] = [to, from];
|
||||||
while(i > 0){
|
ratio = 1 / ratio;
|
||||||
i--;
|
|
||||||
if(data[i] === ' ' || data[i] === '\n' || data[i] === '\t' || data[i] === ','){
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
if(data[i] === '.'){
|
const reg = new RegExp(`(\\d+(?:\\.\\d+)?)\\s*${from}`, 'g');
|
||||||
number = '.' + number;
|
data = data.replace(reg, (_, value) => {
|
||||||
continue;
|
const result = parseFloat(value) * ratio;
|
||||||
}
|
return `${result.toFixed(2)} ${to}`;
|
||||||
if(isNaN(Number(data[i]))){
|
});
|
||||||
break;
|
|
||||||
}
|
|
||||||
number = data[i] + number;
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
while(data[i] === ' ' || data[i] === '\n' || data[i] === '\t' || data[i] === ','){
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
number: number === '' ? 0 : Number(number),
|
|
||||||
index: i
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let sub = ''
|
|
||||||
let sublen = 0;
|
|
||||||
for(let j=0;j<c.length;j++){
|
|
||||||
const [from, to, ratio] = (toSystem === 'imperial') ? c[j] : [c[j][1], c[j][0], 1/c[j][2]];
|
|
||||||
if(sublen !== from.length){
|
|
||||||
sub = data.substring(i,i+from.length)
|
|
||||||
sublen = from.length;
|
|
||||||
}
|
|
||||||
if(sub === from){
|
|
||||||
const n = extractNumber(data, i);
|
|
||||||
if(n .number !== 0){
|
|
||||||
const num = (n.number * ratio)
|
|
||||||
const rto = (num >= 1) ? num.toFixed(0) : num.toFixed(1);
|
|
||||||
const newData = data.substring(0, n.index) + rto + ' ' + to + data.substring(i+from.length);
|
|
||||||
const offset = newData.length - data.length;
|
|
||||||
i += offset;
|
|
||||||
data = newData;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user