diff --git a/squeebot.repo.json b/squeebot.repo.json index e320e91..6340a66 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -43,7 +43,7 @@ }, { "name": "utility", - "version": "3.1.6" + "version": "3.1.7" } ], "typescript": true diff --git a/utility/plugin.json b/utility/plugin.json index 69b09bd..000ebd8 100644 --- a/utility/plugin.json +++ b/utility/plugin.json @@ -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": [ diff --git a/utility/plugin.ts b/utility/plugin.ts index 0bb27c6..d0ab7a0 100644 --- a/utility/plugin.ts +++ b/utility/plugin.ts @@ -224,6 +224,59 @@ function pingTcpServer(host: string, port: number): Promise { }); } +// 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: ' ', 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(); } }