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",
|
"name": "utility",
|
||||||
"version": "3.1.6"
|
"version": "3.1.7"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"typescript": true
|
"typescript": true
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"main": "plugin.js",
|
"main": "plugin.js",
|
||||||
"name": "utility",
|
"name": "utility",
|
||||||
"description": "Utility commands and math operations",
|
"description": "Utility commands and math operations",
|
||||||
"version": "3.1.6",
|
"version": "3.1.7",
|
||||||
"tags": ["commands", "tools"],
|
"tags": ["commands", "tools"],
|
||||||
"dependencies": ["simplecommands"],
|
"dependencies": ["simplecommands"],
|
||||||
"npmDependencies": [
|
"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 {
|
function addCommands(plugin: UtilityPlugin, commands: any): void {
|
||||||
const cmds = [];
|
const cmds = [];
|
||||||
|
|
||||||
@ -615,7 +668,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const src = simplified[1];
|
let src = simplified[1];
|
||||||
let dst = simplified[2];
|
let dst = simplified[2];
|
||||||
if (dst && dst.toLowerCase() === 'to') {
|
if (dst && dst.toLowerCase() === 'to') {
|
||||||
dst = simplified[3];
|
dst = simplified[3];
|
||||||
@ -632,6 +685,18 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let res = null;
|
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 {
|
try {
|
||||||
res = convert(tqnt).from(src).to(dst);
|
res = convert(tqnt).from(src).to(dst);
|
||||||
@ -653,7 +718,7 @@ function addCommands(plugin: UtilityPlugin, commands: any): void {
|
|||||||
msg.resolve('Failed to convert.');
|
msg.resolve('Failed to convert.');
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
description: 'Convert between quantities in different units (abbreviations only).',
|
description: 'Convert between quantities in different units.',
|
||||||
usage: '<number> <from unit> <to unit>',
|
usage: '<number> <from unit> <to unit>',
|
||||||
aliases: ['cv', 'unit']
|
aliases: ['cv', 'unit']
|
||||||
});
|
});
|
||||||
@ -827,6 +892,9 @@ class UtilityPlugin extends Plugin {
|
|||||||
msg.resolve(this.config.config.ipfsGateway + '/ipfs/' + mmatch[1]);
|
msg.resolve(this.config.config.ipfsGateway + '/ipfs/' + mmatch[1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Initialize list of units
|
||||||
|
createUnitIndex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user