Skip to content

Commit

Permalink
feat(prompt): add minLength, maxLength, minTags and maxTags o…
Browse files Browse the repository at this point in the history
…ption to `List` prompt (#37)

closes #13
  • Loading branch information
c4spar committed May 31, 2020
1 parent 9127471 commit 6836a7d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/prompt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ The `Toggle` prompt has all [base options](#base-options) and the following prom
| Param | Type | Required | Description |
| ----- | :--: | :--: | ----------- |
| separator | `string` | No | String separator. Will trim all white-spaces from start and end of string. Defaults to `','`. |
| minLength | `number` | No | Min length of a single tag. Defaults to `0`. |
| maxLength | `number` | No | Max length of a single tag. Defaults to `infinity`. |
| minTags | `number` | No | Min number of tags. Defaults to `0`. |
| maxTags | `number` | No | Max number of tags. Defaults to `infinity`. |

**Example**

Expand Down
37 changes: 36 additions & 1 deletion packages/prompt/prompts/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import { GenericInput, GenericInputPromptOptions, GenericInputPromptSettings } f
// @TODO: add maxLength option to list prompt
export interface ListOptions extends GenericInputPromptOptions<string[]> {
separator?: string;
minLength?: number;
maxLength?: number;
minTags?: number;
maxTags?: number;
}

export interface ListSettings extends GenericInputPromptSettings<string[]> {
separator: string;
minLength: number;
maxLength: number;
minTags: number;
maxTags: number;
}

export class List extends GenericInput<string[], ListSettings> {
Expand All @@ -25,6 +33,10 @@ export class List extends GenericInput<string[], ListSettings> {
return new this( {
pointer: blue( Figures.POINTER_SMALL ),
separator: ',',
minLength: 0,
maxLength: Infinity,
minTags: 0,
maxTags: Infinity,
...options
} ).prompt();
}
Expand Down Expand Up @@ -75,7 +87,30 @@ export class List extends GenericInput<string[], ListSettings> {
}

protected validate( value: string ): boolean | string {
return typeof value === 'string' && value.length > 0;

if ( typeof value !== 'string' ) {
return false;
}

const values = this.transform( value ).filter( val => val !== '' );

for ( const val of values ) {
if ( val.length < this.settings.minLength ) {
return `Value must be longer then ${ this.settings.minLength } but has a length of ${ val.length }.`;
}
if ( val.length > this.settings.maxLength ) {
return `Value can't be longer then ${ this.settings.maxLength } but has a length of ${ val.length }.`;
}
}

if ( values.length < this.settings.minTags ) {
return `The minimum number of tags is ${ this.settings.minTags } but got ${ values.length }.`;
}
if ( values.length > this.settings.maxTags ) {
return `The maximum number of tags is ${ this.settings.maxTags } but got ${ values.length }.`;
}

return true;
}

protected transform( value: string ): string[] {
Expand Down
53 changes: 50 additions & 3 deletions packages/prompt/test/list_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,60 @@ Deno.test( 'prompt list: separator option: "-"', async () => {
Deno.test( 'prompt list: empty value', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( '' );
await List.prompt( 'message' );
List.inject( null as any );
await List.prompt( {
message: 'message',
minTags: 3
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Invalid answer.` ) );
} );

Deno.test( 'prompt list: min length', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( '12' );
await List.prompt( {
message: 'message',
minLength: 3
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Value must be longer then 3 but has a length of 2.` ) );
} );

Deno.test( 'prompt list: max length', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( '123' );
await List.prompt( {
message: 'message',
maxLength: 2,
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Value can't be longer then 2 but has a length of 3.` ) );
} );

Deno.test( 'prompt list: min tags', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( '' );
await List.prompt( {
message: 'message',
minTags: 3
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }The minimum number of tags is 3 but got 0.` ) );
} );

Deno.test( 'prompt list: max tags', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( '123, 456, 789' );
await List.prompt( {
message: 'message',
maxTags: 2,
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }The maximum number of tags is 2 but got 3.` ) );
} );

// @TODO: add maxLength option to list pormpt
Deno.test('prompt list: null value', async () => {
Deno.test( 'prompt list: null value', async () => {
console.log();
await assertThrowsAsync( async () => {
List.inject( null as any );
Expand Down

0 comments on commit 6836a7d

Please sign in to comment.