Skip to content

Commit

Permalink
feat(prompt): add label option to Secret prompt and change defaul…
Browse files Browse the repository at this point in the history
…t value of `minLength` to `0`
  • Loading branch information
c4spar committed May 31, 2020
1 parent 2b13fab commit 9127471
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
7 changes: 4 additions & 3 deletions packages/prompt/README.md
Expand Up @@ -101,7 +101,7 @@ The `Input` prompt has all [base](#base-options) and the following prompt specif

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

**↑ back to:** [Prompt types](#-types)
Expand Down Expand Up @@ -149,9 +149,10 @@ 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`. |
| label | `string` | No | Name of secret. Defaults to `Password`. |
| hidden | `number` | No | Hide input during typing and show a fix number of asterisk's on success. |
| minLength | `number` | No | Min length of secret value. Defaults to `0`. |
| maxLength | `number` | No | Max length of secret value. Defaults to `infinity`. |

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

Expand Down
2 changes: 1 addition & 1 deletion packages/prompt/prompts/input.ts
Expand Up @@ -22,7 +22,7 @@ export class Input extends GenericInput<string, InputSettings> {

return new this( {
pointer: blue( Figures.POINTER_SMALL ),
minLength: 1,
minLength: 0,
maxLength: Infinity,
...options
} ).prompt();
Expand Down
9 changes: 6 additions & 3 deletions packages/prompt/prompts/secret.ts
Expand Up @@ -5,12 +5,14 @@ import { Figures } from '../lib/figures.ts';
import { GenericInput, GenericInputPromptOptions, GenericInputPromptSettings } from '../lib/generic-input.ts';

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

export interface SecretSettings extends GenericInputPromptSettings<string> {
label: string;
hidden: boolean;
minLength: number;
maxLength: number;
Expand All @@ -26,8 +28,9 @@ export class Secret extends GenericInput<string, SecretSettings> {

return new this( {
pointer: blue( Figures.POINTER_SMALL ),
label: 'Password',
hidden: false,
minLength: 1,
minLength: 0,
maxLength: Infinity,
...options
} ).prompt();
Expand Down Expand Up @@ -60,10 +63,10 @@ export class Secret extends GenericInput<string, SecretSettings> {
return false;
}
if ( value.length < this.settings.minLength ) {
return `Secret must be longer then ${ this.settings.minLength } but has a length of ${ value.length }.`;
return `${this.settings.label} 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 `${this.settings.label} can't be longer then ${ this.settings.maxLength } but has a length of ${ value.length }.`;
}
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/prompt/test/input_test.ts
Expand Up @@ -23,8 +23,11 @@ Deno.test( 'prompt input: empty value', async () => {
console.log();
await assertThrowsAsync( async () => {
Input.inject( '' );
await Input.prompt( 'message' );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Value must be longer then 1 but has a length of 0.` ) );
await Input.prompt( {
message: 'message',
minLength: 8
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Value must be longer then 8 but has a length of 0.` ) );
} );

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

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

0 comments on commit 9127471

Please sign in to comment.