Skip to content

Commit

Permalink
feat(prompt): add minLength and maxLength to Input and Secret
Browse files Browse the repository at this point in the history
… prompt's (#36)
  • Loading branch information
c4spar committed May 31, 2020
1 parent 9aaa740 commit 2b13fab
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 14 deletions.
8 changes: 6 additions & 2 deletions examples/prompt/prompt.ts
Expand Up @@ -11,7 +11,9 @@ await Toggle.prompt( {
} );

await Input.prompt( {
message: `What's your name?`
message: `What's your name?`,
minLength: 3,
maxLength: 8
} );

await Number.prompt( {
Expand All @@ -20,7 +22,9 @@ await Number.prompt( {

await Secret.prompt( {
message: 'Enter your password',
hidden: false
hidden: false,
minLength: 3,
maxLength: 8
} );

await Select.prompt( {
Expand Down
9 changes: 8 additions & 1 deletion packages/prompt/README.md
Expand Up @@ -97,7 +97,12 @@ const name: string = await Input.prompt( `What's your name?` );

**Options**

The `Input` prompt has all [base](#base-options) options and no prompt specific options.
The `Input` prompt has all [base](#base-options) and the following prompt specific options.

| Param | Type | Required | Description |
| ----- | :--: | :--: | ----------- |
| minLength | `number` | No | Min length of value. Defaults to `1`. |
| maxLength | `number` | No | Max length of value. Defaults to `infinity`. |

**↑ back to:** [Prompt types](#-types)

Expand Down Expand Up @@ -144,6 +149,8 @@ The `Secret` prompt has all [base options](#base-options) and the following prom

| Param | Type | Required | Description |
| ----- | :--: | :--: | ----------- |
| minLength | `number` | No | Min length of secret value. Defaults to `1`. |
| maxLength | `number` | No | Max length of secret value. Defaults to `infinity`. |
| hidden | `number` | No | Hide input during typing and show a fix number of asterisk's on success. |

**↑ back to:** [Prompt types](#-types)
Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/prompts/confirm.ts
Expand Up @@ -43,7 +43,7 @@ export class Confirm extends GenericInput<boolean, ConfirmSettings> {
return message;
}

protected validate( value: string ): boolean {
protected validate( value: string ): boolean | string {
return typeof value === 'string' &&
[
this.settings.active[ 0 ].toLowerCase(),
Expand Down
21 changes: 17 additions & 4 deletions packages/prompt/prompts/input.ts
Expand Up @@ -3,11 +3,13 @@ import { Figures } from '../lib/figures.ts';
import { GenericInput, GenericInputPromptOptions, GenericInputPromptSettings } from '../lib/generic-input.ts';

export interface InputOptions extends GenericInputPromptOptions<string> {

minLength?: number;
maxLength?: number;
}

export interface InputSettings extends GenericInputPromptSettings<string> {

minLength: number;
maxLength: number;
}

export class Input extends GenericInput<string, InputSettings> {
Expand All @@ -20,12 +22,23 @@ export class Input extends GenericInput<string, InputSettings> {

return new this( {
pointer: blue( Figures.POINTER_SMALL ),
minLength: 1,
maxLength: Infinity,
...options
} ).prompt();
}

protected validate( value: string ): boolean {
return typeof value === 'string' && value.length > 0;
protected validate( value: string ): boolean | string {
if ( typeof value !== 'string' ) {
return false;
}
if ( value.length < this.settings.minLength ) {
return `Value must be longer then ${ this.settings.minLength } but has a length of ${ value.length }.`;
}
if ( value.length > this.settings.maxLength ) {
return `Value can't be longer then ${ this.settings.maxLength } but has a length of ${ value.length }.`;
}
return true;
}

protected transform( value: string ): string | undefined {
Expand Down
19 changes: 17 additions & 2 deletions packages/prompt/prompts/secret.ts
Expand Up @@ -6,10 +6,14 @@ import { GenericInput, GenericInputPromptOptions, GenericInputPromptSettings } f

export interface SecretOptions extends GenericInputPromptOptions<string> {
hidden?: boolean;
minLength?: number;
maxLength?: number;
}

export interface SecretSettings extends GenericInputPromptSettings<string> {
hidden: boolean;
minLength: number;
maxLength: number;
}

export class Secret extends GenericInput<string, SecretSettings> {
Expand All @@ -23,6 +27,8 @@ export class Secret extends GenericInput<string, SecretSettings> {
return new this( {
pointer: blue( Figures.POINTER_SMALL ),
hidden: false,
minLength: 1,
maxLength: Infinity,
...options
} ).prompt();
}
Expand All @@ -49,8 +55,17 @@ export class Secret extends GenericInput<string, SecretSettings> {
return `${ await this.getMessage() } ${ this.settings.pointer } ${ green( value ) }`;
}

protected validate( value: string ): boolean {
return typeof value === 'string' && value.length > 0;
protected validate( value: string ): boolean | string {
if ( typeof value !== 'string' ) {
return false;
}
if ( value.length < this.settings.minLength ) {
return `Secret must be longer then ${ this.settings.minLength } but has a length of ${ value.length }.`;
}
if ( value.length > this.settings.maxLength ) {
return `Secret can't be longer then ${ this.settings.maxLength } but has a length of ${ value.length }.`;
}
return true;
}

protected transform( value: string ): string | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/prompts/select.ts
Expand Up @@ -90,7 +90,7 @@ export class Select<S extends SelectSettings> extends GenericList<string, string
this.writeLine( line );
}

protected validate( value: string ): boolean {
protected validate( value: string ): boolean | string {
return typeof value === 'string' &&
value.length > 0 &&
this.settings.options.findIndex( option => option.value === value ) !== -1;
Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/prompts/toggle.ts
Expand Up @@ -92,7 +92,7 @@ export class Toggle extends GenericInput<boolean, ToggleSettings> {
this.input = this.settings.inactive;
}

protected validate( value: string ): boolean {
protected validate( value: string ): boolean | string {
return [ this.settings.active, this.settings.inactive ].indexOf( value ) !== -1;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/test/input_test.ts
Expand Up @@ -24,7 +24,7 @@ Deno.test( 'prompt input: empty value', async () => {
await assertThrowsAsync( async () => {
Input.inject( '' );
await Input.prompt( 'message' );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Invalid answer.` ) );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Value must be longer then 1 but has a length of 0.` ) );
} );

Deno.test( 'prompt input: invalid value', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/test/secret_test.ts
Expand Up @@ -24,7 +24,7 @@ Deno.test( 'prompt secret: empty value', async () => {
await assertThrowsAsync( async () => {
Secret.inject( '' );
await Secret.prompt( 'message' );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Invalid answer.` ) );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Secret must be longer then 1 but has a length of 0.` ) );
} );

Deno.test( 'prompt secret: invalid value', async () => {
Expand Down

0 comments on commit 2b13fab

Please sign in to comment.