create an index of available units to convert by name
This commit is contained in:
parent
a2f2064a2f
commit
921e9df04e
@ -43,7 +43,7 @@
|
||||
},
|
||||
{
|
||||
"name": "utility",
|
||||
"version": "3.1.6"
|
||||
"version": "3.1.7"
|
||||
}
|
||||
],
|
||||
"typescript": true
|
||||
|
@ -2,7 +2,7 @@
|
||||
"main": "plugin.js",
|
||||
"name": "utility",
|
||||
"description": "Utility commands and math operations",
|
||||
"version": "3.1.6",
|
||||
"version": "3.1.7",
|
||||
"tags": ["commands", "tools"],
|
||||
"dependencies": ["simplecommands"],
|
||||
"npmDependencies": [
|
||||
|
@ -224,6 +224,59 @@ function pingTcpServer(host: string, port: number): Promise<number> {
|
||||
});
|
||||
}
|
||||
|
||||
// Create an object to efficiently translate unit names to their abbreviations
|
||||
const unitIndex: { [x: string]: string[] } = {};
|
||||
const backupLowercaseUnitTable: { [x: string]: string } = {};
|
||||
function createUnitIndex(): void {
|
||||
const fullList = convert().list();
|
||||
fullList.forEach((unit) => {
|
||||
const { abbr, singular, plural } = unit;
|
||||
if (!unitIndex[abbr]) {
|
||||
unitIndex[abbr] = [
|
||||
abbr, singular.toLowerCase(), plural.toLowerCase()
|
||||
];
|
||||
|
||||
if (abbr === 'l') {
|
||||
unitIndex[abbr].push('liter', 'liters');
|
||||
}
|
||||
|
||||
if (singular.includes('meter')) {
|
||||
unitIndex[abbr].push(
|
||||
singular.replace('meter', 'metre').toLowerCase(),
|
||||
plural.replace('meter', 'metre').toLowerCase(),
|
||||
);
|
||||
}
|
||||
|
||||
backupLowercaseUnitTable[abbr.toLowerCase()] = abbr;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Get a unit by it's full name or abbreviation
|
||||
function getUnitAbbreviation(fullText: string): string | null {
|
||||
// Regular abbreviation
|
||||
if (unitIndex[fullText]) {
|
||||
return fullText;
|
||||
}
|
||||
|
||||
// Lowercase abbreviation
|
||||
const lowerText = fullText.toLowerCase();
|
||||
if (backupLowercaseUnitTable[lowerText]) {
|
||||
return backupLowercaseUnitTable[lowerText];
|
||||
}
|
||||
|
||||
// Text search
|
||||
const found = Object.keys(unitIndex).reduce((previous, current) => {
|
||||
const arrayList = unitIndex[current];
|
||||
if (arrayList.includes(lowerText)) {
|
||||
return current;
|
||||
}
|
||||
return previous;
|
||||
}, '');
|
||||
|
||||
return found || null;
|
||||
}
|
||||
|
||||
function addCommands(plugin: UtilityPlugin, commands: any): void {
|
||||
const cmds = [];
|
||||
|
||||
@ -615,7 +668,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
||||
return true;
|
||||
}
|
||||
|
||||
const src = simplified[1];
|
||||
let src = simplified[1];
|
||||
let dst = simplified[2];
|
||||
if (dst && dst.toLowerCase() === 'to') {
|
||||
dst = simplified[3];
|
||||
@ -632,6 +685,18 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
||||
}
|
||||
|
||||
let res = null;
|
||||
src = getUnitAbbreviation(src);
|
||||
dst = getUnitAbbreviation(dst);
|
||||
|
||||
if (!src) {
|
||||
msg.resolve('Source unit not found!');
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!dst) {
|
||||
msg.resolve('Destination unit not found!');
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
res = convert(tqnt).from(src).to(dst);
|
||||
@ -653,7 +718,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
||||
msg.resolve('Failed to convert.');
|
||||
return true;
|
||||
},
|
||||
description: 'Convert between quantities in different units (abbreviations only).',
|
||||
description: 'Convert between quantities in different units.',
|
||||
usage: '<number> <from unit> <to unit>',
|
||||
aliases: ['cv', 'unit']
|
||||
});
|
||||
@ -827,6 +892,9 @@ class UtilityPlugin extends Plugin {
|
||||
msg.resolve(this.config.config.ipfsGateway + '/ipfs/' + mmatch[1]);
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize list of units
|
||||
createUnitIndex();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user