From 6ef132ba45c5ef05620f8c4c05ce6e28eddf1642 Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 13 Aug 2024 17:20:29 +0800 Subject: [PATCH] feat(koishi): support fuzzy match between `-` and `_` --- packages/core/src/command/command.ts | 6 +++++- packages/core/src/command/index.ts | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/core/src/command/command.ts b/packages/core/src/command/command.ts index 2aa39d1c5..16be10af1 100644 --- a/packages/core/src/command/command.ts +++ b/packages/core/src/command/command.ts @@ -98,8 +98,12 @@ export class Command< } } + static normalize(name: string) { + return name.toLowerCase().replace(/_/g, '-') + } + private _registerAlias(name: string, prepend = false, options: Command.Alias = {}) { - name = name.toLowerCase() + name = Command.normalize(name) if (name.startsWith('.')) name = this.parent.name + name // check global diff --git a/packages/core/src/command/index.ts b/packages/core/src/command/index.ts index 7a072f142..d330e672c 100644 --- a/packages/core/src/command/index.ts +++ b/packages/core/src/command/index.ts @@ -269,7 +269,7 @@ export class Commander { _resolve(key: string) { if (!key) return {} - const segments = key.toLowerCase().split('.') + const segments = Command.normalize(key).split('.') let i = 1, name = segments[0], command: Command while ((command = this.get(name)) && i < segments.length) { name = command.name + '.' + segments[i++] @@ -325,7 +325,7 @@ export class Commander { command(def: string, ...args: [Command.Config?] | [string, Command.Config?]) { const desc = typeof args[0] === 'string' ? args.shift() as string : '' const config = args[0] as Command.Config - const path = def.split(' ', 1)[0].toLowerCase() + const path = Command.normalize(def.split(' ', 1)[0]) const decl = def.slice(path.length) const segments = path.split(/(?=[./])/g)