Skip to content

Commit 2e03da9

Browse files
committed
refactor: [Command] alias, aliases
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent ae0783f commit 2e03da9

File tree

6 files changed

+56
-45
lines changed

6 files changed

+56
-45
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,27 +436,27 @@ Add a prepared `option`.
436436

437437
#### `Command#alias([alias])`
438438

439-
Get an alias for the command or add command aliases.
439+
Get an alias for the command or add a command alias.
440440

441441
> 👉 **Note**: This method can be called more than once to add multiple aliases.
442442
443443
##### Overloads
444444

445-
- `alias(alias: List<string> | string): this`
445+
- `alias(alias: string): this`
446446
- `alias(): CommandName`
447447

448448
##### Parameters
449449

450-
- `alias` ([`List<string>`](#list) | `string`)
451-
— an alias, or list of aliases, for the command
450+
- `alias` (`string`)
451+
— an alias for the command
452452

453453
##### Returns
454454

455455
([`CommandName`](#commandname) | [`this`](#commandinfo)) Command alias or `this` command
456456

457457
#### `Command#aliases([aliases])`
458458

459-
Get or set aliases for the command.
459+
Get or add aliases for the command.
460460

461461
##### Overloads
462462

@@ -2025,6 +2025,8 @@ Command metadata (TypeScript interface).
20252025
20262026
#### Properties
20272027
2028+
- `aliases` (`Set<string>`)
2029+
— list of command aliases
20282030
- `arguments` ([`Argument[]`](#argumentinfo))
20292031
— list of command arguments
20302032
- `helpOption` ([`Option`](#optioninfo) | `null` | `undefined`)

src/interfaces/__tests__/command.metadata.spec-d.mts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ describe('unit-d:interfaces/CommandMetadata', () => {
2525
expectTypeOf<TestSubject>().toExtend<Expect>()
2626
})
2727

28+
it('should match [aliases: Set<string>]', () => {
29+
expectTypeOf<TestSubject>()
30+
.toHaveProperty('aliases')
31+
.toEqualTypeOf<Set<string>>()
32+
})
33+
2834
it('should match [arguments: Argument[]]', () => {
2935
expectTypeOf<TestSubject>()
3036
.toHaveProperty('arguments')

src/interfaces/command.metadata.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ type Skip = 'arguments' | 'options' | 'subcommands'
2626
* @extends {Omit<CommandInfo,Skip>}
2727
*/
2828
interface CommandMetadata extends Omit<CommandInfo, Skip> {
29+
/**
30+
* List of command aliases.
31+
*/
32+
aliases: Set<string>
33+
2934
/**
3035
* List of command arguments.
3136
*

src/lib/__snapshots__/command.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Command {
6565
"name": "help",
6666
"helpCommand": null,
6767
"hidden": false,
68+
"aliases": Set {},
6869
"arguments": [],
6970
"helpOption": Option {
7071
"info": {
@@ -119,7 +120,6 @@ Command {
119120
"subcommands": Map {},
120121
"version": undefined,
121122
"action": undefined,
122-
"aliases": Set {},
123123
"done": undefined,
124124
"exit": undefined,
125125
"optionPriority": "local",
@@ -154,6 +154,7 @@ Command {
154154
"name": undefined,
155155
"description": undefined,
156156
"hidden": false,
157+
"aliases": Set {},
157158
"arguments": [],
158159
"helpCommand": [Circular],
159160
"helpOption": Option {
@@ -211,7 +212,6 @@ Command {
211212
},
212213
"version": undefined,
213214
"action": undefined,
214-
"aliases": Set {},
215215
"done": undefined,
216216
"exit": undefined,
217217
"optionPriority": "local",

src/lib/__tests__/command.spec.mts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ describe('unit:lib/Command', () => {
234234
aliases = new Set([alias1, alias2])
235235
})
236236

237-
it('should add command aliases and return `this`', () => {
237+
it('should add command alias and return `this`', () => {
238238
// Arrange
239239
const subject: TestSubject = new TestSubject('timezone', {
240240
aliases: alias2
@@ -263,29 +263,29 @@ describe('unit:lib/Command', () => {
263263
})
264264

265265
describe('#aliases', () => {
266-
it('should return list of command aliases', () => {
267-
// Act
268-
const result = new TestSubject().aliases()
269-
270-
// Expect
271-
expect(result).to.be.instanceof(Set).and.empty
272-
expect(result).to.not.be.frozen
273-
})
274-
275-
it('should set command aliases and return `this`', () => {
266+
it('should add command aliases and return `this`', () => {
276267
// Arrange
277-
const aliases: Set<string> = new Set(['tz'])
268+
const a1: string = 't'
269+
const a2: string = 'tz'
278270
const property: string = 'info.aliases'
279-
const subject: TestSubject = new TestSubject('timezone', { aliases: 't' })
271+
const subject: TestSubject = new TestSubject('timezone', { aliases: a1 })
280272

281273
// Act
282-
const result = subject.aliases(aliases)
274+
const result = subject.aliases(a2)
283275

284276
// Expect
285277
expect(result).to.eq(subject)
286278
expect(result).to.have.nested.property(property).be.instanceof(Set)
287-
expect(result).to.have.nested.property(property).eql(aliases)
288-
expect(result).to.have.nested.property(property).not.eq(aliases)
279+
expect(result).to.have.nested.property(property).eql(new Set([a1, a2]))
280+
})
281+
282+
it('should return list of command aliases', () => {
283+
// Act
284+
const result = new TestSubject().aliases()
285+
286+
// Expect
287+
expect(result).to.be.instanceof(Set).and.empty
288+
expect(result).to.not.be.frozen
289289
})
290290
})
291291

src/lib/command.mts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ class Command extends Helpable {
267267

268268
this.info = {
269269
...this.info,
270+
aliases: new Set(),
270271
arguments: [],
271272
helpCommand: null,
272273
helpOption: null,
@@ -313,7 +314,7 @@ class Command extends Helpable {
313314
})
314315

315316
this.action(this.info.action)
316-
this.aliases(this.info.aliases)
317+
this.aliases(data.aliases)
317318
this.done(this.info.done)
318319
this.exiter(this.info.exit)
319320
this.helpCommand(data.helpCommand)
@@ -613,22 +614,20 @@ class Command extends Helpable {
613614
}
614615

615616
/**
616-
* Add command aliases.
617+
* Add a command alias.
617618
*
618-
* > 👉 **Note**: This method can be called more than once to add multiple
619-
* > aliases.
620-
*
621-
* @see {@linkcode List}
619+
* > 👉 **Note**: This method can be called more than once
620+
* > to add multiple aliases.
622621
*
623622
* @public
624623
* @instance
625624
*
626-
* @param {List<string> | string} alias
627-
* An alias, or list of aliases, for the command
625+
* @param {string} alias
626+
* An alias for the command
628627
* @return {this}
629628
* `this` command
630629
*/
631-
public alias(alias: List<string> | string): this
630+
public alias(alias: string): this
632631

633632
/**
634633
* Get an alias for the command.
@@ -639,33 +638,31 @@ class Command extends Helpable {
639638
* @instance
640639
*
641640
* @return {CommandName}
642-
* Alias for `this` command
641+
* Command alias
643642
*/
644643
public alias(): CommandName
645644

646645
/**
647-
* Get an alias for the command or add command aliases.
646+
* Get or add a command alias.
648647
*
649648
* @see {@linkcode CommandName}
650-
* @see {@linkcode List}
651649
*
652650
* @public
653651
* @instance
654652
*
655-
* @param {List<string> | string} [alias]
656-
* An alias, or list of aliases, for the command
653+
* @param {string} [alias]
654+
* An alias for the command
657655
* @return {CommandName | this}
658656
* Command alias or `this` command
659657
*/
660-
public alias(alias?: List<string> | string): CommandName | this {
658+
public alias(alias?: string): CommandName | this {
661659
if (!arguments.length) return fallback([...this.aliases()][0], null)
662660
ok(this.info.aliases instanceof Set, 'expected `info.aliases`')
663-
for (const x of toList(alias)) this.info.aliases.add(x)
664-
return this
661+
return this.info.aliases.add(alias!), this
665662
}
666663

667664
/**
668-
* Set aliases for the command.
665+
* Add aliases for the command.
669666
*
670667
* @see {@linkcode List}
671668
*
@@ -686,12 +683,12 @@ class Command extends Helpable {
686683
* @instance
687684
*
688685
* @return {Set<string>}
689-
* List of aliases for `this` command
686+
* List of command aliases
690687
*/
691688
public aliases(): Set<string>
692689

693690
/**
694-
* Get or set aliases for the command.
691+
* Get or add aliases for the command.
695692
*
696693
* @see {@linkcode List}
697694
*
@@ -706,8 +703,9 @@ class Command extends Helpable {
706703
public aliases(
707704
aliases?: List<string> | string | null | undefined
708705
): Set<string> | this {
709-
if (!arguments.length) return new Set(toList(this.info.aliases))
710-
return this.info.aliases = new Set(toList(aliases)), this
706+
if (!arguments.length) return this.info.aliases
707+
for (const alias of toList(aliases)) this.alias(alias)
708+
return this
711709
}
712710

713711
/**

0 commit comments

Comments
 (0)